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

275 lines
6.9 KiB
Rust
Raw Normal View History

2020-03-21 02:31:03 +00:00
use crate::{Backoff, JobResult, JobStatus, MaxRetries, ShouldStop};
2020-03-21 03:04:23 +00:00
use chrono::{offset::Utc, DateTime, Duration};
2018-12-16 18:43:44 +00:00
use log::trace;
2018-11-08 02:20:30 +00:00
use serde_json::Value;
2020-03-22 17:52:43 +00:00
use uuid::Uuid;
2018-11-08 02:20:30 +00:00
2020-03-21 02:31:03 +00:00
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
2019-09-22 17:12:08 +00:00
/// Information about the sate of an attempted job
pub struct ReturnJobInfo {
2020-03-22 17:52:43 +00:00
pub(crate) id: Uuid,
pub(crate) result: JobResult,
}
impl ReturnJobInfo {
2020-03-22 17:52:43 +00:00
pub(crate) fn fail(id: Uuid) -> Self {
ReturnJobInfo {
id,
result: JobResult::Failure,
}
}
2020-03-22 17:52:43 +00:00
pub(crate) fn pass(id: Uuid) -> Self {
ReturnJobInfo {
id,
result: JobResult::Success,
}
}
2020-04-21 00:30:56 +00:00
pub(crate) fn unregistered(id: Uuid) -> Self {
ReturnJobInfo {
id,
2020-04-21 00:30:56 +00:00
result: JobResult::Unregistered,
}
}
}
2018-11-08 02:20:30 +00:00
2020-03-21 02:31:03 +00:00
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
2019-09-22 17:12:08 +00:00
/// Information about a newly created job
2018-12-16 18:43:44 +00:00
pub struct NewJobInfo {
2020-04-21 00:30:56 +00:00
/// Name of the job
name: String,
2018-12-16 18:43:44 +00:00
/// Name of the queue that this job is a part of
queue: String,
/// Arguments for a given job
args: Value,
/// the initial MaxRetries value, for comparing to the current retry count
max_retries: MaxRetries,
/// How often retries should be scheduled
backoff_strategy: Backoff,
/// The time this job should be dequeued
next_queue: Option<DateTime<Utc>>,
2020-03-21 03:04:23 +00:00
/// Milliseconds from execution until the job is considered dead
///
/// This is important for storage implementations to reap unfinished jobs
timeout: i64,
2018-12-16 18:43:44 +00:00
}
impl NewJobInfo {
pub(crate) fn schedule(&mut self, time: DateTime<Utc>) {
self.next_queue = Some(time);
}
pub(crate) fn new(
2020-04-21 00:30:56 +00:00
name: String,
2018-12-16 18:43:44 +00:00
queue: String,
max_retries: MaxRetries,
backoff_strategy: Backoff,
2020-03-21 03:04:23 +00:00
timeout: i64,
2020-04-21 00:30:56 +00:00
args: Value,
2018-12-16 18:43:44 +00:00
) -> Self {
NewJobInfo {
2020-04-21 00:30:56 +00:00
name,
2018-12-16 18:43:44 +00:00
queue,
args,
max_retries,
next_queue: None,
backoff_strategy,
2020-03-21 03:04:23 +00:00
timeout,
2018-12-16 18:43:44 +00:00
}
}
2019-09-22 17:12:08 +00:00
/// The name of the queue this job will run in
pub fn queue(&self) -> &str {
&self.queue
}
2019-09-22 17:12:08 +00:00
/// Whether this job is ready to be run immediately
pub fn is_ready(&self) -> bool {
self.next_queue.is_none()
}
2020-03-22 17:52:43 +00:00
pub(crate) fn with_id(self, id: Uuid) -> JobInfo {
2018-12-16 18:43:44 +00:00
JobInfo {
id,
2020-04-21 00:30:56 +00:00
name: self.name,
2018-12-16 18:43:44 +00:00
queue: self.queue,
status: JobStatus::Pending,
args: self.args,
retry_count: 0,
max_retries: self.max_retries,
next_queue: self.next_queue,
backoff_strategy: self.backoff_strategy,
updated_at: Utc::now(),
2020-03-21 03:04:23 +00:00
timeout: self.timeout,
2018-12-16 18:43:44 +00:00
}
}
}
2020-03-21 02:31:03 +00:00
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
/// 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
2020-04-21 00:30:56 +00:00
/// is impossible to create outside of the new_job method.
2018-11-08 02:20:30 +00:00
pub struct JobInfo {
2018-12-16 18:43:44 +00:00
/// ID of the job
2020-03-22 17:52:43 +00:00
id: Uuid,
2018-11-08 02:20:30 +00:00
2020-04-21 00:30:56 +00:00
/// Name of the job
name: String,
2018-11-08 02:20:30 +00:00
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>,
2020-03-21 03:04:23 +00:00
/// Milliseconds from execution until the job is considered dead
///
/// This is important for storage implementations to reap unfinished jobs
timeout: i64,
2018-11-08 02:20:30 +00:00
}
impl JobInfo {
2019-09-22 17:12:08 +00:00
/// The name of the queue this job will run in
2018-12-16 18:43:44 +00:00
pub fn queue(&self) -> &str {
&self.queue
2018-11-08 02:20:30 +00:00
}
2018-11-11 03:15:21 +00:00
fn updated(&mut self) {
2018-11-11 03:15:21 +00:00
self.updated_at = Utc::now();
}
2020-04-21 00:30:56 +00:00
pub(crate) fn name(&self) -> &str {
&self.name
2018-11-08 02:20:30 +00:00
}
pub(crate) fn args(&self) -> Value {
self.args.clone()
}
2019-09-22 17:12:08 +00:00
/// The ID of this job
2020-03-22 17:52:43 +00:00
pub fn id(&self) -> Uuid {
2018-12-16 18:43:44 +00:00
self.id
2018-11-08 02:20:30 +00:00
}
2020-03-22 21:04:49 +00:00
/// How long (in milliseconds) before this job is considered failed and can be requeued
2020-03-22 20:02:32 +00:00
pub fn timeout(&self) -> i64 {
self.timeout
}
2020-03-21 02:31:03 +00:00
/// Convert a JobInfo into a ReturnJobInfo without executing it
pub fn unexecuted(self) -> ReturnJobInfo {
ReturnJobInfo {
id: self.id,
result: JobResult::Unexecuted,
}
}
2020-03-22 21:04:49 +00:00
/// If the job is queued to run in the future, when is that
pub fn next_queue(&self) -> Option<DateTime<Utc>> {
self.next_queue
}
2018-11-08 02:20:30 +00:00
pub(crate) fn increment(&mut self) -> ShouldStop {
self.updated();
2018-11-08 02:20:30 +00:00
self.retry_count += 1;
self.max_retries.compare(self.retry_count)
}
2020-03-22 21:04:49 +00:00
fn set_next_queue(&mut self) {
2018-11-08 02:20:30 +00:00
let now = Utc::now();
let next_queue = match self.backoff_strategy {
2020-03-21 03:04:23 +00:00
Backoff::Linear(secs) => now + Duration::seconds(secs as i64),
2018-11-08 02:20:30 +00:00
Backoff::Exponential(base) => {
let secs = base.pow(self.retry_count);
2020-03-21 03:04:23 +00:00
now + Duration::seconds(secs as i64)
2018-11-08 02:20:30 +00:00
}
};
self.next_queue = Some(next_queue);
2018-12-16 18:43:44 +00:00
trace!(
"Now {}, Next queue {}, ready {}",
now,
next_queue,
self.is_ready(now),
);
}
2019-09-22 17:12:08 +00:00
/// Whether this job is ready to be run
pub fn is_ready(&self, now: DateTime<Utc>) -> bool {
2018-11-08 02:20:30 +00:00
match self.next_queue {
Some(ref time) => now > *time,
None => true,
}
}
pub(crate) fn needs_retry(&mut self) -> bool {
let should_retry = self.increment().should_requeue();
2018-12-16 18:43:44 +00:00
if should_retry {
self.pending();
2020-03-22 21:04:49 +00:00
self.set_next_queue();
2018-12-16 18:43:44 +00:00
}
should_retry
}
2019-09-22 17:12:08 +00:00
/// Whether this job is pending execution
2020-03-21 03:04:23 +00:00
pub fn is_pending(&self, now: DateTime<Utc>) -> bool {
2018-12-16 18:43:44 +00:00
self.status == JobStatus::Pending
2020-03-21 03:04:23 +00:00
|| (self.status == JobStatus::Running
&& (self.updated_at + Duration::milliseconds(self.timeout)) < now)
2018-12-16 18:43:44 +00:00
}
2020-03-22 19:59:36 +00:00
/// Get the status of the job
pub fn status(&self) -> JobStatus {
self.status.clone()
}
/// The the date of the most recent update
pub fn updated_at(&self) -> DateTime<Utc> {
self.updated_at
}
2018-11-11 03:15:21 +00:00
pub(crate) fn is_in_queue(&self, queue: &str) -> bool {
self.queue == queue
}
pub(crate) fn run(&mut self) {
self.updated();
self.status = JobStatus::Running;
}
2018-11-08 02:20:30 +00:00
pub(crate) fn pending(&mut self) {
self.updated();
2018-11-08 02:20:30 +00:00
self.status = JobStatus::Pending;
}
}