1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
9#[serde(rename_all = "lowercase")]
10pub enum JobStatus {
11 #[default]
13 Queued,
14 Running,
16 Success,
18 Failed,
20 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#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct Job {
39 pub job_id: String,
41 pub task_name: String,
43 pub actor_json: Value,
45 pub params_json: Value,
47 pub priority: i32,
49 pub pool: String,
51 pub status: JobStatus,
53 pub idempotency_key: Option<String>,
55 pub created_at: DateTime<Utc>,
57 pub signature_hash: u64,
59 pub attempt: i32,
61}
62
63impl Job {
64 #[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}