#include <userver/dist_lock/dist_locked_worker.hpp>
Perpetually acquires a distributed lock and runs a callback while it is held and runs user callback in a separate task while the lock is held. Cancels the task when the lock is lost.
Definition at line 31 of file dist_locked_worker.hpp.
Public Types | |
| using | WorkerFunc = std::function<void()> |
Public Member Functions | |
| DistLockedWorker (std::string name, WorkerFunc worker_func, std::shared_ptr< DistLockStrategyBase > strategy, const DistLockSettings &settings={}, engine::TaskProcessor *task_processor=nullptr, logging::Level locker_log_level=logging::Level::kInfo) | |
| const std::string & | Name () const |
| Name of the worker. | |
| DistLockSettings | GetSettings () const |
| Retrieves settings in a thread-safe way. | |
| void | UpdateSettings (const DistLockSettings &) |
| Update settings in a thread-safe way. | |
| void | Start () |
| void | Stop () |
| void | RunOnce () |
| bool | IsRunning () const |
| bool | OwnsLock () const noexcept |
| std::optional< std::chrono::steady_clock::duration > | GetLockedDuration () const |
| const Statistics & | GetStatistics () const |
| Returns lock acquisition statistics. | |
| bool | IsCancelAdvised () const |
| Returns true when it is advised to cancel the task. | |
| using dist_lock::DistLockedWorker::WorkerFunc = std::function<void()> |
Definition at line 33 of file dist_locked_worker.hpp.
| dist_lock::DistLockedWorker::DistLockedWorker | ( | std::string | name, |
| WorkerFunc | worker_func, | ||
| std::shared_ptr< DistLockStrategyBase > | strategy, | ||
| const DistLockSettings & | settings = {}, | ||
| engine::TaskProcessor * | task_processor = nullptr, | ||
| logging::Level | locker_log_level = logging::Level::kInfo ) |
Creates a DistLockedWorker.
| name | name of the worker |
| worker_func | a callback that's started each time we acquire the lock and is cancelled when the lock is lost. |
| settings | distributed lock settings |
| strategy | distributed locking strategy |
| task_processor | TaskProcessor for running worker_func, using current TaskProcessor if nullptr |
| locker_log_level | Log level for Locker (default is info) |
| std::optional< std::chrono::steady_clock::duration > dist_lock::DistLockedWorker::GetLockedDuration | ( | ) | const |
Returns for how long the lock is held (if held at all). Returned value may be less than the real duration.
| bool dist_lock::DistLockedWorker::IsRunning | ( | ) | const |
|
noexcept |
| void dist_lock::DistLockedWorker::RunOnce | ( | ) |
Run task once acquiring the lock.
| std::exception | rethrows exception from worker_func. |
| void dist_lock::DistLockedWorker::Start | ( | ) |
Starts acquiring the lock. Please note that it's possible that the lock is acquired and the WorkerFunc is entered before Start() returns.
| void dist_lock::DistLockedWorker::Stop | ( | ) |
Stops acquiring the lock. It is guaranteed that the lock is not held after Stop() return and WorkerFunc is stopped (if was started).