userver: userver/utils/periodic_task.hpp Source File
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>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace engine {
21class TaskProcessor;
22} // namespace engine
23
24namespace testsuite {
26class TestsuiteTasks;
27} // namespace testsuite
28
29namespace utils {
30
32
45class PeriodicTask final {
46public:
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,
73 )
74 : Settings(period, kDistributionPercent, flags, span_level)
75 {}
76
77 constexpr Settings(
78 std::chrono::milliseconds period,
79 std::chrono::milliseconds distribution,
82 )
83 : period(period),
85 flags(flags),
87 {
89 }
90
91 constexpr Settings(
92 std::chrono::milliseconds period,
93 uint8_t distribution_percent,
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
126
130
134 engine::TaskProcessor* task_processor{nullptr};
135 };
136
138 using Callback = std::function<void()>;
139
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
156
163 void Stop() noexcept;
164
166 void SetSettings(Settings settings);
167
184
194 bool SynchronizeDebug(bool preserve_span = false);
195
200
204
207 bool IsRunning() const;
208
211 void RegisterInTestsuite(USERVER_NAMESPACE::testsuite::PeriodicTaskControl& periodic_task_control);
212
215
218 std::string_view GetName() const USERVER_IMPL_LIFETIME_BOUND;
219
220private:
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
251 PeriodicTask& periodic_task,
252 std::string name,
253 const PeriodicTask::Settings& settings,
254 PeriodicTask::Callback callback,
255 testsuite::TestsuiteTasks& testsuite_tasks
256);
257
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
288USERVER_NAMESPACE_END