redis_queue/manager/
dispatch.rs

1//! Queue task dispatcher
2
3use redis::FromRedisValue;
4
5use crate::types::Entry;
6
7use core::future::Future;
8
9#[derive(Debug, PartialEq, Eq, Clone, Copy)]
10///Possible results of task processing
11pub enum TaskResultKind {
12    ///Task is not recognized or cannot be parsed
13    Invalid,
14    ///Temporary failure prevents task execution
15    ///
16    ///Should re-try
17    TempFail,
18    ///Task failed in a way that makes re-try impossible
19    Failure,
20    ///All good
21    Success,
22}
23
24///Result of task dispatch
25///
26///Type parameter `T` is payload within redis queue `Entry`
27pub struct TaskResult<T> {
28    ///Task message entry.
29    ///
30    ///Returned as it was passed
31    pub data: Entry<T>,
32    ///Operation result
33    pub kind: TaskResultKind,
34}
35
36impl TaskResultKind {
37    ///Returns whether re-try is necessary
38    pub const fn is_need_retry(&self) -> bool {
39        match self {
40            Self::TempFail => true,
41            _ => false,
42        }
43    }
44}
45
46///Interface to dispatch raw message with task information
47pub trait Dispatch {
48    ///Payload type to use.
49    type PayloadType: FromRedisValue;
50    ///Future that performs send.
51    type Future: Future<Output = TaskResult<Self::PayloadType>>;
52
53    ///Starts send.
54    fn send(&self, data: Entry<Self::PayloadType>) -> Self::Future;
55}
56
57impl<T: Dispatch> Dispatch for std::sync::Arc<T> {
58    type PayloadType = T::PayloadType;
59    type Future = T::Future;
60
61    #[inline(always)]
62    fn send(&self, data: Entry<Self::PayloadType>) -> Self::Future {
63        T::send(self, data)
64    }
65}
66
67impl<T: Dispatch> Dispatch for std::rc::Rc<T> {
68    type PayloadType = T::PayloadType;
69    type Future = T::Future;
70
71    #[inline(always)]
72    fn send(&self, data: Entry<Self::PayloadType>) -> Self::Future {
73        T::send(self, data)
74    }
75}
76
77impl<T: Dispatch> Dispatch for Box<T> {
78    type PayloadType = T::PayloadType;
79    type Future = T::Future;
80
81    #[inline(always)]
82    fn send(&self, data: Entry<Self::PayloadType>) -> Self::Future {
83        T::send(self, data)
84    }
85}