Github   Telegram
No Matches
Classes | Public Types | Public Member Functions | List of all members
utils::PeriodicTask Class Referencefinal

Task that periodically runs a user callback. More...

#include <userver/utils/periodic_task.hpp>


struct  Settings

Public Types

enum class  Flags {
  kNone = 0 ,
  kNow = 1 << 0 ,
  kStrong = 1 << 1 ,
  kChaotic = 1 << 2 ,
  kCritical = 1 << 4
using Callback = std::function< void()>

Public Member Functions

 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. More...
void SetSettings (Settings settings)
 Set all settings except flags. All flags must be set at the start.
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.

Detailed Description

Task that periodically runs a user callback.

Definition at line 27 of file periodic_task.hpp.

Member Typedef Documentation

◆ Callback

using utils::PeriodicTask::Callback = std::function<void()>

Definition at line 88 of file periodic_task.hpp.

Member Enumeration Documentation

◆ Flags

enum class utils::PeriodicTask::Flags

Immediately call a function.


Account function call time as a part of wait period.


Randomize wait period (+-25% by default)


Use engine::Task::Importance::kCritical flag

Although this periodic task cannot be cancelled due to system overload, it's cancelled upon calling Stop. Subtasks that may be spawned in the callback are not critical by default and may be cancelled as usual.

Definition at line 29 of file periodic_task.hpp.

Member Function Documentation

◆ IsRunning()

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.

◆ RegisterInTestsuite()

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.

◆ ResumeDebug()

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.

◆ Stop()

void utils::PeriodicTask::Stop ( )

Stops the PeriodicTask. If a Step() is in progress, cancels it and waits for its completion.

PeriodicTask must be stopped before the callback becomes invalid. E.g. if your class X stores PeriodicTask and the callback is class' X method, you have to explicitly stop PeriodicTask in ~X() as after ~X() exits the object is destroyed and using X's 'this' in callback is UB.

◆ SuspendDebug()

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.

◆ SynchronizeDebug()

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_spanrun periodic task current span if true. It's here for backward compatibility with existing tests. Will be removed in TAXIDATA-1499.
true if task was successfully executed.
On concurrent invocations, the task is guaranteed to be invoked serially, one time after another.

The documentation for this class was generated from the following file: