background-jobs/jobs-actix/src/storage.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

2020-03-21 02:31:03 +00:00
use anyhow::Error;
use background_jobs_core::{JobInfo, NewJobInfo, ReturnJobInfo, Storage};
2020-03-22 17:52:43 +00:00
use uuid::Uuid;
2019-05-27 17:29:11 +00:00
2020-03-21 02:31:03 +00:00
#[async_trait::async_trait]
2019-05-27 17:29:11 +00:00
pub(crate) trait ActixStorage {
2024-01-08 00:52:09 +00:00
async fn push(&self, job: NewJobInfo) -> Result<Uuid, Error>;
2019-05-27 17:29:11 +00:00
2024-01-08 00:52:09 +00:00
async fn pop(&self, queue: &str, runner_id: Uuid) -> Result<JobInfo, Error>;
2019-05-27 17:29:11 +00:00
2024-01-08 00:52:09 +00:00
async fn heartbeat(&self, job_id: Uuid, runner_id: Uuid) -> Result<(), Error>;
async fn complete(&self, ret: ReturnJobInfo) -> Result<(), Error>;
2019-05-27 17:29:11 +00:00
}
2019-09-17 22:49:45 +00:00
pub(crate) struct StorageWrapper<S>(pub(crate) S)
2019-05-27 17:29:11 +00:00
where
2020-03-21 02:31:03 +00:00
S: Storage + Send + Sync,
S::Error: Send + Sync + 'static;
2019-05-27 17:29:11 +00:00
2020-03-21 02:31:03 +00:00
#[async_trait::async_trait]
2019-09-17 22:49:45 +00:00
impl<S> ActixStorage for StorageWrapper<S>
2019-05-27 17:29:11 +00:00
where
2020-03-21 02:31:03 +00:00
S: Storage + Send + Sync,
S::Error: Send + Sync + 'static,
2019-05-27 17:29:11 +00:00
{
2024-01-08 00:52:09 +00:00
async fn push(&self, job: NewJobInfo) -> Result<Uuid, Error> {
Ok(self.0.push(job).await?)
}
async fn pop(&self, queue: &str, runner_id: Uuid) -> Result<JobInfo, Error> {
Ok(self.0.pop(queue, runner_id).await?)
2019-05-27 17:29:11 +00:00
}
2024-01-08 00:52:09 +00:00
async fn heartbeat(&self, job_id: Uuid, runner_id: Uuid) -> Result<(), Error> {
Ok(self.0.heartbeat(job_id, runner_id).await?)
2019-05-27 17:29:11 +00:00
}
2024-01-08 00:52:09 +00:00
async fn complete(&self, ret: ReturnJobInfo) -> Result<(), Error> {
2024-01-08 22:28:18 +00:00
self.0.complete(ret).await?;
Ok(())
2019-05-27 17:29:11 +00:00
}
}