simple-queue 0.2.2

A simple persistent queue implementation in Rust backed by PostgreSQL and tokio
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#![cfg(feature = "janitor")]

mod setup;
use setup::*;
use simple_queue::prelude::*;
use std::time::Duration;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Insert a job row directly with the given status, bypassing the queue logic.
async fn insert_job_with_status(pool: &sqlx::PgPool, queue: &str, status: &str) -> uuid::Uuid {
    let id = uuid::Uuid::new_v4();
    sqlx::query!(
        r#"
        INSERT INTO job_queue (id, queue, job_data, status, attempt, max_attempts)
        VALUES ($1, $2, '{}', $3, 0, 3)
        "#,
        id,
        queue,
        status,
    )
    .execute(pool)
    .await
    .expect("failed to insert job");
    id
}

/// Count rows in a table matching a queue and optional status.
async fn count_in(pool: &sqlx::PgPool, table: &str, id: uuid::Uuid) -> i64 {
    let row: (i64,) = sqlx::query_as(&format!("SELECT COUNT(*) FROM {} WHERE id = $1", table))
        .bind(id)
        .fetch_one(pool)
        .await
        .expect("count query failed");
    row.0
}

/// Spawn a queue with an unreachably long janitor interval so the background
/// janitor task never fires during a test – we call the methods manually.
async fn spawn_queue_no_janitor(pool: &sqlx::PgPool) -> std::sync::Arc<SimpleQueue> {
    spawn_queue_with(pool, |q| {
        q.with_janitor_interval(tokio::time::Duration::from_secs(3600))
            .with_empty_poll_sleep(tokio::time::Duration::from_millis(100))
            .with_heartbeat_interval(tokio::time::Duration::from_millis(500))
            .with_default_backoff_strategy(simple_queue::sync::BackoffStrategy::Linear {
                delay: chrono::Duration::milliseconds(100),
            })
            .with_max_reprocess_count(10)
    })
    .await
}

// ---------------------------------------------------------------------------
// run_archiver tests
// ---------------------------------------------------------------------------

/// A completed job must be removed from job_queue and appear in job_queue_archive.
#[tokio::test(flavor = "multi_thread")]
async fn test_archiver_moves_completed_job() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    let id = insert_job_with_status(&ctx.pool, "janitor-archive-completed", "completed").await;

    let janitor = queue.janitor().await;
    janitor.run_archiver().await.expect("run_archiver failed");

    assert_eq!(
        count_in(&ctx.pool, "job_queue", id).await,
        0,
        "completed job should be removed from job_queue"
    );
    assert_eq!(
        count_in(&ctx.pool, "job_queue_archive", id).await,
        1,
        "completed job should appear in job_queue_archive"
    );
}

/// All completed jobs in one run are archived together.
#[tokio::test(flavor = "multi_thread")]
async fn test_archiver_moves_multiple_completed_jobs() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    let mut inserted = Vec::new();
    for _ in 0..5 {
        let id = insert_job_with_status(&ctx.pool, "janitor-archive-multi", "completed").await;
        inserted.push(id);
    }

    let janitor = queue.janitor().await;
    janitor.run_archiver().await.expect("run_archiver failed");

    for id in &inserted {
        assert_eq!(
            count_in(&ctx.pool, "job_queue", *id).await,
            0,
            "job {id} should be gone from job_queue"
        );
        assert_eq!(
            count_in(&ctx.pool, "job_queue_archive", *id).await,
            1,
            "job {id} should be in job_queue_archive"
        );
    }
}

/// The archiver must not touch jobs whose status is not `completed`.
#[tokio::test(flavor = "multi_thread")]
async fn test_archiver_ignores_non_completed_statuses() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    let untouched_statuses = [
        "pending",
        "running",
        "failed",
        "cancelled",
        "critical_failure",
        "unprocessable",
        "bad_job",
    ];

    let mut ids = Vec::new();
    for status in &untouched_statuses {
        let id = insert_job_with_status(&ctx.pool, "janitor-archive-ignore", status).await;
        ids.push((*status, id));
    }

    let janitor = queue.janitor().await;
    janitor.run_archiver().await.expect("run_archiver failed");

    for (status, id) in &ids {
        assert_eq!(
            count_in(&ctx.pool, "job_queue", *id).await,
            1,
            "status={status}: job should still be in job_queue"
        );
        assert_eq!(
            count_in(&ctx.pool, "job_queue_archive", *id).await,
            0,
            "status={status}: job must NOT appear in job_queue_archive"
        );
    }
}

