apalis_core/storage/
mod.rs

1use std::time::Duration;
2
3use futures::Future;
4
5use crate::{
6    backend::Backend,
7    request::{Parts, Request},
8    task::task_id::TaskId,
9};
10
11/// Represents a [Storage] that can persist a request.
12pub trait Storage: Backend<Request<Self::Job, Self::Context>> {
13    /// The type of job that can be persisted
14    type Job;
15
16    /// The error produced by the storage
17    type Error;
18
19    /// This is the type that storages store as the metadata related to a job
20    type Context: Default;
21
22    /// The format that the storage persists the jobs usually `Vec<u8>`
23    type Compact;
24
25    /// Pushes a job to a storage
26    fn push(
27        &mut self,
28        job: Self::Job,
29    ) -> impl Future<Output = Result<Parts<Self::Context>, Self::Error>> + Send {
30        self.push_request(Request::new(job))
31    }
32
33    /// Pushes a constructed request to a storage
34    fn push_request(
35        &mut self,
36        req: Request<Self::Job, Self::Context>,
37    ) -> impl Future<Output = Result<Parts<Self::Context>, Self::Error>> + Send;
38
39    /// Pushes a constructed request to a storage
40    fn push_raw_request(
41        &mut self,
42        req: Request<Self::Compact, Self::Context>,
43    ) -> impl Future<Output = Result<Parts<Self::Context>, Self::Error>> + Send;
44
45    /// Push a job with defaults into the scheduled set
46    fn schedule(
47        &mut self,
48        job: Self::Job,
49        on: i64,
50    ) -> impl Future<Output = Result<Parts<Self::Context>, Self::Error>> + Send {
51        self.schedule_request(Request::new(job), on)
52    }
53
54    /// Push a request into the scheduled set
55    fn schedule_request(
56        &mut self,
57        request: Request<Self::Job, Self::Context>,
58        on: i64,
59    ) -> impl Future<Output = Result<Parts<Self::Context>, Self::Error>> + Send;
60
61    /// Return the number of pending jobs from the queue
62    fn len(&mut self) -> impl Future<Output = Result<i64, Self::Error>> + Send;
63
64    /// Fetch a job given an id
65    fn fetch_by_id(
66        &mut self,
67        job_id: &TaskId,
68    ) -> impl Future<Output = Result<Option<Request<Self::Job, Self::Context>>, Self::Error>> + Send;
69
70    /// Update a job details
71    fn update(
72        &mut self,
73        job: Request<Self::Job, Self::Context>,
74    ) -> impl Future<Output = Result<(), Self::Error>> + Send;
75
76    /// Reschedule a job
77    fn reschedule(
78        &mut self,
79        job: Request<Self::Job, Self::Context>,
80        wait: Duration,
81    ) -> impl Future<Output = Result<(), Self::Error>> + Send;
82
83    /// Returns true if there is no jobs in the storage
84    fn is_empty(&mut self) -> impl Future<Output = Result<bool, Self::Error>> + Send;
85
86    /// Vacuum the storage, removes done and killed jobs
87    fn vacuum(&mut self) -> impl Future<Output = Result<usize, Self::Error>> + Send;
88}