background-jobs/jobs-core/src/lib.rs

92 lines
1.9 KiB
Rust
Raw Normal View History

2018-11-05 02:13:06 +00:00
#[macro_use]
extern crate failure;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
use failure::Error;
2018-11-08 02:20:30 +00:00
mod job_info;
mod processor;
mod processors;
mod storage;
pub use crate::{
job_info::JobInfo, processor::Processor, processors::Processors, storage::Storage,
};
2018-11-06 02:53:03 +00:00
2018-11-05 02:13:06 +00:00
#[derive(Debug, Fail)]
pub enum JobError {
#[fail(display = "Error performing job: {}", _0)]
Processing(#[cause] Error),
#[fail(display = "Could not make JSON value from arguments")]
Json,
#[fail(display = "No processor available for job")]
MissingProcessor,
}
2018-11-07 02:01:43 +00:00
/// Set the status of a job when storing it
2018-11-05 02:13:06 +00:00
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum JobStatus {
2018-11-07 02:01:43 +00:00
/// Job should be queued
2018-11-05 02:13:06 +00:00
Pending,
2018-11-07 02:01:43 +00:00
/// Job has been dequeued, but is not yet running
Staged,
2018-11-07 02:01:43 +00:00
/// Job is running
Running,
/// Job has failed
Failed,
/// Job has finished
2018-11-05 02:13:06 +00:00
Finished,
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum Backoff {
/// Seconds between execution
Linear(usize),
/// Base for seconds between execution
Exponential(usize),
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum MaxRetries {
/// Keep retrying forever
Infinite,
/// Put a limit on the number of retries
Count(usize),
}
2018-11-07 03:26:48 +00:00
impl MaxRetries {
fn compare(&self, retry_count: u32) -> ShouldStop {
match *self {
MaxRetries::Infinite => ShouldStop::Requeue,
MaxRetries::Count(ref count) => {
if (retry_count as usize) <= *count {
2018-11-07 03:26:48 +00:00
ShouldStop::Requeue
} else {
ShouldStop::LimitReached
}
}
}
}
}
2018-11-05 02:13:06 +00:00
#[derive(Clone, Debug, Eq, PartialEq)]
2018-11-06 02:53:03 +00:00
pub enum ShouldStop {
2018-11-05 02:13:06 +00:00
LimitReached,
Requeue,
}
impl ShouldStop {
2018-11-06 02:53:03 +00:00
pub fn should_requeue(&self) -> bool {
2018-11-05 02:13:06 +00:00
*self == ShouldStop::Requeue
}
}