#pragma once #include #include #include #include #include #include #include #include namespace vls::scheduler { enum class Priority { artifact = 0, background = 1, foreground = 2 }; struct Budget { std::size_t foreground{4}; std::size_t background{2}; std::size_t artifact{1}; std::size_t queued{64}; std::size_t memory_mib{1024}; std::chrono::milliseconds watchdog{30000}; [[nodiscard]] bool valid() const noexcept; }; enum class Outcome { completed, failed, cancelled, timed_out }; struct Estimate { Priority priority{Priority::background}; std::string key; std::uint64_t revision{}; std::size_t memory_mib{}; }; struct Result { std::string key; std::uint64_t revision{}; Outcome outcome{Outcome::failed}; std::string detail; }; struct Metrics { std::uint64_t accepted{}, completed{}, failed{}, cancelled{}, timed_out{}, rejected{}; std::size_t queued_peak{}, running_peak{}, running_memory_peak{}; std::uint64_t queue_nanoseconds{}, run_nanoseconds{}; }; // Work returns a detail string on success or throws on failure. It must honor // the stop token so cancellation and the running watchdog stay bounded. using Work = std::function; using Completion = std::function; // Bounded, priority-separated executor. Foreground, background and artifact // pools have reserved capacity, so a saturated background queue never delays a // foreground request. Every accepted submission receives exactly one terminal // completion; a refused submission receives none and returns false. class Scheduler { public: explicit Scheduler(Budget = Budget{}); ~Scheduler(); Scheduler(const Scheduler &) = delete; Scheduler &operator=(const Scheduler &) = delete; Scheduler(Scheduler &&) = delete; Scheduler &operator=(Scheduler &&) = delete; // A newer revision of the same key supersedes queued work immediately and // requests cooperative cancellation of running work. [[nodiscard]] bool submit(Estimate, Work, Completion); void cancel(std::string_view key); void cancel_all(); // Waits until every accepted submission has reached a terminal result. void drain(); [[nodiscard]] Metrics metrics() const; [[nodiscard]] const Budget &budget() const noexcept; private: struct State; std::unique_ptr state_; }; } // namespace vls::scheduler