use serde::{Deserialize, Serialize};
use super::{
super::store::{RunStore, next_id, now_secs},
FileRuntimeStore, RuntimeError, SCHEMA_VERSION, fs_util, lock_unpoisoned,
};
#[derive(Serialize, Deserialize)]
struct RunEvent {
schema: u32,
at: i64,
run_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
agent_id: Option<String>,
state: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
error: Option<String>,
}
impl RunStore for FileRuntimeStore {
fn start_run(&self, agent_id: &str) -> Result<String, RuntimeError> {
let run_id = next_id("run");
self.append_run_event(RunEvent {
schema: SCHEMA_VERSION,
at: now_secs(),
run_id: run_id.clone(),
agent_id: Some(agent_id.to_string()),
state: "running".to_string(),
error: None,
})?;
Ok(run_id)
}
fn update_run_state(
&self,
run_id: &str,
state: &str,
error: Option<&str>,
) -> Result<(), RuntimeError> {
self.append_run_event(RunEvent {
schema: SCHEMA_VERSION,
at: now_secs(),
run_id: run_id.to_string(),
agent_id: None,
state: state.to_string(),
error: error.map(str::to_string),
})
}
fn finish_run(&self, run_id: &str) -> Result<(), RuntimeError> {
self.update_run_state(run_id, "finished", None)
}
fn fail_run(&self, run_id: &str, error: &str) -> Result<(), RuntimeError> {
self.update_run_state(run_id, "failed", Some(error))
}
}
impl FileRuntimeStore {
fn append_run_event(&self, event: RunEvent) -> Result<(), RuntimeError> {
let line = serde_json::to_string(&event)
.map_err(|error| RuntimeError::Store(error.to_string()))?;
let _guard = lock_unpoisoned(&self.runs_lock);
fs_util::append_lines(&self.runs_path(), &[line])
}
}