use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum JobStatus {
#[default]
Queued,
Running,
Success,
Failed,
Canceled,
}
impl std::fmt::Display for JobStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Queued => write!(f, "queued"),
Self::Running => write!(f, "running"),
Self::Success => write!(f, "success"),
Self::Failed => write!(f, "failed"),
Self::Canceled => write!(f, "canceled"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Job {
pub job_id: String,
pub task_name: String,
pub actor_json: Value,
pub params_json: Value,
pub priority: i32,
pub pool: String,
pub status: JobStatus,
pub idempotency_key: Option<String>,
pub created_at: DateTime<Utc>,
pub signature_hash: u64,
pub attempt: i32,
}
impl Job {
#[must_use]
pub fn new(
task_name: &str,
actor_json: Value,
params_json: Value,
priority: i32,
pool: &str,
signature_hash: u64,
idempotency_key: Option<String>,
) -> Self {
Self {
job_id: uuid::Uuid::new_v4().to_string(),
task_name: task_name.to_string(),
actor_json,
params_json,
priority,
pool: pool.to_string(),
status: JobStatus::Queued,
idempotency_key,
created_at: Utc::now(),
signature_hash,
attempt: 1,
}
}
}