rewrite all to PIP
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
#include "runner.h"
|
||||
|
||||
#include "pipeline.h"
|
||||
|
||||
#include <piprocess.h>
|
||||
#include <pisystemtime.h>
|
||||
|
||||
PipelineRunner::PipelineRunner(const PIString & pipelines_dir, const PIString & logs_dir)
|
||||
: pipelines_dir_(pipelines_dir)
|
||||
, logs_dir_(logs_dir) {}
|
||||
|
||||
PipelineRunner::~PipelineRunner() {}
|
||||
|
||||
PIString PipelineRunner::startRun(const PIString & pipeline_id) {
|
||||
Pipeline pipeline = findPipeline(pipelines_dir_, pipeline_id);
|
||||
if (pipeline.id.isEmpty()) return PIString();
|
||||
|
||||
PIString run_id = generateUUID();
|
||||
|
||||
RunState state;
|
||||
state.run_id = run_id;
|
||||
state.pipeline_id = pipeline_id;
|
||||
state.status = RunStatus::Running;
|
||||
state.current_step = 0;
|
||||
|
||||
// Initialize step results
|
||||
for (int i = 0; i < pipeline.prompts.size(); ++i) {
|
||||
StepResult sr;
|
||||
sr.step_index = i;
|
||||
sr.title = pipeline.prompts[i].title;
|
||||
sr.status = RunStatus::Pending;
|
||||
state.steps << sr;
|
||||
}
|
||||
|
||||
{
|
||||
PIMutexLocker ml(mutex_);
|
||||
runs_.insert(run_id, state);
|
||||
active_runs_.insert(run_id, true);
|
||||
}
|
||||
|
||||
appendLog(run_id, "Starting pipeline: " + pipeline.name + " (run_id: " + run_id + ")");
|
||||
|
||||
// Execute in a separate thread
|
||||
// Use a lambda that captures a copy of the pipeline and run_id
|
||||
PIString rid = run_id;
|
||||
PIString pdir = pipelines_dir_;
|
||||
PIString ldir = logs_dir_;
|
||||
PIThread * thread = new PIThread([this, rid, pipeline, pdir, ldir]() { executePipeline(rid, pipeline); });
|
||||
thread->start();
|
||||
|
||||
return run_id;
|
||||
}
|
||||
|
||||
RunState PipelineRunner::getRunState(const PIString & run_id) {
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (runs_.contains(run_id)) {
|
||||
return runs_.value(run_id);
|
||||
}
|
||||
return RunState();
|
||||
}
|
||||
|
||||
bool PipelineRunner::isRunActive(const PIString & run_id) {
|
||||
PIMutexLocker ml(mutex_);
|
||||
return active_runs_.contains(run_id);
|
||||
}
|
||||
|
||||
void PipelineRunner::executePipeline(const PIString & run_id, const Pipeline & pipeline) {
|
||||
int total = pipeline.prompts.size();
|
||||
|
||||
for (int i = 0; i < total; ++i) {
|
||||
const Prompt & prompt = pipeline.prompts[i];
|
||||
appendLog(run_id, "[Step " + PIString::fromNumber(i + 1) + "/" + PIString::fromNumber(total) + "] Starting: " + prompt.title);
|
||||
appendLog(run_id, "[Step " + PIString::fromNumber(i + 1) + "/" + PIString::fromNumber(total) + "] Prompt: " + prompt.text);
|
||||
|
||||
// Update step status to running
|
||||
{
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (runs_.contains(run_id)) {
|
||||
runs_[run_id].current_step = i;
|
||||
runs_[run_id].steps[i].status = RunStatus::Running;
|
||||
}
|
||||
}
|
||||
|
||||
// Build command: opencode run <text> --title <title>
|
||||
PIProcess proc;
|
||||
proc.enableReadStdOut(true);
|
||||
proc.enableReadStdErr(true);
|
||||
|
||||
if (!pipeline.working_dir.isEmpty()) {
|
||||
proc.setWorkingDirectory(pipeline.working_dir);
|
||||
}
|
||||
|
||||
PIStringList args;
|
||||
args << "run" << prompt.text << "--title" << prompt.title;
|
||||
proc.exec("opencode", args);
|
||||
|
||||
// Wait for process to finish (max 300 seconds per step)
|
||||
proc.waitForFinish(PISystemTime::fromSeconds(300));
|
||||
|
||||
PIByteArray stdoutData = proc.readOutput();
|
||||
PIByteArray stderrData = proc.readError();
|
||||
int rc = proc.exitCode();
|
||||
|
||||
PIString output = PIString::fromUTF8(stdoutData);
|
||||
PIString error = PIString::fromUTF8(stderrData);
|
||||
|
||||
RunStatus stepStatus = (rc == 0) ? RunStatus::Completed : RunStatus::Error;
|
||||
appendLog(run_id,
|
||||
"[Step " + PIString::fromNumber(i + 1) + "/" + PIString::fromNumber(total) + "] " +
|
||||
runStatusToString(stepStatus).toUpperCase() + " (returncode: " + PIString::fromNumber(rc) + ")");
|
||||
|
||||
// Update step result
|
||||
{
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (runs_.contains(run_id)) {
|
||||
runs_[run_id].steps[i].status = stepStatus;
|
||||
runs_[run_id].steps[i].returncode = rc;
|
||||
runs_[run_id].steps[i].output = output;
|
||||
runs_[run_id].steps[i].error = error;
|
||||
}
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
appendLog(run_id, "Pipeline failed at step " + PIString::fromNumber(i + 1));
|
||||
{
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (runs_.contains(run_id)) {
|
||||
runs_[run_id].status = RunStatus::Error;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Mark as completed if all steps passed
|
||||
{
|
||||
PIMutexLocker ml(mutex_);
|
||||
if (runs_.contains(run_id) && runs_[run_id].status == RunStatus::Running) {
|
||||
runs_[run_id].status = RunStatus::Completed;
|
||||
}
|
||||
active_runs_.remove(run_id);
|
||||
}
|
||||
|
||||
appendLog(run_id, "Pipeline finished");
|
||||
}
|
||||
|
||||
void PipelineRunner::appendLog(const PIString & run_id, const PIString & line) {
|
||||
::appendLog(logs_dir_, run_id, line);
|
||||
}
|
||||
Reference in New Issue
Block a user