mytheclipse_queue/job.rs
1//! The `Job` type and its metadata.
2//!
3//! A minimal, dependency-free in-process job queue using `tokio::sync::mpsc`.
4
5use std::time::{Duration, SystemTime};
6
7/// A unique identifier for a queued job.
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9pub struct JobId(pub String);
10
11impl JobId {
12 pub fn generate() -> Self {
13 Self(uuid::Uuid::new_v4().to_string())
14 }
15}
16
17impl std::fmt::Display for JobId {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 write!(f, "{}", self.0)
20 }
21}
22
23/// A unit of queued work.
24#[derive(Debug, Clone)]
25pub struct Job {
26 /// The unique ID of this job.
27 pub id: JobId,
28 /// The topic/queue name the job was delivered from.
29 pub topic: String,
30 /// The raw payload bytes.
31 pub payload: Vec<u8>,
32 /// How many times this job has been attempted (0 = first attempt).
33 pub attempt: u32,
34 /// When the job was first enqueued.
35 pub enqueued_at: SystemTime,
36 /// When the job was delivered to the worker (None if not yet delivered).
37 pub delivered_at: Option<SystemTime>,
38 /// Optional visibility timeout - after this the job becomes visible again.
39 pub visibility_timeout: Option<Duration>,
40}
41
42impl Job {
43 pub fn new(id: JobId, topic: &str, payload: Vec<u8>) -> Self {
44 // One clock read for both timestamps: they describe the same instant,
45 // and `SystemTime::now()` is not free.
46 let now = SystemTime::now();
47 Self {
48 id,
49 topic: topic.to_string(),
50 payload,
51 attempt: 0,
52 enqueued_at: now,
53 delivered_at: Some(now),
54 visibility_timeout: None,
55 }
56 }
57}