| #ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_STATIC_STATE_H_ |
| #define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_STATIC_STATE_H_ |
| |
| #include <memory> |
| #include <string> |
| |
| #include "safepower_agent.pb.h" |
| #include "state_updater.h" |
| #include "absl/functional/any_invocable.h" |
| #include "absl/status/statusor.h" |
| #include "absl/strings/string_view.h" |
| #include "absl/time/time.h" |
| |
| namespace safepower_agent { |
| |
| struct StaticStateLoaderConfig { |
| absl::Duration retry_interval = absl::Seconds(1); |
| absl::Duration timeout = absl::InfiniteDuration(); |
| int max_retries = -1; |
| }; |
| |
| class StaticStateLoader |
| : public std::enable_shared_from_this<StaticStateLoader> { |
| private: |
| struct PrivateToken { |
| explicit PrivateToken() = default; |
| }; |
| |
| public: |
| using GetStateCallback = absl::AnyInvocable<void( |
| absl::StatusOr<safepower_agent_proto::SystemState>)>; |
| |
| static std::shared_ptr<StaticStateLoader> Create( |
| absl::string_view name, |
| absl::AnyInvocable<absl::StatusOr<safepower_agent_proto::SystemState>()> |
| get_state, |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| state_updater, |
| const StaticStateLoaderConfig& config = {}); |
| |
| static std::shared_ptr<StaticStateLoader> Create( |
| absl::string_view name, |
| absl::AnyInvocable<void(GetStateCallback)> get_state, |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| state_updater, |
| const StaticStateLoaderConfig& config = {}); |
| |
| StaticStateLoader( |
| PrivateToken token, absl::string_view name, |
| absl::AnyInvocable<absl::StatusOr<safepower_agent_proto::SystemState>()> |
| get_state, |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| state_updater, |
| const StaticStateLoaderConfig& config = {}); |
| StaticStateLoader( |
| PrivateToken token, absl::string_view name, |
| absl::AnyInvocable<void(GetStateCallback)> get_state, |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| state_updater, |
| const StaticStateLoaderConfig& config = {}); |
| |
| ~StaticStateLoader(); |
| |
| private: |
| void Start(); |
| |
| private: |
| void TryLoadState(); |
| |
| std::string name_; |
| absl::AnyInvocable<void(GetStateCallback)> get_state_; |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| state_updater_; |
| absl::Duration retry_interval_; |
| absl::Time timeout_time_; |
| int retries_left_ = 0; |
| }; |
| |
| } // namespace safepower_agent |
| |
| #endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_STATIC_STATE_H_ |