blob: 8a6ffa524c02efbb238103c18130506e942d5b69 [file] [edit]
#ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_POLLING_STATE_H_
#define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_POLLING_STATE_H_
#include <memory>
namespace safepower_agent {
// Concrete implementations of PollingState should inherit from this class.
//
// LIFETIME & ASYNC SAFETY RULE:
// Concrete states that perform asynchronous operations (e.g. gRPC or HTTP
// calls) MUST capture a weak pointer to themselves (`weak_from_this()`) in
// their async callbacks.
//
// The PollingScheduler takes ownership of the PollingState via unique_ptr and
// immediately converts it to a shared_ptr. This ensures that the PollingState
// is managed by a shared_ptr, allowing `weak_from_this()` to function correctly
// during the lifetime of the scheduler.
//
// When the scheduler is destroyed, it releases its shared_ptr, allowing the
// state to be destroyed once all in-flight async callbacks (which should only
// hold weak pointers) are finished.
class PollingState : public std::enable_shared_from_this<PollingState> {
public:
virtual ~PollingState() = default;
// Executed periodically by the scheduler
virtual void Check() = 0;
// Called when the polling is paused
virtual void OnPause() {}
};
} // namespace safepower_agent
#endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_POLLING_STATE_H_