#include <userver/utils/periodic_task.hpp>
Task that periodically runs a user callback. Callback is started after the previous callback execution is finished every period + A - B, where:
TaskProcessor to execute the callback and many other options are specified in PeriodicTask::Settings.
Definition at line 45 of file periodic_task.hpp.
Classes | |
| struct | Settings |
| Configuration parameters for PeriodicTask. More... | |
Public Types | |
| enum class | Flags { kNone = 0 , kNow = 1 << 0 , kStrong = 1 << 1 , kChaotic = 1 << 2 , kCritical = 1 << 4 } |
| using | Callback = std::function<void()> |
| Signature of the task to be executed each period. | |
Public Member Functions | |
| PeriodicTask () | |
| Default constructor that does nothing. | |
| PeriodicTask (PeriodicTask &&)=delete | |
| PeriodicTask (const PeriodicTask &)=delete | |
| PeriodicTask (std::string name, Settings settings, Callback callback) | |
| void | Start (std::string name, Settings settings, Callback callback) |
| void | Stop () noexcept |
| Stops the PeriodicTask. If a Step() is in progress, cancels it and waits for its completion. | |
| void | SetSettings (Settings settings) |
| Set all settings except flags. All flags must be set at the start. | |
| void | ForceStepAsync () |
| Non-blocking force next iteration. | |
| bool | SynchronizeDebug (bool preserve_span=false) |
| void | SuspendDebug () |
| void | ResumeDebug () |
| bool | IsRunning () const |
| void | RegisterInTestsuite (::testsuite::PeriodicTaskControl &periodic_task_control) |
| Settings | GetCurrentSettings () const |
| Get current settings. Note that they might become stale very quickly. | |
| std::string_view | GetName () const |
| Returns the task name, or "<name not set>" before a name is assigned. The returned view remains valid until this object is destroyed. | |
| using utils::PeriodicTask::Callback = std::function<void()> |
Signature of the task to be executed each period.
Definition at line 138 of file periodic_task.hpp.
|
strong |
| Enumerator | |
|---|---|
| kNone | None of the below flags. |
| kNow | Immediately call a function. |
| kStrong | Account function call time as a part of wait period. |
| kChaotic | Randomize wait period (+-25% by default). |
| kCritical |
|
Definition at line 47 of file periodic_task.hpp.
Constructs the periodic task and calls Start()
| void utils::PeriodicTask::ForceStepAsync | ( | ) |
Non-blocking force next iteration.
Returns immediately, without waiting for Step() to finish.
| bool utils::PeriodicTask::IsRunning | ( | ) | const |
Checks if a periodic task (not a single iteration only) is running. It may be in a callback execution or sleeping between callbacks.
| void utils::PeriodicTask::RegisterInTestsuite | ( | ::testsuite::PeriodicTaskControl & | periodic_task_control | ) |
Make this periodic task available for testsuite. Testsuite provides a way to call it directly from testcase.
| void utils::PeriodicTask::ResumeDebug | ( | ) |
Stop skipping Step() calls from loop. Returns without waiting for DoStep() call. The purpose is to control task execution from tests.
Stops the periodic execution of previous task and starts the periodic execution of the new task.
|
noexcept |
Stops the PeriodicTask. If a Step() is in progress, cancels it and waits for its completion.
| void utils::PeriodicTask::SuspendDebug | ( | ) |
Skip Step() calls from loop until ResumeDebug() is called. If DoStep() is executing, wait its completion, for a potentially long time. The purpose is to control task execution from tests.
| bool utils::PeriodicTask::SynchronizeDebug | ( | bool | preserve_span = false | ) |
Force next DoStep() iteration. It is guaranteed that there is at least one call to DoStep() during SynchronizeDebug() execution. DoStep() is executed as usual in the PeriodicTask's task (NOT in current task).
| preserve_span | run periodic task current span if true. It's here for backward compatibility with existing tests. Will be removed in TAXIDATA-1499. |
|
friend |
Registers a high-level business periodic task for scoped execution in production and manual invocation in testsuite.
In production, registration during construction defers startup until the owner is fully constructed. Registration on an already-open utils::ResourceScopeStorage starts immediately. Initial settings are applied synchronously. Changes made through utils::PeriodicTask::SetSettings before scope opening are used at startup. Without an explicit task processor, the worker uses the task processor on which the scope opens. Closing the scope cancels and joins the worker. The task and callback dependencies must remain alive until the scope closes. Register each task only once and do not start it manually while the scope owns it. See where to obtain scopes.
In testsuite mode, opening the scope registers the callback as periodic/<name> without starting the worker. This makes tests deterministic by letting them decide when to run the callback. Use raw utils::PeriodicTask::Start only for low-level maintenance tasks such as network keepalive or housekeeping.
In testsuite mode, scope cleanup unregisters the task using testsuite::TestsuiteTasks::UnregisterTask. The registry must outlive the scope. Unregistering does not wait for pending or in-flight invocations; callers must ensure those have finished before destroying the callback's dependencies.
| std::runtime_error | At scope opening if the testsuite task name is already registered. |