simple-queue 0.1.2

A simple persistent queue implementation in Rust backed by PostgreSQL and tokio
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
// simple_queue/tests/integration_test.rs
mod setup;
use setup::*;
use simple_queue::prelude::*;
use sqlx::PgPool;
use std::time::Duration;

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

struct FailHandler;
impl Handler for FailHandler {
    const QUEUE: &'static str = "test-fail";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::Failed)
    }
}

struct MaxAttemptsFailHandler;
impl Handler for MaxAttemptsFailHandler {
    const QUEUE: &'static str = "test-max-attempts-fail";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::Failed)
    }
}

struct CancelHandler;
impl Handler for CancelHandler {
    const QUEUE: &'static str = "test-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 = "test-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 = "test-unprocessable";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::Unprocessable)
    }
}

struct RetryAtHandler;
impl Handler for RetryAtHandler {
    const QUEUE: &'static str = "test-retry-at";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::RetryAt(
            chrono::Utc::now() + chrono::Duration::seconds(5),
        ))
    }
}

struct RescheduleHandler;
impl Handler for RescheduleHandler {
    const QUEUE: &'static str = "test-reschedule";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::RescheduleAt(
            chrono::Utc::now() + chrono::Duration::seconds(5),
        ))
    }
}
struct RescheduleImmediateHandler;
impl Handler for RescheduleImmediateHandler {
    const QUEUE: &'static str = "test-reschedule-immediate";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        Ok(JobResult::RescheduleAt(
            chrono::Utc::now() + chrono::Duration::milliseconds(10),
        ))
    }
}

struct WorkHandler;
impl Handler for WorkHandler {
    const QUEUE: &'static str = "test-work";
    async fn process(&self, _queue: &SimpleQueue, _job: &Job) -> Result<JobResult, BoxDynError> {
        tokio::time::sleep(Duration::from_millis(10)).await;
        Ok(JobResult::Success)
    }
}

