rusmes-cli 0.1.1

Administrative CLI tool for RusMES — user/mailbox/queue management, backup, restore, migration, metrics inspection, and configuration validation
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Queue management commands

use anyhow::Result;
use colored::*;
use serde::{Deserialize, Serialize};
use tabled::{Table, Tabled};

use crate::client::Client;

#[derive(Debug, Serialize, Deserialize, Tabled)]
pub struct QueuedMessage {
    pub id: String,
    pub from: String,
    pub to: String,
    pub size_kb: u64,
    pub attempts: u32,
    pub next_retry: String,
    pub status: String,
}

/// List messages in the queue
pub async fn list(client: &Client, json: bool, filter: Option<&str>) -> Result<()> {
    let mut url = "/api/queue".to_string();
    if let Some(f) = filter {
        url.push_str(&format!("?status={}", f));
    }

    let messages: Vec<QueuedMessage> = client.get(&url).await?;

    if json {
        println!("{}", serde_json::to_string_pretty(&messages)?);
    } else {
        if messages.is_empty() {
            println!("{}", "Queue is empty".green());
            return Ok(());
        }

        let table = Table::new(&messages).to_string();
        println!("{}", table);

        let total_size: u64 = messages.iter().map(|m| m.size_kb).sum();
        let failed = messages.iter().filter(|m| m.status == "failed").count();
        let pending = messages.iter().filter(|m| m.status == "pending").count();
        let retrying = messages.iter().filter(|m| m.status == "retrying").count();

        println!(
            "\n{} messages in queue ({} pending, {} retrying, {} failed)",
            messages.len().to_string().bold(),
            pending,
            retrying,
            failed
        );
        println!("Total size: {} KB", total_size);
    }

    Ok(())
}

/// Flush the queue (attempt immediate delivery)
pub async fn flush(client: &Client, json: bool) -> Result<()> {
    #[derive(Deserialize, Serialize)]
    struct FlushResponse {
        messages_processed: u32,
        messages_sent: u32,
        messages_failed: u32,
    }

    let response: FlushResponse = client
        .post("/api/queue/flush", &serde_json::json!({}))
        .await?;

    if json {
        println!("{}", serde_json::to_string_pretty(&response)?);
    } else {
        println!("{}", "✓ Queue flushed".green().bold());
        println!("  Processed: {}", response.messages_processed);
        println!("  Sent: {}", response.messages_sent.to_string().green());
        if response.messages_failed > 0 {
            println!("  Failed: {}", response.messages_failed.to_string().red());
        }
    }

    Ok(())
}

/// Inspect a specific message in the queue
pub async fn inspect(client: &Client, message_id: &str, json: bool) -> Result<()> {
    #[derive(Deserialize, Serialize)]
    struct MessageDetails {
        id: String,
        from: String,
        to: Vec<String>,
        subject: String,
        size_bytes: u64,
        attempts: u32,
        max_attempts: u32,
        status: String,
        created_at: String,
        last_attempt: Option<String>,
        next_retry: Option<String>,
        error: Option<String>,
        headers: Vec<(String, String)>,
    }

    let details: MessageDetails = client.get(&format!("/api/queue/{}", message_id)).await?;

    if json {
        println!("{}", serde_json::to_string_pretty(&details)?);
    } else {
        println!("{}", format!("Message: {}", message_id).bold());
        println!("  From: {}", details.from);
        println!("  To: {}", details.to.join(", "));
        println!("  Subject: {}", details.subject);
        println!("  Size: {} KB", details.size_bytes / 1024);
        println!(
            "  Status: {}",
            match details.status.as_str() {
                "pending" => details.status.yellow(),
                "retrying" => details.status.blue(),
                "failed" => details.status.red(),
                "sent" => details.status.green(),
                _ => details.status.normal(),
            }
        );
        println!(
            "  Attempts: {} / {}",
            details.attempts, details.max_attempts
        );
        println!("  Created: {}", details.created_at);

        if let Some(last) = &details.last_attempt {
            println!("  Last attempt: {}", last);
        }

        if let Some(next) = &details.next_retry {
            println!("  Next retry: {}", next);
        }

        if let Some(error) = &details.error {
            println!("  Error: {}", error.red());
        }

        if !details.headers.is_empty() {
            println!("\n  Headers:");
            for (key, value) in &details.headers {
                println!("    {}: {}", key.bold(), value);
            }
        }
    }

    Ok(())
}

