userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
dist_locked_task.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/dist_lock/dist_locked_task.hpp
4
/// @brief @copybrief dist_lock::DistLockedTask
5
6
#
include
<
chrono
>
7
#
include
<
functional
>
8
#
include
<
memory
>
9
#
include
<
optional
>
10
#
include
<
string
>
11
12
#
include
<
userver
/
dist_lock
/
dist_lock_settings
.
hpp
>
13
#
include
<
userver
/
dist_lock
/
dist_lock_strategy
.
hpp
>
14
#
include
<
userver
/
engine
/
task
/
task_base
.
hpp
>
15
#
include
<
userver
/
engine
/
task
/
task_processor_fwd
.
hpp
>
16
#
include
<
userver
/
logging
/
level
.
hpp
>
17
18
USERVER_NAMESPACE_BEGIN
19
20
namespace
dist_lock
{
21
namespace
impl {
22
23
class
Locker;
24
25
}
// namespace impl
26
27
/// @ingroup userver_concurrency
28
///
29
/// @brief A task that tries to acquire a distributed lock and runs user
30
/// callback once while the lock is held.
31
///
32
/// When dist lock starts, the lock worker tries to take a lock in the
33
/// loop. If succeeded, a task is launched that executes the user code.
34
/// In the background, dist lock tries to extend the lock. In case of loss of
35
/// the lock, the user task is canceled.
36
///
37
/// ## Example with retrying
38
/// @snippet dist_lock/dist_lock_test.cpp Sample distributed locked task Retry
39
/// ## Example without retrying
40
/// @snippet dist_lock/dist_lock_test.cpp Sample distributed locked task SingleAttempt
41
///
42
/// @see @ref scripts/docs/en/userver/periodics.md
43
/// @see AlwaysBusyDistLockStrategy
44
class
DistLockedTask
final
:
public
engine::
TaskBase
{
45
public
:
46
using
WorkerFunc = std::function<
void
()>;
47
48
/// Default constructor.
49
/// Creates an invalid task.
50
DistLockedTask
() =
default
;
51
52
DistLockedTask(DistLockedTask&&) =
delete
;
53
DistLockedTask& operator=(DistLockedTask&&) =
delete
;
54
55
DistLockedTask(
const
DistLockedTask&) =
delete
;
56
DistLockedTask& operator=(
const
DistLockedTask&&) =
delete
;
57
58
~DistLockedTask();
59
60
/// Creates a DistLockedTask.
61
/// @param name name of the task
62
/// @param worker_func a callback that is started once we've acquired the lock
63
/// and is cancelled when the lock is lost.
64
/// @param settings distributed lock settings
65
/// @param strategy distributed locking strategy
66
/// @param mode distributed lock waiting mode
67
/// @param retry_mode run task continuously or once (needed mainly for tests)
68
/// @param base_log_level logging level settings. WARNING and ERROR levels are not overridden
69
/// @note `worker_func` must honour task cancellation and stop ASAP when
70
/// it is cancelled, otherwise brain split is possible (IOW, two different
71
/// users do work assuming both of them hold the lock, which is not true).
72
DistLockedTask
(
73
std::string name,
74
WorkerFunc worker_func,
75
std::shared_ptr<
DistLockStrategyBase
> strategy,
76
const
DistLockSettings
& settings = {},
77
DistLockWaitingMode
mode =
DistLockWaitingMode
::
kWait
,
78
DistLockRetryMode
retry_mode =
DistLockRetryMode
::
kRetry
,
79
logging::
Level
base_log_level = logging
::
Level
::
kInfo
80
);
81
82
/// Creates a DistLockedTask to be run in a specific engine::TaskProcessor
83
DistLockedTask
(
84
engine::TaskProcessor& task_processor,
85
std::string name,
86
WorkerFunc worker_func,
87
std::shared_ptr<
DistLockStrategyBase
> strategy,
88
const
DistLockSettings
& settings = {},
89
DistLockWaitingMode
mode =
DistLockWaitingMode
::
kWait
,
90
DistLockRetryMode
retry_mode =
DistLockRetryMode
::
kRetry
,
91
logging::
Level
base_log_level = logging
::
Level
::
kInfo
92
);
93
94
/// Returns for how long the lock is held (if held at all). Returned value
95
/// may be less than the real duration.
96
std::optional<std::chrono::steady_clock::duration>
GetLockedDuration
()
const
;
97
98
void
Get()
noexcept
(
false
);
99
100
private
:
101
DistLockedTask(engine::TaskProcessor&, std::shared_ptr<impl::Locker>,
DistLockWaitingMode
);
102
103
std::shared_ptr<impl::Locker> locker_ptr_;
104
};
105
106
}
// namespace dist_lock
107
108
USERVER_NAMESPACE_END
userver
dist_lock
dist_locked_task.hpp
Generated on
for userver by
Doxygen
1.17.0