feat: persist run state to disk with recovery on startup
This commit is contained in:
@@ -2,11 +2,90 @@
|
||||
|
||||
#include "pipeline.h"
|
||||
|
||||
#include <pidir.h>
|
||||
#include <pifile.h>
|
||||
#include <pijson.h>
|
||||
#include <piprocess.h>
|
||||
#include <pisystemtime.h>
|
||||
#include <pithread.h>
|
||||
#include <pitime.h>
|
||||
|
||||
static PIString getStateFilePath(const PIString & logs_dir, const PIString & run_id) {
|
||||
return logs_dir + run_id + ".state.json";
|
||||
}
|
||||
|
||||
PIJSON PipelineRunner::stepResultToJSON(const StepResult & sr) {
|
||||
PIJSON j = PIJSON::newObject();
|
||||
j["step_index"] = sr.step_index;
|
||||
j["title"] = sr.title;
|
||||
j["status"] = runStatusToString(sr.status);
|
||||
j["returncode"] = sr.returncode;
|
||||
j["output"] = sr.output;
|
||||
j["error"] = sr.error;
|
||||
return j;
|
||||
}
|
||||
|
||||
StepResult PipelineRunner::stepResultFromJSON(const PIJSON & j) {
|
||||
StepResult sr;
|
||||
sr.step_index = j["step_index"].toInt();
|
||||
sr.title = j["title"].toString();
|
||||
PIString statusStr = j["status"].toString();
|
||||
if (statusStr == "pending")
|
||||
sr.status = RunStatus::Pending;
|
||||
else if (statusStr == "running")
|
||||
sr.status = RunStatus::Running;
|
||||
else if (statusStr == "completed")
|
||||
sr.status = RunStatus::Completed;
|
||||
else
|
||||
sr.status = RunStatus::Error;
|
||||
sr.returncode = j["returncode"].toInt();
|
||||
sr.output = j["output"].toString();
|
||||
sr.error = j["error"].toString();
|
||||
return sr;
|
||||
}
|
||||
|
||||
PIJSON PipelineRunner::runStateToJSON(const RunState & state) {
|
||||
PIJSON j = PIJSON::newObject();
|
||||
j["run_id"] = state.run_id;
|
||||
j["pipeline_id"] = state.pipeline_id;
|
||||
j["status"] = runStatusToString(state.status);
|
||||
j["current_step"] = state.current_step;
|
||||
|
||||
PIJSON stepsArr = PIJSON::newArray();
|
||||
for (int i = 0; i < state.steps.size(); ++i) {
|
||||
stepsArr << stepResultToJSON(state.steps[i]);
|
||||
}
|
||||
j["steps"] = stepsArr;
|
||||
return j;
|
||||
}
|
||||
|
||||
RunState PipelineRunner::runStateFromJSON(const PIJSON & j) {
|
||||
RunState state;
|
||||
state.run_id = j["run_id"].toString();
|
||||
state.pipeline_id = j["pipeline_id"].toString();
|
||||
|
||||
PIString statusStr = j["status"].toString();
|
||||
if (statusStr == "pending")
|
||||
state.status = RunStatus::Pending;
|
||||
else if (statusStr == "running")
|
||||
state.status = RunStatus::Running;
|
||||
else if (statusStr == "completed")
|
||||
state.status = RunStatus::Completed;
|
||||
else
|
||||
state.status = RunStatus::Error;
|
||||
|
||||
state.current_step = j["current_step"].toInt();
|
||||
|
||||
PIJSON stepsArrJSON = j["steps"];
|
||||
const auto & stepsArr = stepsArrJSON.array();
|
||||
for (int i = 0; i < stepsArr.size(); ++i) {
|
||||
if (stepsArr[i].isObject()) {
|
||||
state.steps << stepResultFromJSON(stepsArr[i]);
|
||||
}
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
PipelineRunner::PipelineRunner(const PIString & pipelines_dir, const PIString & logs_dir)
|
||||
: pipelines_dir_(pipelines_dir)
|
||||
, logs_dir_(logs_dir) {}
|
||||
@@ -47,6 +126,7 @@ PIString PipelineRunner::startRun(const PIString & pipeline_id) {
|
||||
active_runs_.insert(run_id, true);
|
||||
}
|
||||
|
||||
saveRunState(run_id);
|
||||
appendLog(run_id, "Starting pipeline: " + pipeline.name + " (run_id: " + run_id + ")");
|
||||
|
||||
// Execute in a separate thread
|
||||
@@ -59,11 +139,19 @@ PIString PipelineRunner::startRun(const PIString & pipeline_id) {
|
||||
}
|
||||
|
||||
RunState PipelineRunner::getRunState(const PIString & run_id) {
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (runs_.contains(run_id)) {
|
||||
return runs_.value(run_id);
|
||||
{
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (runs_.contains(run_id)) {
|
||||
return runs_.value(run_id);
|
||||
}
|
||||
}
|
||||
return RunState();
|
||||
|
||||
RunState state = loadRunState(run_id);
|
||||
if (!state.run_id.isEmpty()) {
|
||||
PIMutexLocker ml(mutex_);
|
||||
runs_.insert(run_id, state);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
bool PipelineRunner::isRunActive(const PIString & run_id) {
|
||||
@@ -100,6 +188,8 @@ void PipelineRunner::executePipeline(const PIString & run_id, const Pipeline & p
|
||||
active_runs_.remove(run_id);
|
||||
}
|
||||
|
||||
saveRunState(run_id);
|
||||
|
||||
appendLog(run_id, "Pipeline finished");
|
||||
#else
|
||||
for (int i = 0; i < total; ++i) {
|
||||
@@ -116,6 +206,8 @@ void PipelineRunner::executePipeline(const PIString & run_id, const Pipeline & p
|
||||
}
|
||||
}
|
||||
|
||||
saveRunState(run_id);
|
||||
|
||||
// Build command: opencode run <text> --title <title>
|
||||
PIStringList args;
|
||||
args << "run" << prompt.text << "--title" << prompt.title;
|
||||
@@ -141,6 +233,8 @@ void PipelineRunner::executePipeline(const PIString & run_id, const Pipeline & p
|
||||
}
|
||||
}
|
||||
|
||||
saveRunState(run_id);
|
||||
|
||||
if (rc != 0) {
|
||||
appendLog(run_id, "Pipeline failed at step " + PIString::fromNumber(i + 1));
|
||||
{
|
||||
@@ -149,6 +243,7 @@ void PipelineRunner::executePipeline(const PIString & run_id, const Pipeline & p
|
||||
runs_[run_id].status = RunStatus::Error;
|
||||
}
|
||||
}
|
||||
saveRunState(run_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -162,6 +257,8 @@ void PipelineRunner::executePipeline(const PIString & run_id, const Pipeline & p
|
||||
active_runs_.remove(run_id);
|
||||
}
|
||||
|
||||
saveRunState(run_id);
|
||||
|
||||
appendLog(run_id, "Pipeline finished");
|
||||
#endif
|
||||
}
|
||||
@@ -169,3 +266,57 @@ void PipelineRunner::executePipeline(const PIString & run_id, const Pipeline & p
|
||||
void PipelineRunner::appendLog(const PIString & run_id, const PIString & line) {
|
||||
::appendLog(logs_dir_, run_id, line);
|
||||
}
|
||||
|
||||
void PipelineRunner::saveRunState(const PIString & run_id) {
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (!runs_.contains(run_id)) return;
|
||||
|
||||
RunState state = runs_.value(run_id);
|
||||
PIJSON j = runStateToJSON(state);
|
||||
|
||||
PIString path = getStateFilePath(logs_dir_, run_id);
|
||||
PIString tmpPath = path + ".tmp";
|
||||
|
||||
if (PIFile::writeAll(tmpPath, j.toJSON(PIJSON::Tree).toUTF8())) {
|
||||
PIFile::remove(path);
|
||||
PIFile::rename(tmpPath, path);
|
||||
}
|
||||
}
|
||||
|
||||
RunState PipelineRunner::loadRunState(const PIString & run_id) {
|
||||
PIString path = getStateFilePath(logs_dir_, run_id);
|
||||
if (!PIFile::isExists(path)) return RunState();
|
||||
|
||||
PIByteArray data = PIFile::readAll(path);
|
||||
PIJSON j = PIJSON::fromJSON(PIString::fromUTF8(data));
|
||||
if (j.isObject()) {
|
||||
return runStateFromJSON(j);
|
||||
}
|
||||
return RunState();
|
||||
}
|
||||
|
||||
void PipelineRunner::recoverRuns() {
|
||||
PIDir dir(logs_dir_);
|
||||
if (!dir.isExists()) return;
|
||||
|
||||
const auto entries = dir.entries();
|
||||
for (const auto & entry: entries) {
|
||||
if (!entry.isFile() || entry.extension() != "json") continue;
|
||||
PIString fileName = entry.name();
|
||||
if (!fileName.endsWith(".state.json")) continue;
|
||||
|
||||
PIString run_id = fileName.left(fileName.length() - PIString(".state.json").length());
|
||||
RunState state = loadRunState(run_id);
|
||||
|
||||
if (state.status == RunStatus::Running) {
|
||||
PIMutexLocker ml(mutex_);
|
||||
runs_.insert(run_id, state);
|
||||
active_runs_.insert(run_id, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PipelineRunner::removeStateFile(const PIString & run_id) {
|
||||
PIString path = getStateFilePath(logs_dir_, run_id);
|
||||
PIFile::remove(path);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,17 @@ public:
|
||||
// Check if run is still active
|
||||
bool isRunActive(const PIString & run_id);
|
||||
|
||||
// Persist run state to disk
|
||||
void saveRunState(const PIString & run_id);
|
||||
RunState loadRunState(const PIString & run_id);
|
||||
void recoverRuns();
|
||||
void removeStateFile(const PIString & run_id);
|
||||
|
||||
private:
|
||||
PIJSON runStateToJSON(const RunState & state);
|
||||
PIJSON stepResultToJSON(const StepResult & sr);
|
||||
RunState runStateFromJSON(const PIJSON & j);
|
||||
StepResult stepResultFromJSON(const PIJSON & j);
|
||||
void executePipeline(const PIString & run_id, const Pipeline & pipeline);
|
||||
void appendLog(const PIString & run_id, const PIString & line);
|
||||
|
||||
|
||||
@@ -28,6 +28,9 @@ bool Server::start() {
|
||||
PIDir::make(pipelines_dir_);
|
||||
PIDir::make(logs_dir_);
|
||||
|
||||
// Recover running state from disk
|
||||
runner_.recoverRuns();
|
||||
|
||||
// Register routes
|
||||
httpserver_->registerPath("/api/pipelines", PIHTTP::Method::Get, this, &Server::listPipelines);
|
||||
httpserver_->registerPath("/api/pipelines", PIHTTP::Method::Post, this, &Server::createPipeline);
|
||||
|
||||
Reference in New Issue
Block a user