#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:
A
is +/- distribution * rand(0.0, 1.0)
if Flags::kChaotic flag is set, otherwise is 0
;B
is the time of previous callback execution if Flags::kStrong flag is set, otherwise is 0
;TaskProcessor to execute the callback and many other options are specified in PeriodicTask::Settings.
Definition at line 38 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) | |
Constructs the periodic task and calls Start() | |
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. | |
using utils::PeriodicTask::Callback = std::function<void()> |
Signature of the task to be executed each period.
Definition at line 120 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 40 of file periodic_task.hpp.
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.
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. |