| #ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_CONDITION_H_ | |
| #define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_CONDITION_H_ | |
| #include <memory> | |
| #include <ostream> | |
| #include <string> | |
| #include <tuple> | |
| #include <vector> | |
| #include "action.pb.h" | |
| #include "system_state.pb.h" | |
| #include "state_updater.h" | |
| #include "absl/base/nullability.h" | |
| #include "absl/base/thread_annotations.h" | |
| #include "absl/functional/any_invocable.h" | |
| #include "absl/status/status.h" | |
| #include "absl/strings/string_view.h" | |
| #include "absl/synchronization/mutex.h" | |
| #include "absl/time/time.h" | |
| #include "google/protobuf/repeated_ptr_field.h" | |
| namespace safepower_agent { | |
| class Condition { | |
| class Listener | |
| : public StateUpdater<safepower_agent_proto::SystemState>::Listener { | |
| public: | |
| explicit Listener(Condition& condition) : condition_(condition) {} | |
| void UpdateState(const safepower_agent_proto::SystemState& current_state, | |
| const safepower_agent_proto::SystemState& update) override; | |
| void Done() override {} | |
| private: | |
| Condition& condition_; | |
| }; | |
| public: | |
| explicit Condition(const safepower_agent_proto::Condition& condition, | |
| absl::Time original_start_time); | |
| ~Condition(); | |
| // Dumps debug information about this condition to the provided stream. | |
| void DumpDebugInfo(std::ostream& os) const; | |
| using MatchList = | |
| std::vector<const safepower_agent_proto::Condition* >; | |
| static constexpr absl::Duration kTimeoutPadding = absl::Milliseconds(250); | |
| absl::Status WaitForMatch( | |
| absl::string_view unique_id, | |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> | |
| state_updater, | |
| absl::AnyInvocable<void(absl::Status, MatchList matches) &&> callback) | |
| ABSL_LOCKS_EXCLUDED(callback_mutex_); | |
| std::tuple<absl::Status, MatchList> CheckState( | |
| const safepower_agent_proto::SystemState& state); | |
| bool active() const ABSL_LOCKS_EXCLUDED(callback_mutex_); | |
| static std::tuple<absl::Status, MatchList> Matches( | |
| const safepower_agent_proto::Condition& condition, | |
| const safepower_agent_proto::SystemState& state, absl::Time start_time, | |
| absl::Time current_time); | |
| private: | |
| absl::Status TriggerCallback(absl::Status match_status, MatchList match_list) | |
| ABSL_EXCLUSIVE_LOCKS_REQUIRED(callback_mutex_); | |
| absl::Status RegisterTimeouts( | |
| const safepower_agent_proto::Condition& condition, | |
| absl::string_view job_name_prefix); | |
| absl::Status RegisterTimeouts( | |
| const google::protobuf::RepeatedPtrField<safepower_agent_proto::Condition>& | |
| conditions, | |
| absl::string_view job_name_prefix); | |
| Listener listener_; | |
| std::string complete_job_name_; | |
| const safepower_agent_proto::Condition& condition_; | |
| std::vector<std::string> pending_jobs_; | |
| absl::Time start_time_; | |
| mutable absl::Mutex callback_mutex_; | |
| absl::AnyInvocable<void(absl::Status, MatchList matches) &&> callback_ | |
| ABSL_GUARDED_BY(callback_mutex_); | |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> | |
| state_updater_; | |
| }; | |
| } // namespace safepower_agent | |
| #endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_CONDITION_H_ |