tako_rs_core/queue/job.rs
1//! Job and dead-letter task types passed across the queue.
2
3use std::time::Instant;
4
5use super::QueueError;
6
7/// A job passed to a handler function.
8///
9/// Contains the serialized payload and metadata about the job.
10pub struct Job {
11 /// The raw JSON payload.
12 pub(crate) payload: Vec<u8>,
13 /// Job name (the key it was registered under).
14 pub name: String,
15 /// Current attempt number (0-based).
16 pub attempt: u32,
17 /// Unique job ID.
18 pub id: u64,
19}
20
21impl Job {
22 /// Deserialize the job payload into the expected type.
23 pub fn deserialize<T: serde::de::DeserializeOwned>(&self) -> Result<T, QueueError> {
24 serde_json::from_slice(&self.payload).map_err(|e| QueueError::HandlerError(e.to_string()))
25 }
26
27 /// Access the raw payload bytes.
28 pub fn raw_payload(&self) -> &[u8] {
29 &self.payload
30 }
31}
32
33/// A failed job stored in the dead letter queue.
34#[derive(Debug, Clone)]
35pub struct DeadJob {
36 /// Unique job ID.
37 pub id: u64,
38 /// Job name.
39 pub name: String,
40 /// Raw payload.
41 pub payload: Vec<u8>,
42 /// Number of attempts made.
43 pub attempts: u32,
44 /// The final error message.
45 pub error: String,
46 /// When the job was moved to the DLQ.
47 pub failed_at: Instant,
48}