#pragma once #include #include #include #include #include #include namespace lm { class Database; class MissionQueue { public: explicit MissionQueue(Database& db); ~MissionQueue(); MissionQueue(const MissionQueue&) = delete; MissionQueue& operator=(const MissionQueue&) = delete; nlohmann::json list() const; nlohmann::json runnerStatus() const; std::optional enqueue(const nlohmann::json& payload, std::string& err); bool removeById(const std::string& id, std::string& err); bool clearAll(std::string& err); bool reorder(const nlohmann::json& ordered_ids, std::string& err); bool pause(std::string& err); bool resume(std::string& err); bool cancel(std::string& err); private: enum class LoopControl { None, Break, Continue }; Database& db_; mutable std::recursive_mutex mu_; nlohmann::json queue_; nlohmann::json runner_; std::thread worker_; std::atomic stop_{false}; std::atomic wake_{false}; std::atomic paused_{false}; std::atomic cancel_{false}; void load(); void saveUnlocked() const; void ensureRunnerDefaults(); void startWorkerIfNeeded(); void workerLoop(); void runMissionActions(nlohmann::json& entry); LoopControl executeActionsUnlocked(const nlohmann::json& actions, const nlohmann::json& parameters, nlohmann::json& log, int loop_depth); void sleepMs(int ms); void setRunnerState(const std::string& state, const std::string& message = ""); void insertByPriorityUnlocked(nlohmann::json& entry); }; } // namespace lm