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