2020-03-21 02:31:03 +00:00
|
|
|
use anyhow::Error;
|
2019-05-27 17:29:11 +00:00
|
|
|
use background_jobs_core::{JobInfo, NewJobInfo, ReturnJobInfo, Stats, Storage};
|
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
#[async_trait::async_trait]
|
2019-05-27 17:29:11 +00:00
|
|
|
pub(crate) trait ActixStorage {
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn new_job(&self, job: NewJobInfo) -> Result<u64, Error>;
|
2019-05-27 17:29:11 +00:00
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn request_job(&self, queue: &str, runner_id: u64) -> Result<Option<JobInfo>, Error>;
|
2019-05-27 17:29:11 +00:00
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn return_job(&self, ret: ReturnJobInfo) -> Result<(), Error>;
|
2019-05-27 17:29:11 +00:00
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn get_stats(&self) -> Result<Stats, 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
|
|
|
{
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn new_job(&self, job: NewJobInfo) -> Result<u64, Error> {
|
|
|
|
Ok(self.0.new_job(job).await?)
|
2019-05-27 17:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn request_job(&self, queue: &str, runner_id: u64) -> Result<Option<JobInfo>, Error> {
|
|
|
|
Ok(self.0.request_job(queue, runner_id).await?)
|
2019-05-27 17:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn return_job(&self, ret: ReturnJobInfo) -> Result<(), Error> {
|
|
|
|
Ok(self.0.return_job(ret).await?)
|
2019-05-27 17:29:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
async fn get_stats(&self) -> Result<Stats, Error> {
|
|
|
|
Ok(self.0.get_stats().await?)
|
2019-05-27 17:29:11 +00:00
|
|
|
}
|
|
|
|
}
|