backie/src/queue.rs

44 lines
850 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::time::Duration;
2023-03-04 18:07:17 +00:00
2023-03-11 16:49:23 +00:00
pub struct Queue<S>
where
2023-03-14 14:06:22 +00:00
S: TaskStore,
2023-03-11 16:49:23 +00:00
{
2023-03-14 14:06:22 +00:00
task_store: S,
}
2023-03-04 18:07:17 +00:00
2023-03-11 16:49:23 +00:00
impl<S> Queue<S>
where
2023-03-14 14:06:22 +00:00
S: TaskStore,
2023-03-11 16:49:23 +00:00
{
2023-03-13 13:11:19 +00:00
pub fn new(task_store: S) -> Self {
2023-03-14 14:06:22 +00:00
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,
{
2023-03-14 14:06:22 +00:00
// TODO: Add option to specify the timeout of a task
self.task_store
.create_task(NewTask::new(background_task, Duration::from_secs(10))?)
.await?;
Ok(())
}
2023-03-04 18:07:17 +00:00
}
2023-03-14 14:06:22 +00:00
impl<S> Clone for Queue<S>
where
S: TaskStore + Clone,
{
fn clone(&self) -> Self {
Self {
task_store: self.task_store.clone(),
}
}
}