/// Archiver on an empty queue must succeed without errors.
#[tokio::test(flavor = "multi_thread")]
async fn test_archiver_empty_queue_is_noop() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    let janitor = queue.janitor().await;
    janitor
        .run_archiver()
        .await
        .expect("run_archiver should not fail on empty queue");
}

// ---------------------------------------------------------------------------
// run_dlq tests
// ---------------------------------------------------------------------------

/// Every status that the DLQ handles must be moved to job_queue_archive.
#[tokio::test(flavor = "multi_thread")]
async fn test_dlq_moves_all_terminal_statuses() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    // Statuses that run_dlq is responsible for
    let dlq_statuses = ["unprocessable", "cancelled", "critical_failure", "bad_job"];

    let mut ids = Vec::new();
    for status in &dlq_statuses {
        let id = insert_job_with_status(&ctx.pool, "janitor-dlq-terminal", status).await;
        ids.push((*status, id));
    }

    let janitor = queue.janitor().await;
    janitor.run_dlq().await.expect("run_dlq failed");

    for (status, id) in &ids {
        assert_eq!(
            count_in(&ctx.pool, "job_queue", *id).await,
            0,
            "status={status}: job should be removed from job_queue"
        );
        assert_eq!(
            count_in(&ctx.pool, "job_queue_archive", *id).await,
            1,
            "status={status}: job should appear in job_queue_archive"
        );
    }
}

/// DLQ must not touch `running` or `completed` jobs; those belong to other paths.
#[tokio::test(flavor = "multi_thread")]
async fn test_dlq_ignores_running_and_completed() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;

    let ignored_statuses = ["running", "completed"];

    let mut ids = Vec::new();
    for status in &ignored_statuses {
        let id = insert_job_with_status(&ctx.pool, "janitor-dlq-ignore", status).await;
        ids.push((*status, id));
    }

    let janitor = queue.janitor().await;
    janitor.run_dlq().await.expect("run_dlq failed");

    for (status, id) in &ids {
        assert_eq!(
            count_in(&ctx.pool, "job_queue", *id).await,
            1,
            "status={status}: job should still be in job_queue"
        );
        assert_eq!(
            count_in(&ctx.pool, "job_queue_archive", *id).await,
            0,
            "status={status}: job must NOT appear in job_queue_archive"
        );
    }
}

/// DLQ on an empty queue must succeed without errors.
#[tokio::test(flavor = "multi_thread")]
async fn test_dlq_empty_queue_is_noop() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    let janitor = queue.janitor().await;
    janitor
        .run_dlq()
        .await
        .expect("run_dlq should not fail on empty queue");
}

// ---------------------------------------------------------------------------
// Cross-method isolation tests
// ---------------------------------------------------------------------------

/// run_archiver must not accidentally process DLQ statuses and vice-versa:
/// run_archiver leaves DLQ statuses untouched, run_dlq leaves completed untouched.
#[tokio::test(flavor = "multi_thread")]
async fn test_archiver_and_dlq_do_not_overlap() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    let completed_id = insert_job_with_status(&ctx.pool, "janitor-overlap", "completed").await;
    let cancelled_id = insert_job_with_status(&ctx.pool, "janitor-overlap", "cancelled").await;

    // Running only the archiver should leave the cancelled job alone.
    {
        let janitor = queue.janitor().await;
        janitor.run_archiver().await.expect("run_archiver failed");
    }

    assert_eq!(
        count_in(&ctx.pool, "job_queue", completed_id).await,
        0,
        "completed job should have been archived"
    );
    assert_eq!(
        count_in(&ctx.pool, "job_queue", cancelled_id).await,
        1,
        "cancelled job should still be in job_queue after archiver"
    );

    // Now run the DLQ; it should move the remaining cancelled job.
    {
        let janitor = queue.janitor().await;
        janitor.run_dlq().await.expect("run_dlq failed");
    }

    assert_eq!(
        count_in(&ctx.pool, "job_queue", cancelled_id).await,
        0,
        "cancelled job should be removed from job_queue after dlq"
    );
    assert_eq!(
        count_in(&ctx.pool, "job_queue_archive", cancelled_id).await,
        1,
        "cancelled job should be in job_queue_archive after dlq"
    );
}

