Files
App/src/mission/mission_queue.hpp
HiepLM 098e1b2b69
Some checks failed
Test / test (push) Has been cancelled
Chuyển lưu trữ dữ liệu sang data base
2026-06-17 11:16:30 +07:00

65 lines
1.7 KiB
C++

#pragma once
#include <nlohmann/json.hpp>
#include <atomic>
#include <mutex>
#include <optional>
#include <string>
#include <thread>
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<nlohmann::json> 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<bool> stop_{false};
std::atomic<bool> wake_{false};
std::atomic<bool> paused_{false};
std::atomic<bool> 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