blob: 75e90e0ce17eb39c21364db1573cf5d2794f8c11 [file]
#ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_SERVICE_IMPL_H_
#define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_SERVICE_IMPL_H_
#include <memory>
#include <ostream>
#include <utility>
#include "action_context.h"
#include "event_queue.h"
#include "safepower_agent.pb.h"
#include "state_updater.h"
#include "absl/base/nullability.h"
#include "absl/base/thread_annotations.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/mutex.h"
namespace safepower_agent {
class ServiceImpl {
public:
explicit ServiceImpl(
const
std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>>&
system_state_updater,
const
std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>>&
global_system_state_updater,
std::unique_ptr<ActionContextManager> action_context_manager)
: system_state_updater_(system_state_updater),
global_system_state_updater_(global_system_state_updater),
action_context_manager_(std::move(action_context_manager)) {}
absl::Status StartAction(
const safepower_agent_proto::StartActionRequest& request);
absl::StatusOr<
std::shared_ptr<StateUpdater<safepower_agent_proto::ActionStateLog>>>
MonitorAction(const safepower_agent_proto::MonitorActionRequest& request);
absl::StatusOr<
std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>>>
MonitorState();
void GetSupportedActions(
safepower_agent_proto::GetSupportedActionsResponse& response);
absl::Status GetFeatures(
safepower_agent_proto::GetFeaturesResponse& response);
absl::Status SystemStateUpdate(
const safepower_agent_proto::SystemState& request);
// Dumps the internal state of the SafepowerLocalAgentImpl to the provided
// stream.
void DumpSafepowerState(std::ostream& os) const;
// Runs the event loop. Blocks until Stop() is called.
void Run();
// Initiates graceful shutdown of the event loop, transitioning the service
// into a shutting down state. The blocking Run() loop is expected to stop
// once all active work is drained.
void Stop();
private:
absl::Status CheckDisruption() const;
std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>>
system_state_updater_;
std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>>
global_system_state_updater_;
std::unique_ptr<ActionContextManager> action_context_manager_;
enum class State { kUnstarted, kRunning, kStopping, kStopped };
mutable absl::Mutex mutex_;
State state_ ABSL_GUARDED_BY(mutex_) = State::kUnstarted;
EventQueue event_queue_;
};
} // namespace safepower_agent
#endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_SERVICE_IMPL_H_