Skip to main content

simple_queue/
result.rs

1/// Result of a job execution, should be returned by the job handler.
2pub enum JobResult {
3    /// Job processed successfully.
4    Success,
5    /// Job failed, subject to retry (with BackoffStrategy).
6    Failed,
7    /// Retry (i.e. attempt count incremented) at a specific time.
8    /// IMPORTANT: Job cannot be retried sooner than the backoff strategy allows
9    RetryAt(chrono::DateTime<chrono::Utc>),
10    /// Reschedule to specific time.
11    /// Attempt it NOT consumed. Use with caution.
12    /// Queue's max_reprocess_count is a safety measure to prevent infinite rescheduling.
13    RescheduleAt(chrono::DateTime<chrono::Utc>),
14    /// Handler not found for the job type, status changes to unprocessable.
15    HandlerMissing,
16    /// Critical failure, job is not retried.
17    Critical,
18    /// Cancel the job (i.e. do not retry anymore), status changes to cancelled.
19    Cancel,
20    /// Handler can't process the job (e.g. deserialization failures), status changes to unprocessable.
21    Unprocessable,
22    /// Internal error, same as Failed but looks nicer when handler encountered recoverable error.
23    /// Attempt is consumed.
24    InternalError,
25}
26impl JobResult {
27    pub(crate) fn handle(&self) -> JobResultInternal {
28        match self {
29            JobResult::Success => JobResultInternal::Completed,
30            JobResult::Failed => JobResultInternal::Pending,
31            JobResult::RetryAt(_) => JobResultInternal::Pending,
32            JobResult::RescheduleAt(_) => JobResultInternal::Pending,
33            JobResult::InternalError => JobResultInternal::Pending,
34            JobResult::HandlerMissing => JobResultInternal::Unprocessable,
35            JobResult::Unprocessable => JobResultInternal::Unprocessable,
36            JobResult::Critical => JobResultInternal::Critical,
37            JobResult::Cancel => JobResultInternal::Cancelled,
38        }
39    }
40}
41
42#[derive(Debug)]
43pub(crate) enum JobResultInternal {
44    Pending,
45    Failed,
46    Completed,
47    Unprocessable,
48    Cancelled,
49    Critical,
50    Running,
51    BadJob,
52}
53impl std::fmt::Display for JobResultInternal {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        match self {
56            JobResultInternal::Pending => write!(f, "pending"),
57            JobResultInternal::Failed => write!(f, "failed"),
58            JobResultInternal::Completed => write!(f, "completed"),
59            JobResultInternal::Unprocessable => write!(f, "unprocessable"),
60            JobResultInternal::Cancelled => write!(f, "cancelled"),
61            JobResultInternal::Critical => write!(f, "critical_failure"),
62            JobResultInternal::Running => write!(f, "running"),
63            JobResultInternal::BadJob => write!(f, "bad_job"),
64        }
65    }
66}
67
68pub(crate) enum AnyJobResult {
69    Internal(JobResultInternal),
70    Public(JobResult),
71}
72
73impl From<JobResult> for AnyJobResult {
74    fn from(result: JobResult) -> Self {
75        Self::Public(result)
76    }
77}
78
79impl From<JobResultInternal> for AnyJobResult {
80    fn from(result: JobResultInternal) -> Self {
81        Self::Internal(result)
82    }
83}