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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::{boxed::Box, fmt::Debug};
use futures::future::{self, Future};
use slog_scope;
use self::notification::{Notification, NotificationType};
pub use self::sqs::Queue as Sqs;
use crate::{
db::{auth_db::DbClient, delivery_problems::DeliveryProblems, message_data::MessageData},
logging::MozlogLogger,
settings::Settings,
types::error::{AppError, AppErrorKind, AppResult},
};
mod mock;
pub mod notification;
pub mod sqs;
#[cfg(test)]
mod test;
#[derive(Debug)]
pub struct Queues {
bounce_queue: Box<Incoming>,
complaint_queue: Box<Incoming>,
delivery_queue: Box<Incoming>,
notification_queue: Box<Outgoing>,
delivery_problems: DeliveryProblems<DbClient>,
message_data: MessageData,
}
pub trait Incoming: Debug + Sync {
fn receive(&'static self) -> ReceiveFuture;
fn delete(&'static self, message: Message) -> DeleteFuture;
}
type ReceiveFuture = Box<Future<Item = Vec<Message>, Error = AppError>>;
type DeleteFuture = Box<Future<Item = (), Error = AppError>>;
pub trait Outgoing: Debug + Sync {
fn send(&'static self, body: &Notification) -> SendFuture;
}
type SendFuture = Box<Future<Item = String, Error = AppError>>;
pub trait Factory {
fn new(id: String, settings: &Settings) -> Self;
}
#[derive(Debug, Default)]
pub struct Message {
pub id: String,
pub notification: Notification,
}
#[derive(Debug)]
pub struct QueueIds {
pub bounce: String,
pub complaint: String,
pub delivery: String,
pub notification: String,
}
impl Queues {
pub fn new<Q: 'static>(ids: QueueIds, settings: &Settings) -> Queues
where
Q: Incoming + Outgoing + Factory,
{
Queues {
bounce_queue: Box::new(Q::new(ids.bounce, settings)),
complaint_queue: Box::new(Q::new(ids.complaint, settings)),
delivery_queue: Box::new(Q::new(ids.delivery, settings)),
notification_queue: Box::new(Q::new(ids.notification, settings)),
delivery_problems: DeliveryProblems::new(settings, DbClient::new(settings)),
message_data: MessageData::new(settings),
}
}
pub fn process(&'static self) -> QueueFuture {
let joined_futures = self
.process_queue(&self.bounce_queue)
.join3(
self.process_queue(&self.complaint_queue),
self.process_queue(&self.delivery_queue),
)
.map(|results| results.0 + results.1 + results.2);
Box::new(joined_futures)
}
fn process_queue(&'static self, queue: &'static Box<Incoming>) -> QueueFuture {
let future = queue
.receive()
.and_then(move |messages| {
let mut futures: Vec<Box<Future<Item = (), Error = AppError>>> = Vec::new();
for mut message in messages {
if message.notification.notification_type != NotificationType::Null {
let future = self
.handle_notification(&mut message.notification)
.and_then(move |_| queue.delete(message));
futures.push(Box::new(future));
}
}
future::join_all(futures.into_iter())
})
.map(|results| results.len());
Box::new(future)
}
fn handle_notification(
&'static self,
notification: &mut Notification,
) -> Box<Future<Item = (), Error = AppError>> {
let result = match notification.notification_type {
NotificationType::Bounce => self.record_bounce(notification),
NotificationType::Complaint => self.record_complaint(notification),
NotificationType::Delivery => Ok(()),
NotificationType::Null => {
Err(AppErrorKind::InvalidNotification("null type".to_owned()).into())
}
};
match result {
Ok(_) => {
notification.metadata = self
.message_data
.consume(¬ification.mail.message_id)
.ok()
.unwrap_or(None);
let future = self
.notification_queue
.send(¬ification)
.map(|id| {
info!("{}", "Sent message to notification queue"; "id" => id);
()
})
.or_else(|error| {
let logger = MozlogLogger(slog_scope::logger());
let log = MozlogLogger::with_app_error(&logger, &error)
.expect("MozlogLogger::with_app_error error");
slog_error!(log, "{}", "Error sending notification to queue");
Ok(())
});
Box::new(future)
}
Err(error) => Box::new(future::err(error)),
}
}
fn record_bounce(&'static self, notification: &Notification) -> AppResult<()> {
if let Some(ref bounce) = notification.bounce {
for recipient in &bounce.bounced_recipients {
self.delivery_problems.record_bounce(
&recipient,
bounce.bounce_type,
bounce.bounce_subtype,
bounce.timestamp,
)?;
}
Ok(())
} else {
Err(AppErrorKind::InvalidNotification(
"missing bounce payload".to_owned(),
))?
}
}
fn record_complaint(&'static self, notification: &Notification) -> AppResult<()> {
if let Some(ref complaint) = notification.complaint {
for recipient in &complaint.complained_recipients {
self.delivery_problems.record_complaint(
&recipient,
complaint.complaint_feedback_type,
complaint.timestamp,
)?;
}
Ok(())
} else {
Err(AppErrorKind::InvalidNotification(
"missing complaint payload".to_owned(),
))?
}
}
}
type QueueFuture = Box<Future<Item = usize, Error = AppError>>;