background_jobs_core/
lib.rs1#![deny(missing_docs)]
2
3mod box_error;
10mod catch_unwind;
11mod job;
12mod job_info;
13mod processor_map;
14mod storage;
15mod unsend_job;
16
17pub use crate::{
18 box_error::BoxError,
19 job::{new_job, new_scheduled_job, process, Job},
20 job_info::{JobInfo, NewJobInfo, ReturnJobInfo},
21 processor_map::{CachedProcessorMap, ProcessorMap},
22 storage::{memory_storage, Storage},
23};
24
25pub use unsend_job::{JoinError, UnsendJob, UnsendSpawner};
26
27#[derive(Debug, thiserror::Error)]
28pub enum JobError {
30 #[error("{0}")]
32 Processing(#[from] BoxError),
33
34 #[error("Could not make JSON value from arguments")]
36 Json,
37
38 #[error("This job type was not registered for the client")]
40 Unregistered,
41}
42
43#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
44pub enum JobResult {
46 Success,
48
49 Failure,
51
52 Unregistered,
54
55 Unexecuted,
57}
58
59impl JobResult {
60 pub const fn success() -> Self {
62 JobResult::Success
63 }
64
65 pub const fn failure() -> Self {
67 JobResult::Failure
68 }
69
70 pub const fn unregistered() -> Self {
72 JobResult::Unregistered
73 }
74
75 pub const fn is_failure(self) -> bool {
77 matches!(self, JobResult::Failure)
78 }
79
80 pub const fn is_success(self) -> bool {
82 matches!(self, JobResult::Success)
83 }
84
85 pub const fn is_unregistered(self) -> bool {
87 matches!(self, JobResult::Unregistered)
88 }
89
90 pub const fn is_unexecuted(self) -> bool {
92 matches!(self, JobResult::Unexecuted)
93 }
94}
95
96#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
97pub enum Backoff {
99 Linear(usize),
104
105 Exponential(usize),
111}
112
113#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
114pub enum MaxRetries {
116 Infinite,
118
119 Count(usize),
121}
122
123impl MaxRetries {
124 fn compare(self, retry_count: u32) -> ShouldStop {
125 match self {
126 MaxRetries::Infinite => ShouldStop::Requeue,
127 MaxRetries::Count(count) => {
128 if (retry_count as usize) <= count {
129 ShouldStop::Requeue
130 } else {
131 ShouldStop::LimitReached
132 }
133 }
134 }
135 }
136}
137
138#[derive(Clone, Copy, Debug, Eq, PartialEq)]
139pub enum ShouldStop {
141 LimitReached,
143
144 Requeue,
146}
147
148impl ShouldStop {
149 pub const fn should_requeue(&self) -> bool {
151 matches!(self, ShouldStop::Requeue)
152 }
153}
154
155impl From<serde_json::error::Error> for JobError {
156 fn from(_: serde_json::error::Error) -> Self {
157 JobError::Json
158 }
159}