diff --git a/jobs-core/src/job_info.rs b/jobs-core/src/job_info.rs index b55ad80..8bf32d0 100644 --- a/jobs-core/src/job_info.rs +++ b/jobs-core/src/job_info.rs @@ -90,6 +90,10 @@ impl NewJobInfo { &self.queue } + pub fn timeout(&self) -> i64 { + self.timeout + } + /// Whether this job is ready to be run immediately pub fn is_ready(&self) -> bool { self.next_queue.is_none() @@ -240,6 +244,16 @@ impl JobInfo { && (self.updated_at + Duration::milliseconds(self.timeout)) < now) } + /// Get the status of the job + pub fn status(&self) -> JobStatus { + self.status.clone() + } + + /// The the date of the most recent update + pub fn updated_at(&self) -> DateTime { + self.updated_at + } + pub(crate) fn is_in_queue(&self, queue: &str) -> bool { self.queue == queue } diff --git a/jobs-core/src/lib.rs b/jobs-core/src/lib.rs index 67fe03d..0879577 100644 --- a/jobs-core/src/lib.rs +++ b/jobs-core/src/lib.rs @@ -125,6 +125,31 @@ impl JobStatus { } } +impl std::fmt::Display for JobStatus { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + JobStatus::Pending => write!(f, "Pending"), + JobStatus::Running => write!(f, "Running"), + } + } +} + +#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, thiserror::Error)] +#[error("Invalid job status")] +pub struct JobStatusError; + +impl std::str::FromStr for JobStatus { + type Err = JobStatusError; + + fn from_str(s: &str) -> Result { + match s { + "Pending" => Ok(JobStatus::Pending), + "Running" => Ok(JobStatus::Running), + _ => Err(JobStatusError), + } + } +} + #[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)] /// Different styles for retrying jobs pub enum Backoff {