use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type JobId = String;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobSpawnRequest {
pub label: String,
pub binary: String,
pub args: Vec<String>,
#[serde(default)]
pub cwd: Option<String>,
#[serde(default)]
pub env: HashMap<String, String>,
#[serde(default)]
pub timeout_secs: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum JobEvent {
JobStarted {
job_id: JobId,
#[serde(flatten)]
data: JobStartedData,
},
JobProgress {
job_id: JobId,
#[serde(flatten)]
data: JobProgressData,
},
JobLogLine {
job_id: JobId,
#[serde(flatten)]
data: JobLogLineData,
},
JobOutputChunk {
job_id: JobId,
#[serde(flatten)]
data: JobOutputChunkData,
},
JobCancelled {
job_id: JobId,
#[serde(flatten)]
data: JobCancelledData,
},
JobCompleted {
job_id: JobId,
#[serde(flatten)]
data: JobCompletedData,
},
JobFailed {
job_id: JobId,
#[serde(flatten)]
data: JobFailedData,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobStartedData {
pub pid: Option<u32>,
pub started_at: String, }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobProgressData {
pub percent: Option<u8>,
pub step: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobLogLineData {
pub line: String,
pub stream: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobOutputChunkData {
pub bytes: Vec<u8>,
pub stream: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobCancelledData {
pub reason: String,
pub cancelled_at: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobCompletedData {
pub exit_code: i32,
pub completed_at: String,
pub output_tail: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobFailedData {
pub exit_code: Option<i32>,
pub error: String,
pub failed_at: String,
}
pub trait JobHost: Send + Sync {
fn spawn(&self, req: JobSpawnRequest) -> JobId;
fn cancel(&self, job_id: &JobId) -> bool;
fn status(&self, job_id: &JobId) -> Option<JobStatus>;
fn drain_events(&self) -> Vec<JobEvent>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JobStatus {
Pending,
Running {
started_at: String,
percent: Option<u8>,
},
Cancelled {
reason: String,
},
Completed {
exit_code: i32,
},
Failed {
exit_code: Option<i32>,
error: String,
},
}