userver: userver/components/state.hpp Source File
Loading...
Searching...
No Matches
state.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/components/state.hpp
4/// @brief @copybrief components::State
5
6#include <string_view>
7#include <unordered_set>
8#include <vector>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace components {
13
14class ComponentContext;
15
16enum class ComponentHealth;
17
18namespace impl {
19class ComponentContextImpl;
20}
21
22// clang-format off
23/// @brief All components pass through these stages during the service lifetime.
24/// @see @ref scripts/docs/en/userver/component_system.md
25///
26/// @dot
27/// digraph ServiceLifetimeStages {
28/// node [shape=record];
29///
30/// kLoading [label="{kLoading | <f0> * Components are constructed }"];
31/// kOnAllComponentsLoadedIsRunning [label="{kOnAllComponentsLoadedIsRunning | <f1> * OnAllComponentsLoaded is called }"];
32/// kRunning [label="{kRunning | <f2> * All components loaded successfully \n * Service is fully operational }"];
33/// kGracefulShutdown [label="{kGracefulShutdown | <f3> * First, waits graceful_shutdown_continue_accepting_requests_interval. Then OnGracefulShutdown is called * }"];
34/// kOnAllComponentsAreStoppingIsRunning [label="{kOnAllComponentsAreStoppingIsRunning | <f4> * OnAllComponentsAreStopping is called \n * Reverse-dependency order }"];
35/// kStopping [label="{kStopping | <f5> * Components are destroyed \n * Reverse-dependency order }"];
36///
37/// kLoading -> kOnAllComponentsLoadedIsRunning;
38/// kLoading -> kOnAllComponentsAreStoppingIsRunning [label=" Exception during construction "];
39/// kOnAllComponentsLoadedIsRunning -> kRunning;
40/// kOnAllComponentsLoadedIsRunning -> kOnAllComponentsAreStoppingIsRunning [label=" OnAllComponentsLoaded throws "];
41/// kRunning -> kOnAllComponentsAreStoppingIsRunning [label=" Received SIGINT or SIGTERM when graceful shutdown is disabled "];
42/// kRunning -> kGracefulShutdown [label=" Received SIGINT or SIGTERM when graceful shutdown is enabled "];
43/// kGracefulShutdown -> kOnAllComponentsAreStoppingIsRunning;
44/// kOnAllComponentsAreStoppingIsRunning -> kStopping;
45/// }
46/// @enddot
47// clang-format on
49 /// Constructors are running for all registered components. Components can depend on each other at this stage
50 /// by calling @ref components::ComponentContext::FindComponent and friends.
51 ///
52 /// If any component throws an exception, then the service transitions
53 /// into @ref ServiceLifetimeStage::kOnAllComponentsAreStoppingIsRunning stage.
55
56 /// @ref components::ComponentBase::OnAllComponentsLoaded (noop by default) is running for all components.
57 /// This stage starts after constructors for all components have completed without an exception.
58 ///
59 /// The order of `OnAllComponentsLoaded` hooks invocations respects the order of components defined
60 /// at @ref ServiceLifetimeStage::kLoading stage.
62
63 /// This stage marks that all `OnAllComponentsLoaded` hooks (as described
64 /// in @ref ServiceLifetimeStage::kOnAllComponentsLoadedIsRunning) have completed
65 /// successfully (without an exception). At this point the service is fully running.
66 ///
67 /// This stage ends once the service receives a shutdown signal (`SIGINT` or `SIGTERM`).
69
70 /// The service performs a graceful shutdown if either `graceful_shutdown_continue_accepting_requests_interval`
71 /// or `graceful_shutdown_pending_requests_completion_interval` is non-zero.
72 /// First, it waits for `graceful_shutdown_pending_requests_completion_interval` unless the interval is zero.
73 /// Second, it stops accepting new requests and continues processing of already running requests for
74 /// `graceful_shutdown_pending_requests_completion_interval` unless the interval is zero.
75 /// Then the normal shutdown procedure continues in @ref ServiceLifetimeStage::kOnAllComponentsAreStoppingIsRunning.
76 ///
77 /// @see @ref scripts/docs/en/userver/graceful_shutdown.md
78 /// @see @ref components::ManagerControllerComponent
79 ///
80 /// Example:
81 /// @snippet core/functional_tests/graceful_shutdown/static_config.yaml graceful_shutdown_settings
83
84 /// @ref components::ComponentBase::OnAllComponentsAreStopping (noop by default) is running for all components.
85 /// This stage starts once the service has received a shutdown signal (see @ref ServiceLifetimeStage::kRunning) and
86 /// @ref ServiceLifetimeStage::kGracefulShutdown stage (if any) has completed.
87 ///
88 /// If an error occurs during service startup, then `OnAllComponentsAreStopping` runs after
89 /// @ref components::ComponentBase::OnLoadingCancelled for all constructed components.
90 ///
91 /// The order of `OnAllComponentsAreStopping` hooks invocations respects the order of components defined
92 /// at @ref ServiceLifetimeStage::kLoading stage (they run in the reverse-dependency order).
94
95 /// Destructors are running for all components. This stage starts once
96 /// @ref ServiceLifetimeStage::kOnAllComponentsAreStoppingIsRunning stage.
97 ///
98 /// If an error occurs during service startup, then destructors run
99 /// after @ref ServiceLifetimeStage::kOnAllComponentsAreStoppingIsRunning for all constructed components.
100 ///
101 /// The order of destructor invocations respects the order of components defined
102 /// at @ref ServiceLifetimeStage::kLoading stage (they run in the reverse-dependency order).
104};
105
106/// Converts a @ref components::ServiceLifetimeStage to debug string for logging.
108
109/// A view of the components' state that is usable after the components are
110/// constructed and until all the components are destroyed.
111///
112/// @see components::ComponentContext
113class State final {
114public:
115 /// Component name together with its current health.
117 std::string_view name;
118 ComponentHealth health;
119 };
120
121 explicit State(const ComponentContext& cc) noexcept;
122
123 /// @returns true if one of the components is in fatal state and can not
124 /// work. A component is in fatal state if the
125 /// components::ComponentHealth::kFatal value is returned from the overridden
126 /// components::ComponentBase::GetComponentHealth().
128
129 /// @returns all components that are not in
130 /// components::ComponentHealth::kOk state.
131 ///
132 /// Components construction should finish before any call to this function
133 /// is made. The result should not outlive the components destruction.
135
136 /// @returns the current service lifetime stage.
137 /// @see @ref components::ServiceLifetimeStage
139
140 /// @returns true if the service is being shut down gracefully.
142
143 /// @returns true if component with name `component_name` depends
144 /// (directly or transitively) on a component with name `dependency`.
145 ///
146 /// Component with name `component_name` should be loaded.
147 /// Components construction should finish before any call to this function
148 /// is made.
149 ///
150 /// Note that GetAllDependencies usually is more effective, if you are
151 /// planning multiple calls for the same component name.
152 bool HasDependencyOn(std::string_view component_name, std::string_view dependency) const;
153
154 /// @returns all the components that `component_name` depends on directly or
155 /// transitively.
156 ///
157 /// Component with name `component_name` should be loaded.
158 /// Components construction should finish before any call to this function
159 /// is made. The result should now outlive the all the components
160 /// destruction.
161 std::unordered_set<std::string_view> GetAllDependencies(std::string_view component_name) const;
162
163private:
164 const impl::ComponentContextImpl& impl_;
165};
166
167} // namespace components
168
169USERVER_NAMESPACE_END