2024-01-08 00:52:09 +00:00
|
|
|
use std::future::Future;
|
|
|
|
|
2020-03-30 15:36:49 +00:00
|
|
|
use anyhow::Error;
|
2024-01-08 00:52:09 +00:00
|
|
|
use background_jobs_core::{Backoff, JoinError, MaxRetries, UnsendJob, UnsendSpawner};
|
2020-03-30 15:36:49 +00:00
|
|
|
use serde::{de::DeserializeOwned, ser::Serialize};
|
2021-10-11 23:49:39 +00:00
|
|
|
use tracing::Span;
|
2020-03-30 15:36:49 +00:00
|
|
|
|
2024-01-08 00:52:09 +00:00
|
|
|
pub struct ActixSpawner;
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct ActixHandle<T>(actix_rt::task::JoinHandle<T>);
|
|
|
|
|
|
|
|
impl UnsendSpawner for ActixSpawner {
|
|
|
|
type Handle<T> = ActixHandle<T> where T: Send;
|
|
|
|
|
|
|
|
fn spawn<Fut>(future: Fut) -> Self::Handle<Fut::Output>
|
|
|
|
where
|
|
|
|
Fut: Future + 'static,
|
|
|
|
Fut::Output: Send + 'static,
|
|
|
|
{
|
|
|
|
ActixHandle(actix_rt::spawn(future))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Unpin for ActixHandle<T> {}
|
|
|
|
|
|
|
|
impl<T> Future for ActixHandle<T> {
|
|
|
|
type Output = Result<T, JoinError>;
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
mut self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Self::Output> {
|
|
|
|
let res = std::task::ready!(std::pin::Pin::new(&mut self.0).poll(cx));
|
|
|
|
|
|
|
|
std::task::Poll::Ready(res.map_err(|_| JoinError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for ActixHandle<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.0.abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The UnsendJob trait defines parameters pertaining to an instance of a background job
|
2020-03-30 15:36:49 +00:00
|
|
|
///
|
2024-01-08 00:52:09 +00:00
|
|
|
/// This trait is used to implement generic Unsend Jobs in the background jobs library. It requires
|
|
|
|
/// that implementors specify a spawning mechanism that can turn an Unsend future into a Send
|
|
|
|
/// future
|
2024-01-08 00:54:14 +00:00
|
|
|
pub trait ActixJob: Serialize + DeserializeOwned + 'static {
|
2020-03-30 15:36:49 +00:00
|
|
|
/// The application state provided to this job at runtime.
|
|
|
|
type State: Clone + 'static;
|
|
|
|
|
|
|
|
/// The future returned by this job
|
2020-04-21 00:30:56 +00:00
|
|
|
///
|
|
|
|
/// Importantly, this Future does not require Send
|
2020-03-30 15:36:49 +00:00
|
|
|
type Future: Future<Output = Result<(), Error>>;
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
/// The name of the job
|
|
|
|
///
|
|
|
|
/// This name must be unique!!!
|
|
|
|
const NAME: &'static str;
|
|
|
|
|
|
|
|
/// The name of the default queue for this job
|
|
|
|
///
|
|
|
|
/// This can be overridden on an individual-job level, but if a non-existant queue is supplied,
|
|
|
|
/// the job will never be processed.
|
|
|
|
const QUEUE: &'static str = "default";
|
|
|
|
|
|
|
|
/// Define the default number of retries for this job
|
|
|
|
///
|
|
|
|
/// Defaults to Count(5)
|
|
|
|
/// Jobs can override
|
|
|
|
const MAX_RETRIES: MaxRetries = MaxRetries::Count(5);
|
|
|
|
|
|
|
|
/// Define the default backoff strategy for this job
|
|
|
|
///
|
|
|
|
/// Defaults to Exponential(2)
|
|
|
|
/// Jobs can override
|
|
|
|
const BACKOFF: Backoff = Backoff::Exponential(2);
|
|
|
|
|
|
|
|
/// Define the maximum number of milliseconds a job should be allowed to run before being
|
|
|
|
/// considered dead.
|
|
|
|
///
|
|
|
|
/// This is important for allowing the job server to reap processes that were started but never
|
|
|
|
/// completed.
|
|
|
|
///
|
|
|
|
/// Defaults to 15 seconds
|
|
|
|
/// Jobs can override
|
|
|
|
const TIMEOUT: i64 = 15_000;
|
|
|
|
|
2020-03-30 15:36:49 +00:00
|
|
|
/// Users of this library must define what it means to run a job.
|
|
|
|
///
|
|
|
|
/// This should contain all the logic needed to complete a job. If that means queuing more
|
|
|
|
/// jobs, sending an email, shelling out (don't shell out), or doing otherwise lengthy
|
|
|
|
/// processes, that logic should all be called from inside this method.
|
|
|
|
///
|
|
|
|
/// The state passed into this job is initialized at the start of the application. The state
|
|
|
|
/// argument could be useful for containing a hook into something like r2d2, or the address of
|
|
|
|
/// an actor in an actix-based system.
|
|
|
|
fn run(self, state: Self::State) -> Self::Future;
|
|
|
|
|
2021-09-21 15:32:48 +00:00
|
|
|
/// Generate a Span that the job will be processed within
|
|
|
|
fn span(&self) -> Option<Span> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
/// If this job should not use it's default queue, this can be overridden in
|
2020-03-30 15:36:49 +00:00
|
|
|
/// user-code.
|
2020-04-21 00:30:56 +00:00
|
|
|
fn queue(&self) -> &str {
|
|
|
|
Self::QUEUE
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
/// If this job should not use it's default maximum retry count, this can be
|
2020-03-30 15:36:49 +00:00
|
|
|
/// overridden in user-code.
|
2020-04-21 00:30:56 +00:00
|
|
|
fn max_retries(&self) -> MaxRetries {
|
|
|
|
Self::MAX_RETRIES
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
/// If this job should not use it's default backoff strategy, this can be
|
2020-03-30 15:36:49 +00:00
|
|
|
/// overridden in user-code.
|
2020-04-21 00:30:56 +00:00
|
|
|
fn backoff_strategy(&self) -> Backoff {
|
|
|
|
Self::BACKOFF
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Define the maximum number of milliseconds this job should be allowed to run before being
|
|
|
|
/// considered dead.
|
|
|
|
///
|
|
|
|
/// This is important for allowing the job server to reap processes that were started but never
|
|
|
|
/// completed.
|
2020-04-21 00:30:56 +00:00
|
|
|
fn timeout(&self) -> i64 {
|
|
|
|
Self::TIMEOUT
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 00:52:09 +00:00
|
|
|
/// Provide helper methods for queuing ActixJobs
|
|
|
|
pub trait ActixJobExt: ActixJob {
|
|
|
|
/// Turn an ActixJob into a type that implements Job
|
|
|
|
fn into_job(self) -> ActixJobWrapper<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
ActixJobWrapper(self)
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 23:49:39 +00:00
|
|
|
|
2024-01-08 00:52:09 +00:00
|
|
|
impl<T> ActixJobExt for T where T: ActixJob {}
|
|
|
|
|
|
|
|
impl<T> From<T> for ActixJobWrapper<T>
|
2021-10-11 23:49:39 +00:00
|
|
|
where
|
2024-01-08 00:52:09 +00:00
|
|
|
T: ActixJob,
|
2021-10-11 23:49:39 +00:00
|
|
|
{
|
2024-01-08 00:52:09 +00:00
|
|
|
fn from(value: T) -> Self {
|
|
|
|
ActixJobWrapper(value)
|
2021-10-11 23:49:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 00:52:09 +00:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
// A wrapper for ActixJob implementing UnsendJob with an ActixSpawner
|
|
|
|
pub struct ActixJobWrapper<T>(T);
|
|
|
|
|
|
|
|
impl<T> UnsendJob for ActixJobWrapper<T>
|
2020-03-30 15:36:49 +00:00
|
|
|
where
|
2024-01-08 00:52:09 +00:00
|
|
|
T: ActixJob,
|
2020-03-30 15:36:49 +00:00
|
|
|
{
|
2024-01-08 00:52:09 +00:00
|
|
|
type State = <T as ActixJob>::State;
|
2020-03-30 15:36:49 +00:00
|
|
|
|
2024-01-08 00:52:09 +00:00
|
|
|
type Future = <T as ActixJob>::Future;
|
2020-04-21 00:30:56 +00:00
|
|
|
|
2024-01-08 00:52:09 +00:00
|
|
|
type Spawner = ActixSpawner;
|
|
|
|
|
|
|
|
const NAME: &'static str = <T as ActixJob>::NAME;
|
|
|
|
const QUEUE: &'static str = <T as ActixJob>::QUEUE;
|
|
|
|
const MAX_RETRIES: MaxRetries = <T as ActixJob>::MAX_RETRIES;
|
|
|
|
const BACKOFF: Backoff = <T as ActixJob>::BACKOFF;
|
|
|
|
const TIMEOUT: i64 = <T as ActixJob>::TIMEOUT;
|
2021-10-11 23:49:39 +00:00
|
|
|
|
2024-01-08 00:52:09 +00:00
|
|
|
fn run(self, state: Self::State) -> Self::Future {
|
|
|
|
<T as ActixJob>::run(self.0, state)
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 15:32:48 +00:00
|
|
|
fn span(&self) -> Option<Span> {
|
2024-01-08 00:52:09 +00:00
|
|
|
self.0.span()
|
2021-09-21 15:32:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
fn queue(&self) -> &str {
|
2024-01-08 00:52:09 +00:00
|
|
|
self.0.queue()
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
fn max_retries(&self) -> MaxRetries {
|
2024-01-08 00:52:09 +00:00
|
|
|
self.0.max_retries()
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
fn backoff_strategy(&self) -> Backoff {
|
2024-01-08 00:52:09 +00:00
|
|
|
self.0.backoff_strategy()
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
fn timeout(&self) -> i64 {
|
2024-01-08 00:52:09 +00:00
|
|
|
self.0.timeout()
|
2020-03-30 15:36:49 +00:00
|
|
|
}
|
|
|
|
}
|