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

213 lines
5.8 KiB
Rust
Raw Normal View History

2024-01-08 00:52:09 +00:00
use crate::{Backoff, JobResult, MaxRetries, ShouldStop};
2018-11-08 02:20:30 +00:00
use serde_json::Value;
use std::time::SystemTime;
use time::{Duration, OffsetDateTime};
2024-01-08 00:52:09 +00:00
use uuid::{NoContext, Timestamp, Uuid};
2018-11-08 02:20:30 +00:00
2022-11-18 05:55:29 +00:00
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
2019-09-22 17:12:08 +00:00
/// Information about the sate of an attempted job
pub struct ReturnJobInfo {
/// The ID of the job being returned
pub id: Uuid,
/// The result status of the job
pub 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
2022-11-18 05:55:29 +00:00
#[derive(Clone, Debug, PartialEq, Eq, 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<OffsetDateTime>,
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
2024-01-10 05:16:35 +00:00
timeout: u64,
2018-12-16 18:43:44 +00:00
}
impl NewJobInfo {
pub(crate) fn schedule(&mut self, time: SystemTime) {
self.next_queue = Some(time.into());
2018-12-16 18:43:44 +00:00
}
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,
2024-01-10 05:16:35 +00:00
timeout: u64,
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
}
/// The name of this job
pub fn name(&self) -> &str {
&self.name
}
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()
}
/// Construct a JobInfo from a NewJobInfo
pub fn build(self) -> JobInfo {
2018-12-16 18:43:44 +00:00
JobInfo {
2024-01-08 00:52:09 +00:00
id: Uuid::now_v7(),
2020-04-21 00:30:56 +00:00
name: self.name,
2018-12-16 18:43:44 +00:00
queue: self.queue,
args: self.args,
retry_count: 0,
max_retries: self.max_retries,
2024-01-08 00:52:09 +00:00
next_queue: self.next_queue.unwrap_or(OffsetDateTime::now_utc()),
2018-12-16 18:43:44 +00:00
backoff_strategy: self.backoff_strategy,
2020-03-21 03:04:23 +00:00
timeout: self.timeout,
2018-12-16 18:43:44 +00:00
}
}
}
2024-01-08 00:52:09 +00:00
fn uuid_from_timestamp(timestamp: OffsetDateTime) -> Uuid {
let unix_seconds = timestamp.unix_timestamp().abs_diff(0);
let unix_nanos = (timestamp.unix_timestamp_nanos() % i128::from(timestamp.unix_timestamp()))
.abs_diff(0) as u32;
Uuid::new_v7(Timestamp::from_unix(NoContext, unix_seconds, unix_nanos))
}
2022-11-18 05:55:29 +00:00
#[derive(Clone, Debug, PartialEq, Eq, 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
2024-01-08 00:52:09 +00:00
pub id: Uuid,
2018-11-08 02:20:30 +00:00
2020-04-21 00:30:56 +00:00
/// Name of the job
2024-01-08 00:52:09 +00:00
pub 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
2024-01-08 00:52:09 +00:00
pub queue: String,
2018-11-11 03:15:21 +00:00
2018-11-08 02:20:30 +00:00
/// Arguments for a given job
2024-01-08 00:52:09 +00:00
pub args: Value,
2018-11-08 02:20:30 +00:00
/// Retries left for this job, None means no limit
2024-01-08 00:52:09 +00:00
pub retry_count: u32,
2018-11-08 02:20:30 +00:00
/// the initial MaxRetries value, for comparing to the current retry count
2024-01-08 00:52:09 +00:00
pub max_retries: MaxRetries,
2018-11-08 02:20:30 +00:00
/// How often retries should be scheduled
2024-01-08 00:52:09 +00:00
pub backoff_strategy: Backoff,
2018-11-08 02:20:30 +00:00
2018-11-11 03:15:21 +00:00
/// The time this job should be dequeued
2024-01-08 00:52:09 +00:00
pub next_queue: OffsetDateTime,
2018-11-11 03:15:21 +00:00
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
2024-01-10 05:16:35 +00:00
pub timeout: u64,
2018-11-08 02:20:30 +00:00
}
impl JobInfo {
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,
}
}
2024-01-08 00:52:09 +00:00
/// Produce a UUID from the next_queue timestamp
pub fn next_queue_id(&self) -> Uuid {
uuid_from_timestamp(self.next_queue)
2020-03-22 21:04:49 +00:00
}
2024-01-08 00:52:09 +00:00
// Increment the retry-count and determine if the job should be requeued
fn increment(&mut self) -> ShouldStop {
2018-11-08 02:20:30 +00:00
self.retry_count += 1;
self.max_retries.compare(self.retry_count)
}
2024-01-08 00:52:09 +00:00
/// Update the timestamp on the JobInfo to reflect the next queue time
2020-03-22 21:04:49 +00:00
fn set_next_queue(&mut self) {
let now = OffsetDateTime::now_utc();
2018-11-08 02:20:30 +00:00
2024-01-08 00:52:09 +00:00
self.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
}
};
2024-01-08 00:52:09 +00:00
tracing::trace!("Now {}, Next queue {}", now, self.next_queue);
2018-11-08 02:20:30 +00:00
}
2024-01-08 00:52:09 +00:00
/// Increment the retry-count and set next_queue based on the job's configuration
///
/// returns `true` if the job should be retried
pub fn prepare_retry(&mut self) -> bool {
let should_retry = self.increment().should_requeue();
2018-12-16 18:43:44 +00:00
if should_retry {
2020-03-22 21:04:49 +00:00
self.set_next_queue();
2018-12-16 18:43:44 +00:00
}
should_retry
}
2018-11-08 02:20:30 +00:00
}