use std::sync::Arc;
use std::any::Any;
use chrono::{DateTime, Utc};
use uuid::Uuid;
#[derive(Clone)]
pub struct JobContext {
pub job_id: Uuid,
pub queue: String,
pub attempt: u32,
pub enqueued_at: DateTime<Utc>,
state: Arc<dyn Any + Send + Sync>,
}
impl JobContext {
pub(crate) fn new(
job_id: Uuid,
queue: String,
attempt: u32,
enqueued_at: DateTime<Utc>,
state: Arc<dyn Any + Send + Sync>,
) -> Self {
Self { job_id, queue, attempt, enqueued_at, state }
}
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.state.downcast_ref::<T>()
}
pub fn is_first_attempt(&self) -> bool {
self.attempt == 1
}
pub fn is_retry(&self) -> bool {
self.attempt > 1
}
}