Skip to main content

boson_core/models/
job.rs

1//! Job model — an enqueued unit of work.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7/// Status of a job in the queue.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
9#[serde(rename_all = "lowercase")]
10pub enum JobStatus {
11    /// Waiting in queue.
12    #[default]
13    Queued,
14    /// Currently executing.
15    Running,
16    /// Completed successfully.
17    Success,
18    /// Failed (may retry).
19    Failed,
20    /// Canceled by user.
21    Canceled,
22}
23
24impl std::fmt::Display for JobStatus {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Self::Queued => write!(f, "queued"),
28            Self::Running => write!(f, "running"),
29            Self::Success => write!(f, "success"),
30            Self::Failed => write!(f, "failed"),
31            Self::Canceled => write!(f, "canceled"),
32        }
33    }
34}
35
36/// An enqueued unit of work (one task invocation).
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct Job {
39    /// Unique identifier (UUID).
40    pub job_id: String,
41    /// Task name (must exist in registry).
42    pub task_name: String,
43    /// Captured actor (identity) at enqueue time.
44    pub actor_json: Value,
45    /// Serialized task parameters.
46    pub params_json: Value,
47    /// Priority (lower = higher priority). From task config.
48    pub priority: i32,
49    /// Pool name for worker assignment.
50    pub pool: String,
51    /// Current status.
52    pub status: JobStatus,
53    /// Optional idempotency key.
54    pub idempotency_key: Option<String>,
55    /// When the job was enqueued.
56    pub created_at: DateTime<Utc>,
57    /// Signature hash at enqueue (for validation).
58    pub signature_hash: u64,
59    /// Attempt number (1-based); incremented on retry.
60    pub attempt: i32,
61}
62
63impl Job {
64    /// Create a new queued job (typically from enqueue).
65    #[must_use]
66    pub fn new(
67        task_name: &str,
68        actor_json: Value,
69        params_json: Value,
70        priority: i32,
71        pool: &str,
72        signature_hash: u64,
73        idempotency_key: Option<String>,
74    ) -> Self {
75        Self {
76            job_id: uuid::Uuid::new_v4().to_string(),
77            task_name: task_name.to_string(),
78            actor_json,
79            params_json,
80            priority,
81            pool: pool.to_string(),
82            status: JobStatus::Queued,
83            idempotency_key,
84            created_at: Utc::now(),
85            signature_hash,
86            attempt: 1,
87        }
88    }
89}