userver: userver/components/component_base.hpp Source File
Loading...
Searching...
No Matches
component_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/components/component_base.hpp
4/// @brief Contains components::ComponentBase declaration and forward
5/// declarations of components::ComponentConfig and
6/// components::ComponentContext, function components::GetCurrentComponentName()
7
8#include <userver/components/component_fwd.hpp>
9#include <userver/components/raw_component_base.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace components {
14
15/// @ingroup userver_base_classes
16///
17/// @brief Base class for all @ref userver_components "application components",
18/// it depends on components::Logger and components::Tracer.
19class ComponentBase : public RawComponentBase {
20public:
21 ComponentBase(const ComponentConfig&, const ComponentContext&);
22
23 ComponentBase(ComponentBase&&) = delete;
24 ComponentBase(const ComponentBase&) = delete;
25
26 /// It is a good place to stop your work here.
27 /// All components dependent on the current component were destroyed.
28 /// All components on which the current component depends are still alive.
29 ~ComponentBase() override = default;
30
31 /// Override this function to inform the world of the state of your
32 /// component.
33 ///
34 /// @warning The function is called concurrently from multiple threads.
35 ComponentHealth GetComponentHealth() const override { return ComponentHealth::kOk; }
36
37 /// Called once if the creation of any other component failed.
38 /// If the current component expects some other component to take any action
39 /// with the current component, this call is a signal that such action may
40 /// never happen due to components loading was cancelled.
41 /// Application components might not want to override it.
42 void OnLoadingCancelled() override {}
43
44 /// Component may use this function to finalize registration of other
45 /// components that depend on it (for example, handler components register
46 /// in server component, and the latter uses OnAllComponentsLoaded() to start
47 /// processing requests).
48 ///
49 /// Base components may override it and make `final` to do some work after the
50 /// derived object constructor is called. Don't use it otherwise.
51 void OnAllComponentsLoaded() override {}
52
53 /// Component may use this function to stop doing work before the stop of the
54 /// components that depend on it.
55 ///
56 /// Base components may override it and make `final` to do some work before
57 /// the derived object constructor is called. Don't use it otherwise.
58 void OnAllComponentsAreStopping() override {}
59
60 /// If the component pulls more options from @ref ComponentConfig than its
61 /// parent component, then it needs to define `GetStaticConfigSchema` method
62 /// and add its own options to the parent component's options.
63 static yaml_config::Schema GetStaticConfigSchema();
64
65protected:
66 /// Legacy alias, use @ref ComponentBase instead.
67 using LoggableComponentBase = ComponentBase;
68};
69
70/// Deprecated, use @ref ComponentBase instead.
71using LoggableComponentBase = ComponentBase;
72
73} // namespace components
74
75USERVER_NAMESPACE_END