Github   Telegram
Loading...
Searching...
No Matches
manager.hpp
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <shared_mutex>
6#include <string>
7#include <type_traits>
8#include <unordered_map>
9#include <vector>
10
11#include <userver/components/component_context.hpp>
12#include <userver/components/component_fwd.hpp>
13#include <userver/components/impl/component_base.hpp>
14#include <userver/engine/task/task_processor_fwd.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace engine::impl {
19class TaskProcessorPools;
20} // namespace engine::impl
21
22namespace os_signals {
23class ProcessorComponent;
24} // namespace os_signals
25
26namespace components {
27
28class ComponentList;
29struct ManagerConfig;
30
33
34class Manager final {
35 public:
36 using TaskProcessorsMap =
37 std::unordered_map<std::string, std::unique_ptr<engine::TaskProcessor>>;
38
39 Manager(std::unique_ptr<ManagerConfig>&& config,
40 const ComponentList& component_list);
41 ~Manager();
42
43 const ManagerConfig& GetConfig() const;
44 const std::shared_ptr<engine::impl::TaskProcessorPools>&
45 GetTaskProcessorPools() const;
46 const TaskProcessorsMap& GetTaskProcessorsMap() const;
47
48 template <typename Component>
49 void AddComponent(const components::ComponentConfigMap& config_map,
50 const std::string& name) {
51 static_assert(std::is_base_of_v<impl::ComponentBase, Component>);
52 AddComponentImpl(config_map, name,
53 [](const components::ComponentConfig& config,
54 const components::ComponentContext& context) {
55 return std::make_unique<Component>(config, context);
56 });
57 }
58
59 void OnSignal(int signum);
60
61 std::chrono::steady_clock::time_point GetStartTime() const;
62
63 std::chrono::milliseconds GetLoadDuration() const;
64
65 private:
66 class TaskProcessorsStorage {
67 public:
68 explicit TaskProcessorsStorage(
69 std::shared_ptr<engine::impl::TaskProcessorPools> task_processor_pools);
70 ~TaskProcessorsStorage();
71
72 void Reset() noexcept;
73
74 using Map = TaskProcessorsMap;
75
76 const Map& GetMap() const { return task_processors_map_; }
77 const std::shared_ptr<engine::impl::TaskProcessorPools>&
78 GetTaskProcessorPools() const {
79 return task_processor_pools_;
80 }
81
82 void Add(std::string name,
83 std::unique_ptr<engine::TaskProcessor>&& task_processor);
84
85 private:
86 std::shared_ptr<engine::impl::TaskProcessorPools> task_processor_pools_;
87 Map task_processors_map_;
88 };
89
90 void CreateComponentContext(const ComponentList& component_list);
91 void AddComponents(const ComponentList& component_list);
92 void AddComponentImpl(
93 const components::ComponentConfigMap& config_map, const std::string& name,
94 std::function<std::unique_ptr<components::impl::ComponentBase>(
95 const components::ComponentConfig&,
96 const components::ComponentContext&)>
97 factory);
98 void ClearComponents() noexcept;
99 components::ComponentConfigMap MakeComponentConfigMap(
100 const ComponentList& component_list);
101
102 std::unique_ptr<const ManagerConfig> config_;
103 std::vector<ComponentConfig> empty_configs_;
104 TaskProcessorsStorage task_processors_storage_;
105
106 mutable std::shared_timed_mutex context_mutex_;
107 components::ComponentContext component_context_;
108 bool components_cleared_{false};
109
110 engine::TaskProcessor* default_task_processor_{nullptr};
111 const std::chrono::steady_clock::time_point start_time_;
112 std::chrono::milliseconds load_duration_{0};
113
114 os_signals::ProcessorComponent* signal_processor_{nullptr};
115};
116
117} // namespace components
118
119USERVER_NAMESPACE_END