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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! Dead-letter-queue background dispatch: peek / delete-one / resend /
//! purge / replay-batch. Each `spawn_dlq_*` helper reads the active
//! `DlqState` (set when the operator opens the DLQ viewer), fires the
//! matching SQS call, and routes the outcome through `AppMsg::DlqMessages`
//! (peek) or `AppMsg::DlqActionResult` (mutations) for the handler in
//! `msg.rs` to fold back into the viewer.
//!
//! Read-only safety: all four mutating spawners (delete-one / resend /
//! purge / replay) gate through `deny_write` exactly as the single-env
//! write paths do.
//!
//! 0.21+ lift: cluster moved out of `src/app.rs` as part of the
//! `spawn_*` clusters refactor. Pure relocation; the `handle_dlq_key`
//! key handler that sat between these methods stays in `app.rs` (it's
//! not a spawner). No behaviour change.
use super::{flatten_err, App, AppMsg, DlqOp, QueueView};
impl App {
pub(super) fn spawn_dlq_fetch(&mut self) {
let Some(dlq) = self.dlq.as_mut() else { return };
dlq.loading = true;
dlq.error = None;
let env_name = dlq.env_name.clone();
let queue_url = match dlq.viewing {
QueueView::Dlq => dlq.dlq_url.clone(),
QueueView::Main => dlq.main_queue_url.clone(),
};
let queue_for_msg = queue_url.clone();
self.spawn_aws_in(
self.dlq_client(),
"peek_messages",
move |aws| async move { aws.peek_messages(&queue_url, 50).await },
move |gen, result| AppMsg::DlqMessages {
gen,
env_name,
queue_url: queue_for_msg,
result,
},
);
}
/// Delete a single message from whichever queue is currently loaded
/// (`dlq.viewing`). The message's `receipt_handle` keeps it deletable
/// even though our visibility timeout window is short — SQS treats the
/// receipt handle as the canonical authorisation token for delete.
pub(super) fn spawn_dlq_delete_one(&mut self, msg_id: &str) {
let env_for_guard = match self.dlq.as_ref() {
Some(d) => d.env_name.clone(),
None => return,
};
// Same write gate as every sibling DLQ op (resend/purge/replay)
// — a delete is just as destructive as a purge of one.
if self.deny_write(&env_for_guard, "delete") {
return;
}
// Resolved before the `as_mut` borrow: it reads `self.dlq`.
let client = self.dlq_client();
let Some(dlq) = self.dlq.as_mut() else { return };
// Resolve by MESSAGE ID at dispatch time — the armed confirm
// survives refreshes safely; a message that vanished from the
// list refuses instead of deleting a neighbour.
let Some(msg) = dlq.messages.iter().find(|m| m.id == msg_id).cloned() else {
self.error_message =
Some("delete: message no longer in the loaded list — refreshed away?".into());
return;
};
let queue_url = match dlq.viewing {
QueueView::Dlq => dlq.dlq_url.clone(),
QueueView::Main => dlq.main_queue_url.clone(),
};
if queue_url.is_empty() {
self.error_message = Some("queue URL missing — cannot delete".into());
return;
}
let env_name = dlq.env_name.clone();
let tx = self.msg_tx.clone();
let gen = self.generation;
let queue_label = if matches!(dlq.viewing, QueueView::Main) {
"MAIN"
} else {
"DLQ"
};
crate::audit::append_dlq_op(
self.context.account_id.as_deref(),
self.context.profile.as_deref(),
&self.region_for_name(&env_name),
"sqs-delete",
&env_name,
&[("queue", queue_label), ("msg_id", &msg.id)],
);
tokio::spawn(async move {
// SQS queue URLs are region-scoped: against the home
// region's SQS a fan-out row's queue doesn't merely read
// stale, it doesn't exist. Resolve where the env actually
// lives before touching it — this covers a purge and a
// replay, so a wrong-region client is a destructive
// action pointed at the wrong account's queue.
let aws = match client.resolve().await {
Ok(aws) => aws,
Err(e) => {
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result: Err(flatten_err("cached_client", e)),
});
return;
}
};
let result = aws
.delete_message(&queue_url, &msg.receipt_handle)
.await
.map(|_| DlqOp::Deleted {
message_id: msg.id.clone(),
})
.map_err(|e| flatten_err("delete_message", e));
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result,
});
});
}
pub(super) fn spawn_dlq_resend_selected(&mut self) {
let env_name = match self.dlq.as_ref() {
Some(d) => d.env_name.clone(),
None => return,
};
if self.deny_write(&env_name, "resend") {
return;
}
// Resolved before the `as_mut` borrow: it reads `self.dlq`.
let client = self.dlq_client();
let Some(dlq) = self.dlq.as_mut() else { return };
let Some(idx) = dlq.list_state.selected() else {
return;
};
let Some(msg) = dlq.messages.get(idx).cloned() else {
return;
};
if dlq.main_queue_url.is_empty() {
dlq.error = Some("main queue URL unknown — cannot resend".into());
return;
}
let tx = self.msg_tx.clone();
let gen = self.generation;
let env_name = dlq.env_name.clone();
let main_url = dlq.main_queue_url.clone();
let dlq_url = dlq.dlq_url.clone();
crate::audit::append_dlq_op(
self.context.account_id.as_deref(),
self.context.profile.as_deref(),
&self.region_for_name(&env_name),
"dlq-resend",
&env_name,
&[("msg_id", &msg.id)],
);
tokio::spawn(async move {
// SQS queue URLs are region-scoped: against the home
// region's SQS a fan-out row's queue doesn't merely read
// stale, it doesn't exist. Resolve where the env actually
// lives before touching it — this covers a purge and a
// replay, so a wrong-region client is a destructive
// action pointed at the wrong account's queue.
let aws = match client.resolve().await {
Ok(aws) => aws,
Err(e) => {
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result: Err(flatten_err("cached_client", e)),
});
return;
}
};
let result = match aws.send_message(&main_url, &msg.body).await {
Ok(()) => match aws.delete_message(&dlq_url, &msg.receipt_handle).await {
Ok(()) => Ok(DlqOp::Resent {
message_id: msg.id.clone(),
}),
Err(e) => {
tracing::error!(target: "ebman::aws", op = "dlq_delete_after_send", error = ?e, "aws call failed");
Err(format!("sent to main queue, but DLQ delete failed: {e}"))
}
},
Err(e) => {
tracing::error!(target: "ebman::aws", op = "dlq_send", error = ?e, "aws call failed");
Err(format!("send to main queue failed: {e}"))
}
};
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result,
});
});
}
pub(super) fn spawn_dlq_purge(&mut self, env_name: String, dlq_url: String) {
if self.deny_write(&env_name, "purge") {
return;
}
crate::audit::append_dlq_op(
self.context.account_id.as_deref(),
self.context.profile.as_deref(),
&self.region_for_name(&env_name),
"dlq-purge",
&env_name,
&[],
);
let client = self.dlq_client();
let tx = self.msg_tx.clone();
let gen = self.generation;
tokio::spawn(async move {
// SQS queue URLs are region-scoped: against the home
// region's SQS a fan-out row's queue doesn't merely read
// stale, it doesn't exist. Resolve where the env actually
// lives before touching it — this covers a purge and a
// replay, so a wrong-region client is a destructive
// action pointed at the wrong account's queue.
let aws = match client.resolve().await {
Ok(aws) => aws,
Err(e) => {
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result: Err(flatten_err("cached_client", e)),
});
return;
}
};
let result = aws
.purge_queue(&dlq_url)
.await
.map(|_| DlqOp::Purged)
.map_err(|e| flatten_err("purge_queue", e));
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result,
});
});
}
/// Batch DLQ replay: for each message, send the body to the main queue
/// then delete it from the DLQ. A send failure (or a delete failure
/// after a successful send) counts toward `failures` and is logged;
/// the batch keeps going. Result lands as `DlqOp::Replayed`.
pub(super) fn spawn_dlq_replay_batch(&mut self, messages: Vec<crate::aws::QueueMessage>) {
let env_name = match self.dlq.as_ref() {
Some(d) => d.env_name.clone(),
None => return,
};
if self.deny_write(&env_name, "replay") {
return;
}
let Some(dlq) = self.dlq.as_ref() else { return };
if matches!(dlq.viewing, QueueView::Main) {
self.error_message = Some("replay is only available in DLQ view".into());
return;
}
if dlq.main_queue_url.is_empty() {
self.error_message = Some("main queue URL unknown — cannot replay".into());
return;
}
let main_url = dlq.main_queue_url.clone();
let dlq_url = dlq.dlq_url.clone();
let env_name = dlq.env_name.clone();
let client = self.dlq_client();
let tx = self.msg_tx.clone();
let gen = self.generation;
let count = messages.len();
crate::audit::append_dlq_op(
self.context.account_id.as_deref(),
self.context.profile.as_deref(),
&self.region_for_name(&env_name),
"dlq-replay",
&env_name,
&[("count", &count.to_string())],
);
self.status_message = Some(format!("replaying {count} message(s) to the main queue…"));
tokio::spawn(async move {
// SQS queue URLs are region-scoped: against the home
// region's SQS a fan-out row's queue doesn't merely read
// stale, it doesn't exist. Resolve where the env actually
// lives before touching it — this covers a purge and a
// replay, so a wrong-region client is a destructive
// action pointed at the wrong account's queue.
let aws = match client.resolve().await {
Ok(aws) => aws,
Err(e) => {
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result: Err(flatten_err("cached_client", e)),
});
return;
}
};
let mut failures = 0usize;
for msg in &messages {
match aws.send_message(&main_url, &msg.body).await {
Ok(()) => {
if let Err(e) = aws.delete_message(&dlq_url, &msg.receipt_handle).await {
tracing::error!(target: "ebman::aws", op = "dlq_replay_delete", error = ?e, msg_id = %msg.id, "DLQ delete after send failed");
failures += 1;
}
}
Err(e) => {
tracing::error!(target: "ebman::aws", op = "dlq_replay_send", error = ?e, msg_id = %msg.id, "send to main queue failed");
failures += 1;
}
}
}
let result = Ok(DlqOp::Replayed {
count: count - failures,
failures,
});
let _ = tx.send(AppMsg::DlqActionResult {
gen,
env_name,
result,
});
});
}
}