/// Run both archiver and DLQ in sequence and verify all eligible jobs move
/// while ineligible ones remain.
#[tokio::test(flavor = "multi_thread")]
async fn test_full_janitor_cycle_moves_correct_jobs() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue_no_janitor(&ctx.pool).await;

    // Jobs that should be archived
    let to_archive = [
        (
            "completed",
            insert_job_with_status(&ctx.pool, "janitor-full", "completed").await,
        ),
        (
            "cancelled",
            insert_job_with_status(&ctx.pool, "janitor-full", "cancelled").await,
        ),
        (
            "critical_failure",
            insert_job_with_status(&ctx.pool, "janitor-full", "critical_failure").await,
        ),
        (
            "unprocessable",
            insert_job_with_status(&ctx.pool, "janitor-full", "unprocessable").await,
        ),
        (
            "bad_job",
            insert_job_with_status(&ctx.pool, "janitor-full", "bad_job").await,
        ),
    ];

    // Jobs that should remain in job_queue
    let to_keep = [(
        "running",
        insert_job_with_status(&ctx.pool, "janitor-full", "running").await,
    )];

    let janitor = queue.janitor().await;
    janitor.run_archiver().await.expect("run_archiver failed");
    janitor.run_dlq().await.expect("run_dlq failed");

    for (status, id) in &to_archive {
        assert_eq!(
            count_in(&ctx.pool, "job_queue", *id).await,
            0,
            "status={status}: should be gone from job_queue"
        );
        assert_eq!(
            count_in(&ctx.pool, "job_queue_archive", *id).await,
            1,
            "status={status}: should be in job_queue_archive"
        );
    }

    for (status, id) in &to_keep {
        assert_eq!(
            count_in(&ctx.pool, "job_queue", *id).await,
            1,
            "status={status}: should stay in job_queue"
        );
        assert_eq!(
            count_in(&ctx.pool, "job_queue_archive", *id).await,
            0,
            "status={status}: must NOT be in job_queue_archive"
        );
    }
}

// ---------------------------------------------------------------------------
// End-to-end: queue processes a job to completion, janitor archives it
// ---------------------------------------------------------------------------

struct SuccessHandler;
impl Handler for SuccessHandler {
    const QUEUE: &'static str = "janitor-e2e-success";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::Success)
    }
}

struct CancelHandler;
impl Handler for CancelHandler {
    const QUEUE: &'static str = "janitor-e2e-cancel";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::Cancel)
    }
}

struct CriticalHandler;
impl Handler for CriticalHandler {
    const QUEUE: &'static str = "janitor-e2e-critical";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::Critical)
    }
}

