1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use myc_core::domain::entities::{
LocalMessageReading, LocalMessageWrite, RemoteMessageWrite,
};
use myc_notifier::{executor::consume_messages, models::QueueConfig};
use rand::Rng;
use std::{sync::Arc, time::Duration};
/// Dispatch email messages
///
/// Spawns a new thread to consume messages from the email queue.
///
#[tracing::instrument(name = "email_dispatcher", skip_all)]
pub(crate) async fn email_dispatcher(
queue_config: QueueConfig,
local_message_read_repo: Arc<dyn LocalMessageReading>,
local_message_write_repo: Arc<dyn LocalMessageWrite>,
remote_message_write_repo: Arc<dyn RemoteMessageWrite>,
) {
tokio::spawn(async move {
tracing::info!("Starting email dispatcher");
let mut interval = actix_rt::time::interval(Duration::from_secs(
match queue_config
.consume_interval_in_secs
.async_get_or_error()
.await
{
Ok(interval) => interval,
Err(err) => {
panic!("Error on get consume interval: {err}");
}
},
));
//
// Skip the first tick to avoid fetching events that were created in the
// same second as the dispatcher start.
//
interval.tick().await;
//
// Wait for a random time between 1 and the consume interval. This time
// should avoid the webhook dispatcher to start at the same time as the
// email dispatcher and avoid the simultaneous consumption of the same
// event over multiple containers.
//
let random_time =
rand::thread_rng().gen_range(1..=interval.period().as_secs());
tokio::time::sleep(Duration::from_secs(random_time)).await;
loop {
interval.tick().await;
let queue_name = match queue_config
.clone()
.email_queue_name
.async_get_or_error()
.await
{
Ok(name) => name,
Err(err) => {
panic!("Error on get queue name: {err}");
}
};
let claim_batch_size = match queue_config
.clone()
.claim_batch_size
.async_get_or_error()
.await
{
Ok(size) => size,
Err(err) => {
panic!("Error on get claim batch size: {err}");
}
};
match consume_messages(
queue_name.to_owned(),
claim_batch_size,
local_message_read_repo.clone(),
local_message_write_repo.clone(),
remote_message_write_repo.clone(),
)
.await
{
Ok((messages_success, messages_failed)) => {
if messages_success > 0 {
tracing::info!(
"'{}' messages consumed from the queue '{}'",
messages_success,
queue_name.to_owned()
);
}
if messages_failed > 0 {
tracing::error!(
"'{}' messages failed to be consumed from the queue '{}'",
messages_failed,
queue_name.to_owned()
);
}
}
Err(err) => {
if !err.expected() {
panic!("Error on consume messages: {err}");
}
}
};
}
});
}