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 {
29 public:
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(std::string_view alert_id, std::string_view description,
37 std::chrono::seconds duration = kDefaultDuration) noexcept;
38
39 /// Stop an alert before its duration has ended.
40 void StopAlertNow(std::string_view alert_id) noexcept;
41
42 /// Collect fired and active alerts.
44
45 private:
46 static void DoStopAlertNow(std::string_view alert_id,
47 std::vector<Alert>& alerts);
48
49 concurrent::Variable<std::vector<Alert>> alerts_;
50};
51
52} // namespace alerts
53
54USERVER_NAMESPACE_END