Skip to main content

capp_queue/
queue.rs

1//! This module provides a trait for interacting with task storage.
2//! The storage allows tasks to be pushed to and popped from a queue,
3//! and also allows tasks to be set and retrieved by their UUID.
4
5use async_trait::async_trait;
6use serde::{Serialize, de::DeserializeOwned};
7use std::{fmt::Debug, hash::Hash, sync::Arc};
8
9use super::TaskQueueError;
10use crate::task::{Task, TaskId};
11
12#[async_trait]
13pub trait TaskQueue<Data>
14where
15    Data: Debug + Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
16{
17    async fn push(&self, task: &Task<Data>) -> Result<(), TaskQueueError>;
18    async fn pop(&self) -> Result<Task<Data>, TaskQueueError>;
19    async fn ack(&self, task_id: &TaskId) -> Result<(), TaskQueueError>;
20    async fn nack(&self, task: &Task<Data>) -> Result<(), TaskQueueError>;
21    async fn set(&self, task: &Task<Data>) -> Result<(), TaskQueueError>;
22
23    /// Move any tasks left in the in-flight set back to the queue. Called once
24    /// at runtime startup to recover work owned by a previous process that
25    /// crashed before acking. Returns the number of tasks recovered.
26    async fn recover_inflight(&self) -> Result<u64, TaskQueueError> {
27        Ok(0)
28    }
29}
30
31pub type AbstractTaskQueue<D> = Arc<dyn TaskQueue<D> + Send + Sync>;
32
33// Trait used for round-robin queues
34pub trait HasTagKey {
35    type TagValue: Hash + Eq + Clone + ToString + Send + Sync + 'static;
36    fn get_tag_value(&self) -> Self::TagValue;
37}