2018-11-18 01:39:04 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Background Jobs.
|
|
|
|
*
|
|
|
|
* Copyright © 2018 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-11-08 02:20:30 +00:00
|
|
|
use chrono::{offset::Utc, DateTime, Duration as OldDuration};
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use crate::{Backoff, JobStatus, MaxRetries, ShouldStop};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
2018-11-18 01:39:04 +00:00
|
|
|
/// Metadata pertaining to a job that exists within the background_jobs system
|
|
|
|
///
|
|
|
|
/// Although exposed publically, this type should only really be handled by the library itself, and
|
|
|
|
/// is impossible to create outside of a
|
2018-11-18 01:51:53 +00:00
|
|
|
/// [Processor](https://docs.rs/background-jobs/0.1.1/background_jobs/struct.Processor)'s new_job
|
2018-11-18 01:39:04 +00:00
|
|
|
/// method.
|
2018-11-08 02:20:30 +00:00
|
|
|
pub struct JobInfo {
|
|
|
|
/// ID of the job, None means an ID has not been set
|
|
|
|
id: Option<usize>,
|
|
|
|
|
|
|
|
/// Name of the processor that should handle this job
|
|
|
|
processor: String,
|
|
|
|
|
2018-11-11 03:15:21 +00:00
|
|
|
/// Name of the queue that this job is a part of
|
|
|
|
queue: String,
|
|
|
|
|
2018-11-08 02:20:30 +00:00
|
|
|
/// Arguments for a given job
|
|
|
|
args: Value,
|
|
|
|
|
|
|
|
/// Status of the job
|
|
|
|
status: JobStatus,
|
|
|
|
|
|
|
|
/// Retries left for this job, None means no limit
|
|
|
|
retry_count: u32,
|
|
|
|
|
|
|
|
/// the initial MaxRetries value, for comparing to the current retry count
|
|
|
|
max_retries: MaxRetries,
|
|
|
|
|
|
|
|
/// How often retries should be scheduled
|
|
|
|
backoff_strategy: Backoff,
|
|
|
|
|
2018-11-11 03:15:21 +00:00
|
|
|
/// The time this job should be dequeued
|
2018-11-08 02:20:30 +00:00
|
|
|
next_queue: Option<DateTime<Utc>>,
|
2018-11-11 03:15:21 +00:00
|
|
|
|
|
|
|
/// The time this job was last updated
|
|
|
|
updated_at: DateTime<Utc>,
|
2018-11-08 02:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JobInfo {
|
|
|
|
pub(crate) fn new(
|
|
|
|
processor: String,
|
2018-11-11 03:15:21 +00:00
|
|
|
queue: String,
|
2018-11-08 02:20:30 +00:00
|
|
|
args: Value,
|
|
|
|
max_retries: MaxRetries,
|
|
|
|
backoff_strategy: Backoff,
|
|
|
|
) -> Self {
|
|
|
|
JobInfo {
|
|
|
|
id: None,
|
|
|
|
processor,
|
2018-11-11 03:15:21 +00:00
|
|
|
queue,
|
2018-11-08 02:20:30 +00:00
|
|
|
status: JobStatus::Pending,
|
|
|
|
args,
|
|
|
|
retry_count: 0,
|
|
|
|
max_retries,
|
|
|
|
next_queue: None,
|
|
|
|
backoff_strategy,
|
2018-11-11 03:15:21 +00:00
|
|
|
updated_at: Utc::now(),
|
2018-11-08 02:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-11 03:15:21 +00:00
|
|
|
|
|
|
|
pub(crate) fn updated(&mut self) {
|
|
|
|
self.updated_at = Utc::now();
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:20:30 +00:00
|
|
|
pub(crate) fn processor(&self) -> &str {
|
|
|
|
&self.processor
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn args(&self) -> Value {
|
|
|
|
self.args.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn status(&self) -> JobStatus {
|
|
|
|
self.status.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn id(&self) -> Option<usize> {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn set_id(&mut self, id: usize) {
|
|
|
|
if self.id.is_none() {
|
|
|
|
self.id = Some(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn increment(&mut self) -> ShouldStop {
|
|
|
|
self.retry_count += 1;
|
|
|
|
self.max_retries.compare(self.retry_count)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn next_queue(&mut self) {
|
|
|
|
let now = Utc::now();
|
|
|
|
|
|
|
|
let next_queue = match self.backoff_strategy {
|
|
|
|
Backoff::Linear(secs) => now + OldDuration::seconds(secs as i64),
|
|
|
|
Backoff::Exponential(base) => {
|
|
|
|
let secs = base.pow(self.retry_count);
|
|
|
|
now + OldDuration::seconds(secs as i64)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.next_queue = Some(next_queue);
|
|
|
|
}
|
|
|
|
|
2018-11-11 03:15:21 +00:00
|
|
|
pub(crate) fn is_stale(&self) -> bool {
|
|
|
|
self.updated_at < Utc::now() - OldDuration::days(1)
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:20:30 +00:00
|
|
|
pub(crate) fn is_ready(&self, now: DateTime<Utc>) -> bool {
|
|
|
|
match self.next_queue {
|
|
|
|
Some(ref time) => now > *time,
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn is_failed(&self) -> bool {
|
|
|
|
self.status == JobStatus::Failed
|
|
|
|
}
|
|
|
|
|
2018-11-11 03:15:21 +00:00
|
|
|
pub(crate) fn is_in_queue(&self, queue: &str) -> bool {
|
|
|
|
self.queue == queue
|
|
|
|
}
|
|
|
|
|
2018-11-17 01:10:31 +00:00
|
|
|
pub(crate) fn stage(&mut self) {
|
|
|
|
self.status = JobStatus::Staged;
|
|
|
|
}
|
|
|
|
|
2018-11-18 01:39:04 +00:00
|
|
|
/// This method sets the Job's status to running
|
|
|
|
///
|
|
|
|
/// Touching this outside of the background_jobs crates is dangerous, since these libraries
|
|
|
|
/// rely on the state of the job being correct.
|
|
|
|
pub fn set_running(&mut self) {
|
2018-11-17 01:10:31 +00:00
|
|
|
self.status = JobStatus::Running;
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:20:30 +00:00
|
|
|
pub(crate) fn pending(&mut self) {
|
|
|
|
self.status = JobStatus::Pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn fail(&mut self) {
|
|
|
|
self.status = JobStatus::Failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn pass(&mut self) {
|
|
|
|
self.status = JobStatus::Finished;
|
|
|
|
}
|
|
|
|
}
|