/// Delete a message from the queue
pub async fn delete(client: &Client, message_id: &str, json: bool) -> Result<()> {
    #[derive(Deserialize, Serialize)]
    struct DeleteResponse {
        success: bool,
    }

    let response: DeleteResponse = client.delete(&format!("/api/queue/{}", message_id)).await?;

    if json {
        println!("{}", serde_json::to_string_pretty(&response)?);
    } else {
        println!(
            "{}",
            format!("✓ Message {} deleted from queue", message_id)
                .green()
                .bold()
        );
    }

    Ok(())
}

/// Retry a failed message immediately
pub async fn retry(client: &Client, message_id: &str, json: bool) -> Result<()> {
    #[derive(Deserialize, Serialize)]
    struct RetryResponse {
        success: bool,
        error: Option<String>,
    }

    let response: RetryResponse = client
        .post(
            &format!("/api/queue/{}/retry", message_id),
            &serde_json::json!({}),
        )
        .await?;

    if json {
        println!("{}", serde_json::to_string_pretty(&response)?);
    } else if response.success {
        println!(
            "{}",
            format!("✓ Message {} sent successfully", message_id)
                .green()
                .bold()
        );
    } else {
        println!(
            "{}",
            format!("✗ Failed to send message {}", message_id)
                .red()
                .bold()
        );
        if let Some(error) = response.error {
            println!("  Error: {}", error.red());
        }
    }

    Ok(())
}

/// Purge all failed messages from the queue
pub async fn purge(client: &Client, json: bool) -> Result<()> {
    #[derive(Deserialize, Serialize)]
    struct PurgeResponse {
        messages_deleted: u32,
    }

    let response: PurgeResponse = client
        .post("/api/queue/purge", &serde_json::json!({}))
        .await?;

    if json {
        println!("{}", serde_json::to_string_pretty(&response)?);
    } else {
        println!(
            "{}",
            format!("✓ Purged {} failed messages", response.messages_deleted)
                .green()
                .bold()
        );
    }

    Ok(())
}