struct UnprocessableHandler;
impl Handler for UnprocessableHandler {
    const QUEUE: &'static str = "janitor-e2e-unprocessable";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::Unprocessable)
    }
}
/// Wait until a job lands in job_queue_archive (i.e. after janitor sweeps it).
async fn wait_for_archive(
    pool: &sqlx::PgPool,
    id: uuid::Uuid,
    timeout_secs: u64,
) -> Result<(), String> {
    let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
    while std::time::Instant::now() < deadline {
        if count_in(pool, "job_queue_archive", id).await == 1 {
            return Ok(());
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    Err(format!(
        "Timeout waiting for job {id} to appear in job_queue_archive"
    ))
}

/// Wait until the job_queue row for `id` disappears.
#[allow(dead_code)]
async fn wait_for_removal(
    pool: &sqlx::PgPool,
    id: uuid::Uuid,
    timeout_secs: u64,
) -> Result<(), String> {
    let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
    while std::time::Instant::now() < deadline {
        if count_in(pool, "job_queue", id).await == 0 {
            return Ok(());
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    Err(format!(
        "Timeout waiting for job {id} to be removed from job_queue"
    ))
}

/// Full round-trip: queue processes the job to `completed`, then the archiver
/// sweeps it into job_queue_archive.
#[tokio::test(flavor = "multi_thread")]
async fn test_e2e_completed_job_is_archived() {
    let ctx = TestContext::new().await;

    // Use a short janitor interval so the background task fires quickly.
    let queue = spawn_queue_with(&ctx.pool, |q| {
        q.with_janitor_interval(tokio::time::Duration::from_millis(200))
            .with_empty_poll_sleep(tokio::time::Duration::from_millis(100))
            .with_heartbeat_interval(tokio::time::Duration::from_millis(500))
            .with_default_backoff_strategy(simple_queue::sync::BackoffStrategy::Linear {
                delay: chrono::Duration::milliseconds(100),
            })
            .with_max_reprocess_count(10)
    })
    .await;

    queue.register_handler(SuccessHandler);

    let job = Job::new("janitor-e2e-success", serde_json::json!({}));
    let id = job.id;
    queue.insert_job(job).await.unwrap();

    wait_for_archive(&ctx.pool, id, 10)
        .await
        .expect("completed job should reach job_queue_archive");

    assert_eq!(
        count_in(&ctx.pool, "job_queue", id).await,
        0,
        "completed job must not remain in job_queue"
    );
}

/// Full round-trip: queue sets status to `cancelled`, DLQ sweeps it to archive.
#[tokio::test(flavor = "multi_thread")]
async fn test_e2e_cancelled_job_is_archived_by_dlq() {
    let ctx = TestContext::new().await;

    let queue = spawn_queue_with(&ctx.pool, |q| {
        q.with_janitor_interval(tokio::time::Duration::from_millis(200))
            .with_empty_poll_sleep(tokio::time::Duration::from_millis(100))
            .with_heartbeat_interval(tokio::time::Duration::from_millis(500))
            .with_default_backoff_strategy(simple_queue::sync::BackoffStrategy::Linear {
                delay: chrono::Duration::milliseconds(100),
            })
            .with_max_reprocess_count(10)
    })
    .await;

    queue.register_handler(CancelHandler);

    let job = Job::new("janitor-e2e-cancel", serde_json::json!({}));
    let id = job.id;
    queue.insert_job(job).await.unwrap();

    wait_for_archive(&ctx.pool, id, 10)
        .await
        .expect("cancelled job should reach job_queue_archive via DLQ");

    assert_eq!(
        count_in(&ctx.pool, "job_queue", id).await,
        0,
        "cancelled job must not remain in job_queue"
    );
}

/// Full round-trip: critical_failure status is swept by the DLQ.
#[tokio::test(flavor = "multi_thread")]
async fn test_e2e_critical_job_is_archived_by_dlq() {
    let ctx = TestContext::new().await;

    let queue = spawn_queue_with(&ctx.pool, |q| {
        q.with_janitor_interval(tokio::time::Duration::from_millis(200))
            .with_empty_poll_sleep(tokio::time::Duration::from_millis(100))
            .with_heartbeat_interval(tokio::time::Duration::from_millis(500))
            .with_default_backoff_strategy(simple_queue::sync::BackoffStrategy::Linear {
                delay: chrono::Duration::milliseconds(100),
            })
            .with_max_reprocess_count(10)
    })
    .await;

    queue.register_handler(CriticalHandler);

    let job = Job::new("janitor-e2e-critical", serde_json::json!({}));
    let id = job.id;
    queue.insert_job(job).await.unwrap();

    wait_for_archive(&ctx.pool, id, 10)
        .await
        .expect("critical_failure job should reach job_queue_archive via DLQ");

    assert_eq!(
        count_in(&ctx.pool, "job_queue", id).await,
        0,
        "critical_failure job must not remain in job_queue"
    );
}

/// Full round-trip: unprocessable status is swept by the DLQ.
#[tokio::test(flavor = "multi_thread")]
async fn test_e2e_unprocessable_job_is_archived_by_dlq() {
    let ctx = TestContext::new().await;

    let queue = spawn_queue_with(&ctx.pool, |q| {
        q.with_janitor_interval(tokio::time::Duration::from_millis(200))
            .with_empty_poll_sleep(tokio::time::Duration::from_millis(100))
            .with_heartbeat_interval(tokio::time::Duration::from_millis(500))
            .with_default_backoff_strategy(simple_queue::sync::BackoffStrategy::Linear {
                delay: chrono::Duration::milliseconds(100),
            })
            .with_max_reprocess_count(10)
    })
    .await;

    queue.register_handler(UnprocessableHandler);

    let job = Job::new("janitor-e2e-unprocessable", serde_json::json!({}));
    let id = job.id;
    queue.insert_job(job).await.unwrap();

    wait_for_archive(&ctx.pool, id, 10)
        .await
        .expect("unprocessable job should reach job_queue_archive via DLQ");

    assert_eq!(
        count_in(&ctx.pool, "job_queue", id).await,
        0,
        "unprocessable job must not remain in job_queue"
    );
}