apalis_core/storage/
mod.rs

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