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

165 lines
4 KiB
Rust
Raw Normal View History

/*
* This file is part of Background Jobs.
*
2019-05-25 20:26:12 +00:00
* Copyright © 2019 Riley Trautman
*
* Background Jobs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Background Jobs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Background Jobs. If not, see <http://www.gnu.org/licenses/>.
*/
2018-12-16 18:43:44 +00:00
use failure::{Error, Fail};
use serde_derive::{Deserialize, Serialize};
2018-11-05 02:13:06 +00:00
mod job;
2018-11-08 02:20:30 +00:00
mod job_info;
mod processor;
mod processor_map;
mod stats;
2018-11-08 02:20:30 +00:00
mod storage;
pub use crate::{
2018-12-16 18:43:44 +00:00
job::Job,
job_info::{JobInfo, NewJobInfo, ReturnJobInfo},
2018-12-16 18:43:44 +00:00
processor::Processor,
processor_map::ProcessorMap,
stats::{JobStat, Stats},
2019-05-28 00:01:21 +00:00
storage::{memory_storage, Storage},
2018-11-08 02:20:30 +00:00
};
2018-11-06 02:53:03 +00:00
2018-11-05 02:13:06 +00:00
#[derive(Debug, Fail)]
/// The error type returned by a `Processor`'s `process` method
2018-11-05 02:13:06 +00:00
pub enum JobError {
/// Some error occurred while processing the job
2018-11-05 02:13:06 +00:00
#[fail(display = "Error performing job: {}", _0)]
Processing(#[cause] Error),
/// Creating a `Job` type from the provided `serde_json::Value` failed
2018-11-05 02:13:06 +00:00
#[fail(display = "Could not make JSON value from arguments")]
Json,
/// No processor was present to handle a given job
2018-11-05 02:13:06 +00:00
#[fail(display = "No processor available for job")]
MissingProcessor,
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum JobResult {
Success,
Failure,
MissingProcessor,
}
impl JobResult {
pub fn success() -> Self {
JobResult::Success
}
pub fn failure() -> Self {
JobResult::Failure
}
pub fn missing_processor() -> Self {
JobResult::MissingProcessor
}
pub fn is_failure(&self) -> bool {
*self == JobResult::Failure
}
pub fn is_success(&self) -> bool {
*self == JobResult::Success
}
pub fn is_missing_processor(&self) -> bool {
*self == JobResult::MissingProcessor
}
}
2018-11-05 02:13:06 +00:00
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
/// Set the status of a job when storing it
2018-11-05 02:13:06 +00:00
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 is running
Running,
}
impl JobStatus {
pub fn pending() -> Self {
JobStatus::Pending
}
pub fn running() -> Self {
JobStatus::Running
}
2018-11-07 02:01:43 +00:00
pub fn is_pending(&self) -> bool {
*self == JobStatus::Pending
}
2018-11-07 02:01:43 +00:00
pub fn is_running(&self) -> bool {
*self == JobStatus::Running
}
2018-11-05 02:13:06 +00:00
}
#[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)]
/// A type that represents whether a job should be requeued
2018-11-06 02:53:03 +00:00
pub enum ShouldStop {
/// The job has hit the maximum allowed number of retries, and should be failed permanently
2018-11-05 02:13:06 +00:00
LimitReached,
/// The job is allowed to be put back into the job queue
2018-11-05 02:13:06 +00:00
Requeue,
}
impl ShouldStop {
/// A boolean representation of this state
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
}
}