|
| 1 | +//! Queue configuration for dispatching background jobs. |
| 2 | +
|
| 3 | +use std::time::Duration; |
| 4 | + |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + queue_type::{Binding, QueueProps, RetryProps}, |
| 9 | + suffix::Suffix, |
| 10 | + Result, |
| 11 | +}; |
| 12 | + |
| 13 | +/// Message data for a job dispatch request |
| 14 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 15 | +pub enum Message { |
| 16 | + /// Refresh a table of cached data |
| 17 | + RefreshTable(String), |
| 18 | +} |
| 19 | + |
| 20 | +/// AMQP configuration for job runners |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct QueueType { |
| 23 | + props: QueueProps, |
| 24 | +} |
| 25 | + |
| 26 | +impl QueueType { |
| 27 | + /// Construct a new queue configuration given the expected sender and queue |
| 28 | + /// suffix configuration |
| 29 | + /// |
| 30 | + /// # Errors |
| 31 | + /// This function fails if the given queue suffix is invalid. |
| 32 | + pub fn new(sender: &str, suffix: &Suffix) -> Result<Self> { |
| 33 | + let exchange = format!("{}.jobs", sender); |
| 34 | + let queue = suffix.format(format!("{}.runner", exchange))?; |
| 35 | + |
| 36 | + Ok(Self { |
| 37 | + props: QueueProps { |
| 38 | + exchange, |
| 39 | + queue, |
| 40 | + binding: Binding::Fanout, |
| 41 | + prefetch: 1, |
| 42 | + max_len_bytes: 100 * 1024 * 1024, // 100 MiB |
| 43 | + auto_delete: suffix.is_debug(), |
| 44 | + retry: Some(RetryProps { |
| 45 | + max_tries: 5, |
| 46 | + delay_hint: Duration::from_secs(5), |
| 47 | + max_delay: Duration::from_secs(10 * 60), |
| 48 | + }), |
| 49 | + }, |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl crate::QueueType for QueueType { |
| 55 | + type Message = Message; |
| 56 | + |
| 57 | + #[inline] |
| 58 | + fn info(&self) -> crate::queue_type::QueueInfo { |
| 59 | + (&self.props).into() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// The type of an search indexer producer |
| 64 | +#[cfg(feature = "producer")] |
| 65 | +pub type Producer = crate::producer::Producer<QueueType>; |
| 66 | +/// The type of an search indexer consumer |
| 67 | +#[cfg(feature = "consumer")] |
| 68 | +pub type Consumer = crate::consumer::Consumer<QueueType>; |
0 commit comments