Files
Library/include/Void/Scheduler/Scheduler.hpp
T

101 lines
3.3 KiB
C++

#pragma once
#include <Void/Scheduler/Calendar.hpp>
#include <Void/Scheduler/Store.hpp>
#include <Void/Scheduler/Types.hpp>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Scheduler
{
struct SchedulerOptions {
std::string owner_id{"scheduler-default"};
std::shared_ptr<JobStore> 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<Scheduler>
create(SchedulerOptions options = {});
Scheduler(Scheduler&&) noexcept;
Scheduler& operator=(Scheduler&&) noexcept;
Scheduler(const Scheduler&) = delete;
Scheduler& operator=(const Scheduler&) = delete;
~Scheduler() noexcept;
[[nodiscard]] Result<void> register_handler(std::string handler_id,
HandlerFn handler);
[[nodiscard]] Result<std::uint64_t>
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<std::uint64_t>
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<void>
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<std::size_t>
tick(Void::Async::OperationOptions options = {});
/// Execute at most one ready occurrence from the monotonic min-heap
/// outside scheduler/store locks.
[[nodiscard]] Result<bool>
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<ShutdownReport>
close_and_join(Void::Async::OperationOptions options = {});
[[nodiscard]] Result<std::vector<JobRecord>> list_jobs() const;
[[nodiscard]] Result<std::vector<OccurrenceRecord>>
list_occurrences() const;
[[nodiscard]] Result<void>
acknowledge_dead(const OccurrenceId& id);
[[nodiscard]] Result<std::size_t>
prune_completed(std::size_t max_count = 256);
[[nodiscard]] Result<std::size_t>
reclaim_invalidated(std::size_t max_count = 256);
[[nodiscard]] Result<StoreStats> stats() const;
private:
struct State;
explicit Scheduler(std::shared_ptr<State> state) noexcept;
std::shared_ptr<State> m_state;
};
} // namespace Void::Scheduler