#pragma once #include #include #include #include #include namespace ReloadableHost { enum class WorkerState : std::uint8_t { Stopped, Running, Restarting, Failed }; /// Effects already handed to a crashed worker cannot be proven absent. enum class EffectState : std::uint8_t { None, InProgress, /// The worker died (or was terminated) with an effect in progress. /// The host must reconcile with the provider; it must never assume /// the effect was rolled back. Unknown, Completed }; struct RestartPolicy { std::uint32_t max_restarts{3}; std::chrono::milliseconds base_backoff{50}; std::chrono::milliseconds max_backoff{1000}; }; struct WorkerExit { bool exited{false}; int code{-1}; int signal{0}; [[nodiscard]] bool clean() const noexcept { return exited && code == 0 && signal == 0; } }; struct SupervisorReport { WorkerState state{WorkerState::Stopped}; EffectState effects{EffectState::None}; std::uint32_t restarts{0}; /// Advisory delay before the next restart attempt; the caller /// owns the clock so supervision stays deterministic. std::chrono::milliseconds next_backoff{0}; std::string last_error; }; /// Spawn/observe/terminate boundary for one out-of-process provider. /// Implementations own exactly one worker at a time. class WorkerProcessPort { public: WorkerProcessPort() = default; WorkerProcessPort(const WorkerProcessPort&) = delete; WorkerProcessPort& operator=(const WorkerProcessPort&) = delete; virtual ~WorkerProcessPort() = default; [[nodiscard]] virtual Void::Result::Result spawn(const std::string& command) = 0; /// Non-blocking observation. Empty optional while still running. [[nodiscard]] virtual Void::Result::Result< std::optional> try_wait() = 0; virtual void terminate(std::chrono::milliseconds grace) noexcept = 0; [[nodiscard]] virtual bool running() const noexcept = 0; }; /// Supervises one out-of-process provider with an explicit restart policy. /// /// Restarts are attempted only by the next `poll()` after a crash, so the /// backoff is reported rather than slept. A crash with an in-progress /// effect reports `EffectState::Unknown`: this composition makes no /// exactly-once or distributed claim. class WorkerSupervisor { public: WorkerSupervisor(WorkerProcessPort& port, std::string command, RestartPolicy policy = {}); [[nodiscard]] Void::Result::Result start(); /// Mark one effect as handed to the worker, and its confirmed /// completion. void begin_effect() noexcept; void complete_effect() noexcept; /// Observe one exit (non-blocking) and apply the restart policy. /// The next poll after a crash respawns the worker. [[nodiscard]] Void::Result::Result poll(); /// Terminate a running worker with a bounded grace. Never claims an /// in-progress effect was rolled back. void stop(std::chrono::milliseconds grace = std::chrono::milliseconds{ 2000}) noexcept; [[nodiscard]] SupervisorReport report() const; private: [[nodiscard]] std::chrono::milliseconds backoff_for( std::uint32_t restarts) const noexcept; WorkerProcessPort& m_port; std::string m_command; RestartPolicy m_policy; SupervisorReport m_report{}; }; } // namespace ReloadableHost