1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
use crate::{
stream::StreamReceiver, Context, JobRunResult, StepRunResult, WorkflowLog, WorkflowLogType,
WorkflowRunResult, WorkflowStateEvent,
};
pub use tokio_stream::{Stream, StreamExt};
#[derive(Debug, Clone, PartialEq)]
pub enum RunResult {
Succeeded,
Failed { exit_code: i32 },
Cancelled,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Log {
pub log_type: WorkflowLogType,
pub message: String,
}
impl Log {
pub fn log(message: impl Into<String>) -> Self {
Self {
log_type: WorkflowLogType::Log,
message: message.into(),
}
}
pub fn error(message: impl Into<String>) -> Self {
Self {
log_type: WorkflowLogType::Error,
message: message.into(),
}
}
pub fn is_error(&self) -> bool {
self.log_type == WorkflowLogType::Error
}
}
pub type RunResponse = crate::Result<StreamReceiver>;
/// # Runner
/// The `Runner` trait provides the most fundamental deconstruction of a runner. You can implement the `run` method to customize your own runner.
///
/// The `run` method is asynchronous. Before `run` is executed, the `Step` status is `WorkflowState::Pending`.
///
/// During the execution of `run`, the status is set to `WorkflowState::Queued`.
/// At this point, you can handle scheduling logic related to the runner. **(Please avoid executing step's runtime logic within the asynchronous `run` method. It is highly recommended to use a separate thread to process individual steps.)**
///
/// After `run` has completed, the status becomes `WorkflowState::InProgress`. This is when the step is truly running. The `run` method returns a stream result that implements the `Stream` trait, allowing dynamic log updates.
///
/// ## Example
///
/// ```rust
/// struct Runner;
///
/// #[astro_run::async_trait]
/// impl astro_run::Runner for Runner {
/// async fn run(&self, ctx: astro_run::Context) -> astro_run::RunResponse {
/// let (tx, rx) = astro_run::stream();
///
/// tokio::task::spawn(async move {
/// // Send running log
/// tx.log(ctx.command.run);
///
/// // Send success log
/// tx.end(astro_run::RunResult::Succeeded);
/// });
///
/// Ok(rx)
/// }
/// }
/// ```
///
#[async_trait::async_trait]
pub trait Runner: Send + Sync {
fn on_run_workflow(&self, _event: crate::RunWorkflowEvent) {}
fn on_run_job(&self, _event: crate::RunJobEvent) {}
fn on_run_step(&self, _event: crate::RunStepEvent) {}
fn on_step_completed(&self, _result: StepRunResult) {}
fn on_job_completed(&self, _result: JobRunResult) {}
fn on_workflow_completed(&self, _result: WorkflowRunResult) {}
fn on_state_change(&self, _event: WorkflowStateEvent) {}
fn on_log(&self, _log: WorkflowLog) {}
async fn run(&self, config: Context) -> RunResponse;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_log() {
let log = Log::log("test");
assert_eq!(log.log_type, WorkflowLogType::Log);
assert_eq!(log.message, "test");
assert!(!log.is_error());
let log = Log::error("test");
assert_eq!(log.log_type, WorkflowLogType::Error);
assert_eq!(log.message, "test");
assert!(log.is_error());
}
}