use std::collections::HashMap;
use std::future::Future;
use std::time::Duration;
use tokio_util::sync::CancellationToken;
#[derive(Debug, Clone)]
pub struct Step {
pub run_id: String,
pub step_number: u32,
pub payload: Vec<u8>,
pub headers: HashMap<String, String>,
pub job_id: String,
pub attempts: u32,
pub cancel_token: CancellationToken,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum StepOutcome {
Continue {
payload: Vec<u8>,
},
ContinueAfter {
payload: Vec<u8>,
delay: Duration,
},
Succeed {
result: Vec<u8>,
},
Fail {
reason: String,
},
Cancel {
reason: String,
},
}
#[derive(Debug)]
pub struct StepError {
pub message: String,
pub kind: StepErrorKind,
}
impl StepError {
pub fn transient(message: impl Into<String>) -> Self {
Self {
message: message.into(),
kind: StepErrorKind::Transient,
}
}
pub fn permanent(message: impl Into<String>) -> Self {
Self {
message: message.into(),
kind: StepErrorKind::Permanent,
}
}
}
impl std::fmt::Display for StepError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for StepError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum StepErrorKind {
Transient,
Permanent,
}
pub trait StepRunner: Send + Sync {
fn run_step(
&self,
step: &Step,
) -> impl Future<Output = std::result::Result<StepOutcome, StepError>> + Send;
}