supercode-interchange 0.4.18

Canonical, provider-neutral session interchange primitives for Supercode
Documentation
//! Fires: one execution of a job (§2.6; Hermes `cron/executions.db`).

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::ontology::Residue;

/// A fire's status. `unknown` is Hermes's own word for a fire whose scheduler
/// restarted under it; `completed` ⇄ `succeeded` on the codec boundary and
/// `timeout` writes back as `failed`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum FireStatus {
    /// Claimed, not yet started.
    Claimed,
    /// Running (read from Hermes; never written by us).
    Running,
    /// Finished with a result.
    Succeeded,
    /// Finished with an error.
    Failed,
    /// The session ended without a result.
    Timeout,
    /// The scheduler restarted under it.
    Unknown,
}

impl FireStatus {
    /// Hermes's `executions.status` word.
    pub fn hermes_word(self) -> &'static str {
        match self {
            Self::Claimed => "claimed",
            Self::Running => "running",
            Self::Succeeded => "completed",
            Self::Failed | Self::Timeout => "failed",
            Self::Unknown => "unknown",
        }
    }

    /// The status for a Hermes `executions.status` word.
    pub fn from_hermes_word(word: &str) -> Option<Self> {
        Some(match word {
            "claimed" => Self::Claimed,
            "running" => Self::Running,
            "completed" => Self::Succeeded,
            "failed" => Self::Failed,
            "unknown" => Self::Unknown,
            _ => return None,
        })
    }
}

/// One fire.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Fire {
    /// The fire id, which is also its session name `cron_<job_id>_<ts>`.
    pub id: String,
    /// The job.
    pub job_id: String,
    /// The session it opened, when known.
    #[serde(default)]
    pub session_id: Option<String>,
    /// Status.
    pub status: FireStatus,
    /// Claim instant.
    pub claimed_at: String,
    /// Start instant.
    #[serde(default)]
    pub started_at: Option<String>,
    /// Finish instant.
    #[serde(default)]
    pub finished_at: Option<String>,
    /// The error, on failure.
    #[serde(default)]
    pub error: Option<String>,
    /// The delivery this fire produced, if any.
    #[serde(default)]
    pub obligation_id: Option<String>,
    /// Ledger columns the record does not model (`source`, `pid`, …), verbatim.
    #[serde(default)]
    pub residue: Residue,
}