userver: userver/alerts/storage.hpp Source File
Loading...
Searching...
No Matches
storage.hpp
1#pragma once
2
3/// @file userver/alert/storage.hpp
4/// @brief @copybrief alert::Storage
5
6#include <chrono>
7#include <string>
8#include <vector>
9
10#include <userver/concurrent/variable.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace alerts {
15
16struct Alert final {
17 std::string id;
18 std::string description;
19 std::chrono::steady_clock::time_point stop_timepoint;
20};
21
22inline constexpr auto kDefaultDuration = std::chrono::seconds{120};
23inline constexpr auto kInfinity = std::chrono::hours{24 * 999};
24
25/// @ingroup userver_clients
26///
27/// @brief Storage for active fired alerts.
28class Storage final {
29public:
30 Storage() = default;
31 Storage(const Storage&) = delete;
32 Storage(Storage&&) = delete;
33
34 /// Fire an alert. It will be stopped either after `StopAlertNow` for the
35 /// `alert_id` is called or after `duration` seconds.
36 void FireAlert(
37 std::string_view alert_id,
38 std::string_view description,
39 std::chrono::seconds duration = kDefaultDuration
40 ) noexcept;
41
42 /// Stop an alert before its duration has ended.
43 void StopAlertNow(std::string_view alert_id) noexcept;
44
45 /// Collect fired and active alerts.
47
48private:
49 static void DoStopAlertNow(std::string_view alert_id, std::vector<Alert>& alerts);
50
51 concurrent::Variable<std::vector<Alert>> alerts_;
52};
53
54} // namespace alerts
55
56USERVER_NAMESPACE_END