#pragma once #include #include #include #include #include #include #include #include #include #include namespace Void::Scheduler { struct SchedulerOptions { std::string owner_id{"scheduler-default"}; std::shared_ptr store{}; const Void::Time::MonotonicClock* monotonic_clock{ &Void::Time::SystemMonotonicClock::instance()}; const Void::Time::CivilClock* civil_clock{ &Void::Time::SystemCivilClock::instance()}; TimezoneRegistry timezones{}; SchedulerLimits limits{}; RandomU64Fn rng{}; ObservationHook observer{}; }; /// Pure, thread-free timer/calendar job scheduler with pluggable durable /// persistence (`InMemoryJobStore` or `SchedulerStorage::StorageJobStore`). class Scheduler final { public: [[nodiscard]] static Result create(SchedulerOptions options = {}); Scheduler(Scheduler&&) noexcept; Scheduler& operator=(Scheduler&&) noexcept; Scheduler(const Scheduler&) = delete; Scheduler& operator=(const Scheduler&) = delete; ~Scheduler() noexcept; [[nodiscard]] Result register_handler(std::string handler_id, HandlerFn handler); [[nodiscard]] Result add_job(JobSpec spec, Void::Async::OperationOptions options = {}); /// Increment revision and atomically record an old-revision /// invalidation watermark. Failed updates leave prior policy intact. [[nodiscard]] Result update_job(JobSpec spec, Void::Async::OperationOptions options = {}); /// Write a job tombstone, stop new admission, and signal stop to /// active handlers for the job. [[nodiscard]] Result remove_job(std::string_view job_id, Void::Async::OperationOptions options = {}); /// Run one bounded scheduling pass: materialize due occurrences /// under misfire/catch-up/retention limits and dispatch ready /// occurrences outside scheduler/store locks. Returns count of /// occurrences executed in this pass. [[nodiscard]] Result tick(Void::Async::OperationOptions options = {}); /// Execute at most one ready occurrence from the monotonic min-heap /// outside scheduler/store locks. [[nodiscard]] Result dispatch_one(Void::Async::OperationOptions options = {}); /// Stop materialization/dispatch, signal stop to active handlers, /// and wait up to `options.deadline` for live handlers to exit. /// Never erases running records or abandons active frames. [[nodiscard]] Result close_and_join(Void::Async::OperationOptions options = {}); [[nodiscard]] Result> list_jobs() const; [[nodiscard]] Result> list_occurrences() const; [[nodiscard]] Result acknowledge_dead(const OccurrenceId& id); [[nodiscard]] Result prune_completed(std::size_t max_count = 256); [[nodiscard]] Result reclaim_invalidated(std::size_t max_count = 256); [[nodiscard]] Result stats() const; private: struct State; explicit Scheduler(std::shared_ptr state) noexcept; std::shared_ptr m_state; }; } // namespace Void::Scheduler