userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
periodic_task.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <chrono>
7
#include <functional>
8
#include <optional>
9
#include <string>
10
#include <string_view>
11
12
#include <userver/compiler/impl/lifetime.hpp>
13
#include <
userver/logging/level.hpp
>
14
#include <
userver/utils/assert.hpp
>
15
#include <
userver/utils/fast_pimpl.hpp
>
16
#include <
userver/utils/flags.hpp
>
17
18
USERVER_NAMESPACE_BEGIN
19
20
namespace
engine
{
21
class
TaskProcessor;
22
}
// namespace engine
23
24
namespace
testsuite
{
25
class
PeriodicTaskControl
;
26
class
TestsuiteTasks
;
27
}
// namespace testsuite
28
29
namespace
utils
{
30
31
class
ResourceScopeStorage
;
32
45
class
PeriodicTask
final {
46
public
:
47
enum class
Flags
{
49
kNone
= 0,
51
kNow
= 1 << 0,
53
kStrong
= 1 << 1,
55
kChaotic
= 1 << 2,
62
kCritical
= 1 << 4,
63
};
64
66
struct
Settings final {
67
static
constexpr
uint8_t kDistributionPercent = 25;
68
69
constexpr
/*implicit*/
Settings(
70
std::chrono::milliseconds
period
,
71
utils::Flags<Flags>
flags
= {},
72
logging::Level
span_level
=
logging::Level::kInfo
73
)
74
: Settings(
period
, kDistributionPercent,
flags
,
span_level
)
75
{}
76
77
constexpr
Settings(
78
std::chrono::milliseconds
period
,
79
std::chrono::milliseconds
distribution
,
80
utils::Flags<Flags>
flags
= {},
81
logging::Level
span_level
=
logging::Level::kInfo
82
)
83
:
period
(
period
),
84
distribution
(
distribution
),
85
flags
(
flags
),
86
span_level
(
span_level
)
87
{
88
UASSERT
(
distribution
<=
period
);
89
}
90
91
constexpr
Settings(
92
std::chrono::milliseconds
period
,
93
uint8_t distribution_percent,
94
utils::Flags<Flags>
flags
= {},
95
logging::Level
span_level
=
logging::Level::kInfo
96
)
97
: Settings(
period
,
period
* distribution_percent / 100,
flags
,
span_level
)
98
{
99
UASSERT
(distribution_percent <= 100);
100
}
101
102
template
<
class
Rep,
class
Period>
103
constexpr
/*implicit*/
Settings(std::chrono::duration<Rep, Period>
period
)
104
: Settings(
period
, kDistributionPercent, {},
logging::Level::kInfo
)
105
{}
106
107
bool
operator==(
const
Settings& other)
const
noexcept
;
108
109
// Note: Tidy requires us to explicitly initialize these fields, although
110
// the initializers are never used.
111
114
std::chrono::milliseconds
period
{};
115
119
std::chrono::milliseconds
distribution
{};
120
122
std::optional<std::chrono::milliseconds>
exception_period
;
123
125
utils::Flags<Flags>
flags
{};
126
129
logging::Level
span_level
{
logging::Level::kInfo
};
130
134
engine::TaskProcessor*
task_processor
{
nullptr
};
135
};
136
138
using
Callback
= std::function<void()>;
139
141
PeriodicTask
();
142
143
PeriodicTask
(
PeriodicTask
&&) =
delete
;
144
PeriodicTask
(
const
PeriodicTask
&) =
delete
;
145
148
PeriodicTask
(std::string name,
Settings
settings,
Callback
callback);
149
153
void
Start
(std::string name,
Settings
settings,
Callback
callback);
154
155
~PeriodicTask
();
156
163
void
Stop
() noexcept;
164
166
void
SetSettings
(
Settings
settings);
167
183
void
ForceStepAsync
();
184
194
bool
SynchronizeDebug
(
bool
preserve_span = false);
195
199
void
SuspendDebug
();
200
203
void
ResumeDebug
();
204
207
bool
IsRunning
() const;
208
211
void
RegisterInTestsuite
(USERVER_NAMESPACE::
testsuite
::PeriodicTaskControl& periodic_task_control);
212
214
Settings
GetCurrentSettings
() const;
215
218
std
::string_view
GetName
() const USERVER_IMPL_LIFETIME_BOUND;
219
220
private:
221
friend
void
StartPeriodicTask
(
222
ResourceScopeStorage
& scopes,
223
testsuite
::TestsuiteTasks& testsuite_tasks,
224
PeriodicTask
& periodic_task,
225
std
::
string
name,
226
const
Settings
& settings,
227
Callback
callback
228
);
229
230
void
PrepareScopedStart(
std
::
string
name, const
Settings
& settings);
231
232
void
StartPrepared(
Callback
callback);
233
234
class Impl;
235
constexpr static
std
::
size_t
kSize = 448;
236
constexpr static
std
::
size_t
kAlignment = 16;
237
utils
::
FastPimpl
<Impl, kSize, kAlignment> impl_;
238
};
239
250
void
StartPeriodicTask
(
251
PeriodicTask
& periodic_task,
252
std
::
string
name,
253
const
PeriodicTask
::Settings& settings,
254
PeriodicTask
::
Callback
callback,
255
testsuite
::TestsuiteTasks& testsuite_tasks
256
);
257
277
void
StartPeriodicTask
(
278
ResourceScopeStorage
& scopes,
279
testsuite
::TestsuiteTasks& testsuite_tasks,
280
PeriodicTask
& periodic_task,
281
std
::
string
name,
282
const
PeriodicTask
::Settings& settings,
283
PeriodicTask
::
Callback
callback
284
);
285
286
}
// namespace utils
287
288
USERVER_NAMESPACE_END
userver
utils
periodic_task.hpp
Generated on
for userver by
Doxygen
1.17.0