async fn wait_for_status(
    pool: &PgPool,
    queue: &str,
    expected: &str,
    timeout_secs: u64,
) -> Result<String, String> {
    let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
    while std::time::Instant::now() < deadline {
        let result: Result<(String,), _> = sqlx::query_as(&format!(
            "SELECT status FROM job_queue WHERE queue = '{}'",
            queue
        ))
        .fetch_one(pool)
        .await;
        if let Ok((status,)) = result {
            if status == expected {
                return Ok(status);
            }
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    Err(format!(
        "Timeout waiting for status {} on queue {}",
        expected, queue
    ))
}

async fn wait_for_count(
    pool: &PgPool,
    query: &str,
    expected: i64,
    timeout_secs: u64,
) -> Result<i64, String> {
    let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
    while std::time::Instant::now() < deadline {
        let result: Result<(i64,), _> = sqlx::query_as(query).fetch_one(pool).await;
        if let Ok((count,)) = result {
            if count == expected {
                return Ok(count);
            }
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    Err(format!("Timeout waiting for count {}", expected))
}
async fn wait_for_at_least(
    pool: &PgPool,
    query: &str,
    expected: i64,
    timeout_secs: u64,
) -> Result<i64, String> {
    let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
    let mut last_count = 0;
    while std::time::Instant::now() < deadline {
        let result: Result<(i64,), _> = sqlx::query_as(query).fetch_one(pool).await;
        if let Ok((count,)) = result {
            last_count = count;
            if count >= expected {
                return Ok(count);
            }
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    Err(format!(
        "Timeout waiting for count {} - got {}",
        expected, last_count
    ))
}

#[tokio::test(flavor = "multi_thread")]
async fn test_job_creation() {
    let ctx = TestContext::new().await;
    let job = Job::new("test", serde_json::json!({"key": "value"}));
    assert_eq!(job.queue, "test");
    assert_eq!(job.status, "pending");
    assert_eq!(job.attempt, 0);
    assert_eq!(job.max_attempts, 3);
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_insert_and_process_success() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(SuccessHandler);
    queue
        .insert_job(Job::new("test-success", serde_json::json!({})))
        .await
        .unwrap();
    let result = wait_for_status(&ctx.pool, "test-success", "completed", 5).await;
    assert_eq!(result.unwrap(), "completed");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_cancel_result() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(CancelHandler);
    queue
        .insert_job(Job::new("test-cancel", serde_json::json!({})))
        .await
        .unwrap();
    let result = wait_for_status(&ctx.pool, "test-cancel", "cancelled", 5).await;
    assert_eq!(result.unwrap(), "cancelled");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_failed_result() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(FailHandler);
    queue
        .insert_job(Job::new("test-fail", serde_json::json!({})))
        .await
        .unwrap();
    let result = wait_for_status(&ctx.pool, "test-fail", "pending", 5).await;
    // Failed jobs become pending with a future run_at (retry mechanism)
    assert_eq!(result.unwrap(), "pending");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_critical_result() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(CriticalHandler);
    queue
        .insert_job(Job::new("test-critical", serde_json::json!({})))
        .await
        .unwrap();
    let result = wait_for_status(&ctx.pool, "test-critical", "critical_failure", 5).await;
    assert_eq!(result.unwrap(), "critical_failure");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_unprocessable_result() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(UnprocessableHandler);
    queue
        .insert_job(Job::new("test-unprocessable", serde_json::json!({})))
        .await
        .unwrap();
    let result = wait_for_status(&ctx.pool, "test-unprocessable", "unprocessable", 5).await;
    assert_eq!(result.unwrap(), "unprocessable");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_retry_at_result() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(RetryAtHandler);
    queue
        .insert_job(Job::new("test-retry-at", serde_json::json!({})))
        .await
        .unwrap();

    let deadline = std::time::Instant::now() + Duration::from_secs(5);
    let mut success = false;
    while std::time::Instant::now() < deadline {
        let result: Result<(String, Option<chrono::DateTime<chrono::Utc>>), _> =
            sqlx::query_as("SELECT status, run_at FROM job_queue WHERE queue = 'test-retry-at'")
                .fetch_one(&ctx.pool)
                .await;
        if let Ok((status, run_at)) = result {
            if status == "pending" && run_at.is_some() && run_at.unwrap() > chrono::Utc::now() {
                success = true;
                break;
            }
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    assert!(success, "Expected pending status with future run_at");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_reschedule_at_result() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(RescheduleHandler);
    queue
        .insert_job(Job::new("test-reschedule", serde_json::json!({})))
        .await
        .unwrap();

    let deadline = std::time::Instant::now() + Duration::from_secs(5);
    let mut success = false;
    while std::time::Instant::now() < deadline {
        let result: Result<(String, i32, Option<chrono::DateTime<chrono::Utc>>), _> =
            sqlx::query_as(
                "SELECT status, attempt, run_at FROM job_queue WHERE queue = 'test-reschedule'",
            )
            .fetch_one(&ctx.pool)
            .await;
        if let Ok((status, attempt, run_at)) = result {
            if status == "pending" && attempt == 0 && run_at.is_some() {
                success = true;
                break;
            }
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    assert!(
        success,
        "Expected pending status with attempt=0 and future run_at"
    );
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_unique_key_no_duplicate() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(SuccessHandler);
    queue
        .insert_job(Job::new("test-unique-key", serde_json::json!({})).with_unique_key("unique-1"))
        .await
        .unwrap();
    queue
        .insert_job(Job::new("test-unique-key", serde_json::json!({})).with_unique_key("unique-1"))
        .await
        .unwrap();
    tokio::time::sleep(Duration::from_millis(500)).await;

    let count: (i64,) =
        sqlx::query_as("SELECT COUNT(*) FROM job_queue WHERE queue = 'test-unique-key'")
            .fetch_one(&ctx.pool)
            .await
            .unwrap();
    assert_eq!(count.0, 1);
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_missing_handler() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue
        .insert_job(Job::new("unregistered-queue", serde_json::json!({})))
        .await
        .unwrap();
    let result = wait_for_status(&ctx.pool, "unregistered-queue", "critical_failure", 5).await;
    assert_eq!(result.unwrap(), "critical_failure");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_max_attempts() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(MaxAttemptsFailHandler);
    queue
        .insert_job(Job::new("test-max-attempts-fail", serde_json::json!({})).with_max_attempts(5))
        .await
        .unwrap();

    let deadline = std::time::Instant::now() + Duration::from_secs(3);
    let mut success = false;
    while std::time::Instant::now() < deadline {
        let result: Result<(String, i32, chrono::DateTime<chrono::Utc>), _> = sqlx::query_as(
            "SELECT status, attempt, run_at FROM job_queue WHERE queue = 'test-max-attempts-fail'",
        )
        .fetch_one(&ctx.pool)
        .await;
        if let Ok((status, attempt, run_at)) = result {
            tracing::info!(
                "status: {}, attempt: {}, run_at: {}",
                status,
                attempt,
                run_at
            );
            if status == "failed" && attempt == 5 {
                success = true;
                break;
            }
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
    assert!(success, "Expected failed status with attempt=5");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_concurrent_processing() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(WorkHandler);

    for i in 0..20 {
        queue
            .insert_job(Job::new("test-work", serde_json::json!({"index": i})))
            .await
            .unwrap();
    }

    let result = wait_for_count(
        &ctx.pool,
        "SELECT COUNT(*) FROM job_queue WHERE status = 'completed' AND queue = 'test-work'",
        20,
        10,
    )
    .await;
    assert_eq!(result.unwrap(), 20);
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_job_with_fingerprint() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(SuccessHandler);
    queue
        .insert_job(Job::new("test-success", serde_json::json!({})).with_fingerprint("fp-123"))
        .await
        .unwrap();
    let result = wait_for_status(&ctx.pool, "test-success", "completed", 5).await;
    assert_eq!(result.unwrap(), "completed");

    let row: (Option<String>,) =
        sqlx::query_as("SELECT fingerprint FROM job_queue WHERE fingerprint = 'fp-123'")
            .fetch_one(&ctx.pool)
            .await
            .unwrap();
    assert_eq!(row.0, Some("fp-123".to_string()));
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_poison_job() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;
    queue.register_handler(RescheduleImmediateHandler);
    let queue_name = RescheduleImmediateHandler::QUEUE;
    queue
        .insert_job(Job::new(queue_name, serde_json::json!({})))
        .await
        .unwrap();

    tracing::info!("test");
    let deadline = std::time::Instant::now() + Duration::from_secs(5);
    let mut success = false;
    while std::time::Instant::now() < deadline {
        let result: Result<(String, i32, chrono::DateTime<chrono::Utc>, i32), _> = sqlx::query_as(
            "SELECT status, attempt, run_at, reprocess_count FROM job_queue WHERE queue = $1",
        )
        .bind(queue_name)
        .fetch_one(&ctx.pool)
        .await;
        if let Ok((status, attempt, run_at, reprocess_count)) = result {
            tracing::info!(
                "status: {}, attempt: {}, run_at: {}, reprocess_count: {}",
                status,
                attempt,
                run_at,
                reprocess_count
            );
            if status == "bad_job" {
                success = true;
                break;
            }
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
    assert!(success, "Expected failed status with attempt=5");
    ctx.cleanup().await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_queue_isolation() {
    let ctx = TestContext::new().await;
    let queue = spawn_queue(&ctx.pool).await;

    // Register slow handler with semaphore limit of 10
    struct SlowHandler;
    impl Handler for SlowHandler {
        const QUEUE: &'static str = "slow-queue";
        async fn process(
            &self,
            _queue: &SimpleQueue,
            _job: &Job,
        ) -> Result<JobResult, BoxDynError> {
            // Simulate slow work
            tokio::time::sleep(Duration::from_millis(500)).await;
            Ok(JobResult::Success)
        }
    }

    // Register fast handler
    struct FastHandler;
    impl Handler for FastHandler {
        const QUEUE: &'static str = "fast-queue";
        async fn process(
            &self,
            _queue: &SimpleQueue,
            _job: &Job,
        ) -> Result<JobResult, BoxDynError> {
            // Simulate fast work
            tokio::time::sleep(Duration::from_millis(10)).await;
            Ok(JobResult::Success)
        }
    }

    queue.register_handler(SlowHandler);
    queue.register_handler(FastHandler);

    let qc = queue.clone();
    let _ = tokio::spawn(async move {
        let queue = qc;
        let qc = queue.clone();
        tokio::spawn(async move {
            for i in 0..1000 {
                qc.insert_job(Job::new("slow-queue", serde_json::json!({"index": i})))
                    .await
                    .unwrap();
            }
        });

        let qc = queue.clone();
        tokio::spawn(async move {
            // Add few jobs to fast queue
            for i in 0..500 {
                qc.insert_job(Job::new("fast-queue", serde_json::json!({"index": i})))
                    .await
                    .unwrap();
            }
        });
    })
    .await;
    tracing::info!("waiting for fast-queue count");
    // Wait for fast queue jobs to complete
    let result = wait_for_at_least(
        &ctx.pool,
        "SELECT COUNT(*) FROM job_queue WHERE status = 'completed' AND queue = 'fast-queue'",
        500,
        5,
    )
    .await;

    //handle.abort();
    assert_eq!(result.unwrap(), 500);

    // Verify slow queue jobs are still processing (not blocked by fast queue)
    let unprocessed_slow_count: (i64,) = sqlx::query_as(
        "SELECT COUNT(*) FROM job_queue WHERE queue = 'slow-queue' AND status != 'completed'",
    )
    .fetch_one(&ctx.pool)
    .await
    .unwrap();

    // At least some slow jobs should still be processing (not all completed due to semaphore limit)
    tracing::info!("unprocessed_slow_count: {}", unprocessed_slow_count.0);
    assert!(unprocessed_slow_count.0 > 200);

    ctx.cleanup().await;
}