/// Show queue statistics
pub async fn stats(client: &Client, json: bool) -> Result<()> {
    #[derive(Deserialize, Serialize)]
    struct QueueStats {
        total: u32,
        pending: u32,
        retrying: u32,
        failed: u32,
        total_size_mb: u64,
        oldest_message: Option<String>,
        average_attempts: f64,
    }

    let stats: QueueStats = client.get("/api/queue/stats").await?;

    if json {
        println!("{}", serde_json::to_string_pretty(&stats)?);
    } else {
        println!("{}", "Queue Statistics".bold());
        println!("  Total messages: {}", stats.total);
        println!("  Pending: {}", stats.pending.to_string().yellow());
        println!("  Retrying: {}", stats.retrying.to_string().blue());
        println!("  Failed: {}", stats.failed.to_string().red());
        println!("  Total size: {} MB", stats.total_size_mb);
        if let Some(oldest) = stats.oldest_message {
            println!("  Oldest message: {}", oldest);
        }
        println!("  Average attempts: {:.1}", stats.average_attempts);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_queued_message_serialization() {
        let msg = QueuedMessage {
            id: "msg123".to_string(),
            from: "sender@example.com".to_string(),
            to: "recipient@example.com".to_string(),
            size_kb: 100,
            attempts: 2,
            next_retry: "2024-01-01T00:00:00Z".to_string(),
            status: "retrying".to_string(),
        };

        let json = serde_json::to_string(&msg).unwrap();
        assert!(json.contains("msg123"));
    }

    #[test]
    fn test_queue_stats_calculation() {
        let messages = [
            QueuedMessage {
                id: "1".to_string(),
                from: "a@test.com".to_string(),
                to: "b@test.com".to_string(),
                size_kb: 10,
                attempts: 1,
                next_retry: "".to_string(),
                status: "pending".to_string(),
            },
            QueuedMessage {
                id: "2".to_string(),
                from: "a@test.com".to_string(),
                to: "c@test.com".to_string(),
                size_kb: 20,
                attempts: 3,
                next_retry: "".to_string(),
                status: "failed".to_string(),
            },
        ];

        let total_size: u64 = messages.iter().map(|m| m.size_kb).sum();
        let failed = messages.iter().filter(|m| m.status == "failed").count();

        assert_eq!(total_size, 30);
        assert_eq!(failed, 1);
    }

    #[test]
    fn test_queued_message_pending() {
        let msg = QueuedMessage {
            id: "pending123".to_string(),
            from: "sender@example.com".to_string(),
            to: "recipient@example.com".to_string(),
            size_kb: 50,
            attempts: 0,
            next_retry: "".to_string(),
            status: "pending".to_string(),
        };

        assert_eq!(msg.status, "pending");
        assert_eq!(msg.attempts, 0);
    }

    #[test]
    fn test_queued_message_deserialization() {
        let json = r#"{
            "id": "msg456",
            "from": "sender@test.com",
            "to": "recipient@test.com",
            "size_kb": 150,
            "attempts": 1,
            "next_retry": "2024-01-01T12:00:00Z",
            "status": "retrying"
        }"#;

        let msg: QueuedMessage = serde_json::from_str(json).unwrap();
        assert_eq!(msg.id, "msg456");
        assert_eq!(msg.attempts, 1);
        assert_eq!(msg.status, "retrying");
    }

    #[test]
    fn test_queue_status_filtering() {
        let messages = [
            QueuedMessage {
                id: "1".to_string(),
                from: "a@test.com".to_string(),
                to: "b@test.com".to_string(),
                size_kb: 10,
                attempts: 0,
                next_retry: "".to_string(),
                status: "pending".to_string(),
            },
            QueuedMessage {
                id: "2".to_string(),
                from: "a@test.com".to_string(),
                to: "c@test.com".to_string(),
                size_kb: 20,
                attempts: 2,
                next_retry: "".to_string(),
                status: "retrying".to_string(),
            },
            QueuedMessage {
                id: "3".to_string(),
                from: "a@test.com".to_string(),
                to: "d@test.com".to_string(),
                size_kb: 30,
                attempts: 5,
                next_retry: "".to_string(),
                status: "failed".to_string(),
            },
        ];

        let pending = messages.iter().filter(|m| m.status == "pending").count();
        let retrying = messages.iter().filter(|m| m.status == "retrying").count();
        let failed = messages.iter().filter(|m| m.status == "failed").count();

        assert_eq!(pending, 1);
        assert_eq!(retrying, 1);
        assert_eq!(failed, 1);
    }

    #[test]
    fn test_queue_empty() {
        let messages: Vec<QueuedMessage> = vec![];
        assert!(messages.is_empty());
    }

    #[test]
    fn test_queue_message_size_calculation() {
        let messages = [
            QueuedMessage {
                id: "1".to_string(),
                from: "a@test.com".to_string(),
                to: "b@test.com".to_string(),
                size_kb: 100,
                attempts: 1,
                next_retry: "".to_string(),
                status: "pending".to_string(),
            },
            QueuedMessage {
                id: "2".to_string(),
                from: "a@test.com".to_string(),
                to: "c@test.com".to_string(),
                size_kb: 200,
                attempts: 1,
                next_retry: "".to_string(),
                status: "pending".to_string(),
            },
        ];

        let total_kb: u64 = messages.iter().map(|m| m.size_kb).sum();
        let total_mb = total_kb / 1024;

        assert_eq!(total_kb, 300);
        assert_eq!(total_mb, 0); // Less than 1 MB
    }
}