Files
Library/include/Void/Fsm/Orchestrator.hpp
T
faumarayandClaude Opus 4.8 80293ee56f Add async/resilience/reflection primitives + FSM v2 rewrite
A round of new universal capabilities (all generic — templated/reflection
based, no app coupling), verified by the test suite (1103 passing):

Data reflection:
- Reflect.hpp now (de)serializes chrono::duration, chrono::time_point,
  std::variant (tagged), std::array, and non-string-keyed std::map;
  Meta.hpp gains is_time_point_v et al.

Async:
- EventBus gains Mode::Async (worker-thread dispatch + flush()).
- ThreadPool gains a bounded queue, try_submit(), and rejection stats.

New modules / containers:
- Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker,
  ResourceMonitor.
- Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V>
  (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats).
- System: process/CPU/memory metrics (Metrics.hpp) and a streaming
  Process API.

Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions,
history states, deferred events), Configuration/Logging reflection
helpers, and the accompanying test suites.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 15:45:16 +03:00

173 lines
4.1 KiB
C++

#pragma once
#include <Void/Fsm/StateKey.hpp>
#include <Void/Service/Registry.hpp>
#include <cstddef>
#include <memory>
#include <mutex>
#include <utility>
#include <vector>
namespace Void::Fsm
{
namespace Detail
{
// Type-erased supervised FSM. Models wrap a concrete Machine<R>
// subclass; the orchestrator only needs lifecycle + status.
class FsmConcept
{
public:
virtual ~FsmConcept() = default;
virtual void stop() = 0;
[[nodiscard]] virtual Status status() const = 0;
};
template <typename F> class FsmModel final : public FsmConcept
{
public:
explicit FsmModel(std::unique_ptr<F> fsm)
: m_fsm(std::move(fsm))
{
}
void stop() override { m_fsm->stop(); }
[[nodiscard]] Status status() const override
{
return m_fsm->status();
}
[[nodiscard]] F& get() noexcept { return *m_fsm; }
private:
std::unique_ptr<F> m_fsm;
};
} // namespace Detail
/// A non-owning supervision handle to a launched FSM: query status and
/// stop it. The Orchestrator owns the FSM's lifetime.
class FsmHandle
{
public:
FsmHandle() = default;
void stop() const
{
if (m_impl) {
m_impl->stop();
}
}
[[nodiscard]] Status status() const
{
return m_impl ? m_impl->status() : Status::Idle;
}
[[nodiscard]] bool finished() const
{
const auto s = status();
return s == Status::Succeeded || s == Status::Failed;
}
[[nodiscard]] explicit operator bool() const noexcept
{
return m_impl != nullptr;
}
private:
friend class Orchestrator;
explicit FsmHandle(std::shared_ptr<Detail::FsmConcept> impl)
: m_impl(std::move(impl))
{
}
std::shared_ptr<Detail::FsmConcept> m_impl;
};
// ── Orchestrator ──────────────────────────────────────────────────────
// Owns a ServiceRegistry and supervises a set of FSM instances. Each
// launched FSM is injected with the registry as its context, so its
// states resolve dependencies by interface (no concrete coupling), and
// is started immediately. stop_all() winds everything down.
class Orchestrator
{
public:
Orchestrator() = default;
~Orchestrator() { stop_all(); }
Orchestrator(const Orchestrator&) = delete;
Orchestrator& operator=(const Orchestrator&) = delete;
[[nodiscard]] Service::ServiceRegistry& services() noexcept
{
return m_registry;
}
/// Call on_start() on lifecycle services. Call before launching
/// FSMs that expect started services.
void start_services()
{
m_registry.start_all();
m_services_started = true;
m_services_stopped = false;
}
/// Construct F, inject the registry, start it, and supervise.
/// F must derive Fsm::Machine<...> (provides set_services /
/// start / stop / status).
template <typename F, typename... Args>
FsmHandle launch(Args&&... args)
{
auto fsm = std::make_unique<F>(std::forward<Args>(args)...);
fsm->set_services(m_registry);
fsm->start();
auto model =
std::make_shared<Detail::FsmModel<F>>(std::move(fsm));
std::lock_guard lock{m_mutex};
m_running.push_back(model);
return FsmHandle{model};
}
/// Stop and release every supervised FSM, then stop services.
void stop_all()
{
std::vector<std::shared_ptr<Detail::FsmConcept>> running;
{
std::lock_guard lock{m_mutex};
running.swap(m_running);
}
for (auto& m : running) {
m->stop();
}
running.clear();
// Balance start_services() exactly once (stop_all may run
// explicitly and again from the destructor).
if (m_services_started && !m_services_stopped) {
m_registry.stop_all();
m_services_stopped = true;
}
}
[[nodiscard]] std::size_t running_count() const
{
std::lock_guard lock{m_mutex};
return m_running.size();
}
private:
Service::ServiceRegistry m_registry;
mutable std::mutex m_mutex;
std::vector<std::shared_ptr<Detail::FsmConcept>> m_running;
bool m_services_started{false};
bool m_services_stopped{false};
};
} // namespace Void::Fsm