backie/src/queue.rs

34 lines
662 B
Rust
Raw Normal View History

2023-03-11 15:38:32 +00:00
use crate::errors::BackieError;
use crate::runnable::BackgroundTask;
2023-03-11 16:49:23 +00:00
use crate::store::TaskStore;
use crate::task::NewTask;
use std::sync::Arc;
use std::time::Duration;
2023-03-04 18:07:17 +00:00
#[derive(Clone)]
2023-03-11 16:49:23 +00:00
pub struct Queue<S>
where
S: TaskStore,
{
task_store: Arc<S>,
}
2023-03-04 18:07:17 +00:00
2023-03-11 16:49:23 +00:00
impl<S> Queue<S>
where
S: TaskStore,
{
2023-03-11 21:22:25 +00:00
pub fn new(task_store: Arc<S>) -> Self {
Queue { task_store }
}
2023-03-04 18:07:17 +00:00
2023-03-11 15:38:32 +00:00
pub async fn enqueue<BT>(&self, background_task: BT) -> Result<(), BackieError>
where
BT: BackgroundTask,
{
self.task_store
.create_task(NewTask::new(background_task, Duration::from_secs(10))?)
.await?;
Ok(())
}
2023-03-04 18:07:17 +00:00
}