hammerwork 2.4.0

A high-performance, database-driven job queue for Rust with PostgreSQL and MySQL support, featuring job prioritization, cron scheduling, event streaming (Kafka/Kinesis/PubSub), webhooks, rate limiting, Prometheus metrics, and comprehensive monitoring
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
//! #71: listings skip rows they cannot decode instead of failing as a whole, while reads
//! of a single job keep returning the error. The corrupt rows are written with raw SQL
//! (a negative `timeout_seconds`, as older versions could store) and removed afterwards.

#![cfg(any(feature = "postgres", feature = "mysql"))]

mod test_utils;

use chrono::Duration;
use hammerwork::{
    Job, JobId,
    archive::{ArchivalConfig, ArchivalPolicy, ArchivalReason},
    cron::CronSchedule,
    queue::DatabaseQueue,
};
use serde_json::json;
use std::collections::HashSet;
use std::future::Future;

fn ids<'a>(ids: impl IntoIterator<Item = &'a JobId>) -> HashSet<JobId> {
    ids.into_iter().copied().collect()
}

/// Every listing of `queue` returns the healthy jobs and skips the corrupt ones.
///
/// `sql` runs one statement on the queue's database. The statements only contain ids and
/// queue names this test generated.
async fn listings_skip_undecodable_rows<Q, S, F>(queue: &Q, sql: S)
where
    Q: DatabaseQueue + Send + Sync,
    S: Fn(String) -> F,
    F: Future<Output = ()>,
{
    let name = test_utils::unique_queue("undecodable");
    let archive_name = test_utils::unique_queue("undecodable_archive");
    let corrupt = |id: JobId, table: &str| {
        let column = if table == "hammerwork_jobs" {
            "timeout_seconds"
        } else {
            "original_payload_size"
        };
        sql(format!(
            "UPDATE {table} SET {column} = -1 WHERE id = '{id}'"
        ))
    };
    let enqueue = |queue_name: &str| queue.enqueue(Job::new(queue_name.to_string(), json!({})));

    // Ready jobs
    let ready = enqueue(&name).await.unwrap();
    let bad_ready = enqueue(&name).await.unwrap();
    corrupt(bad_ready, "hammerwork_jobs").await;
    let listed = queue.get_ready_jobs(&name, 10).await.unwrap();
    assert_eq!(ids(listed.iter().map(|j| &j.id)), ids([&ready]));

    // A single read still reports the corrupt row.
    let err = queue.get_job(bad_ready).await.unwrap_err();
    assert!(err.to_string().contains("timeout_seconds"), "{err}");

    // Dead jobs, per queue and overall
    let dead = enqueue(&name).await.unwrap();
    queue.mark_job_dead(dead, "gone").await.unwrap();
    let bad_dead = enqueue(&name).await.unwrap();
    queue.mark_job_dead(bad_dead, "gone").await.unwrap();
    corrupt(bad_dead, "hammerwork_jobs").await;
    let listed = queue
        .get_dead_jobs_by_queue(&name, Some(10), Some(0))
        .await
        .unwrap();
    assert_eq!(ids(listed.iter().map(|j| &j.id)), ids([&dead]));
    let all_dead = ids(queue
        .get_dead_jobs(Some(10_000), Some(0))
        .await
        .unwrap()
        .iter()
        .map(|j| &j.id));
    assert!(all_dead.contains(&dead) && !all_dead.contains(&bad_dead));

    // Recurring jobs
    let hourly = CronSchedule::new("0 0 * * * *").unwrap();
    let cron_job = || Job::with_cron_schedule(name.clone(), json!({}), hourly.clone()).unwrap();
    let recurring = queue.enqueue_cron_job(cron_job()).await.unwrap();
    let bad_recurring = queue.enqueue_cron_job(cron_job()).await.unwrap();
    corrupt(bad_recurring, "hammerwork_jobs").await;
    let listed = queue.get_recurring_jobs(&name).await.unwrap();
    assert_eq!(ids(listed.iter().map(|j| &j.id)), ids([&recurring]));

    // Archived jobs
    let archived = enqueue(&archive_name).await.unwrap();
    let bad_archived = enqueue(&archive_name).await.unwrap();
    for id in [archived, bad_archived] {
        queue.mark_job_dead(id, "gone").await.unwrap();
    }
    let policy = ArchivalPolicy::new()
        .archive_dead_after(Duration::seconds(0))
        .enabled(true);
    let stats = queue
        .archive_jobs(
            Some(&archive_name),
            &policy,
            &ArchivalConfig::new(),
            ArchivalReason::Manual,
            None,
        )
        .await
        .unwrap();
    assert_eq!(stats.jobs_archived, 2);
    corrupt(bad_archived, "hammerwork_jobs_archive").await;
    let listed = queue
        .list_archived_jobs(Some(&archive_name), Some(10), Some(0))
        .await
        .unwrap();
    assert_eq!(ids(listed.iter().map(|j| &j.id)), ids([&archived]));

    for (table, queue_name) in [
        ("hammerwork_jobs", &name),
        ("hammerwork_jobs_archive", &archive_name),
    ] {
        sql(format!(
            "DELETE FROM {table} WHERE queue_name = '{queue_name}'"
        ))
        .await;
    }
}

#[cfg(feature = "postgres")]
#[tokio::test]
#[ignore = "requires PostgreSQL: DATABASE_URL"]
async fn postgres_listings_skip_undecodable_rows() {
    let queue = test_utils::setup_postgres_queue().await;
    let pool = sqlx::PgPool::connect(&test_utils::postgres_url())
        .await
        .unwrap();
    listings_skip_undecodable_rows(queue.as_ref(), |statement| {
        let pool = pool.clone();
        async move {
            sqlx::query(&statement).execute(&pool).await.unwrap();
        }
    })
    .await;
}

#[cfg(feature = "mysql")]
#[tokio::test]
#[ignore = "requires MySQL: MYSQL_DATABASE_URL"]
async fn mysql_listings_skip_undecodable_rows() {
    let queue = test_utils::setup_mysql_queue().await;
    let pool = sqlx::MySqlPool::connect(&test_utils::mysql_url())
        .await
        .unwrap();
    listings_skip_undecodable_rows(queue.as_ref(), |statement| {
        let pool = pool.clone();
        async move {
            sqlx::query(&statement).execute(&pool).await.unwrap();
        }
    })
    .await;
}

/// The stale job reaper reclaims a job whose row cannot be decoded (requeued, or dead
/// once out of attempts) instead of failing, which would leave every stale job of every
/// queue unreclaimed for as long as the corrupt row stays `Running`.
async fn reaper_reclaims_undecodable_rows<Q, S, F>(queue: &Q, sql: S)
where
    Q: DatabaseQueue + Send + Sync,
    S: Fn(String) -> F,
    F: Future<Output = ()>,
{
    let name = test_utils::unique_queue("undecodable_reaper");
    let enqueue = |max_attempts: i32| {
        queue.enqueue(Job::new(name.clone(), json!({})).with_max_attempts(max_attempts))
    };
    let healthy = enqueue(3).await.unwrap();
    let bad_requeued = enqueue(3).await.unwrap();
    let bad_dead = enqueue(1).await.unwrap();

    // Claim all three with a lease that has expired by the time the reaper runs.
    let claimed = queue
        .dequeue_batch_leased(&name, None, std::time::Duration::from_millis(1), 10)
        .await
        .unwrap();
    assert_eq!(claimed.len(), 3);
    for id in [bad_requeued, bad_dead] {
        sql(format!(
            "UPDATE hammerwork_jobs SET timeout_seconds = -1 WHERE id = '{id}'"
        ))
        .await;
    }
    tokio::time::sleep(std::time::Duration::from_millis(50)).await;

    // Another test's reaper may reclaim some of them first; either way they end up
    // reclaimed and this call succeeds.
    queue
        .requeue_stale_jobs(std::time::Duration::from_secs(3600))
        .await
        .expect("an undecodable stale row must not fail the reaper");
    let counts = queue.get_job_counts_by_status(&name).await.unwrap();
    assert_eq!(counts.get("Pending"), Some(&2), "{counts:?}");
    assert_eq!(counts.get("Dead"), Some(&1), "{counts:?}");
    assert_eq!(
        queue.get_job(healthy).await.unwrap().unwrap().status,
        hammerwork::JobStatus::Pending
    );

    sql(format!(
        "DELETE FROM hammerwork_jobs WHERE queue_name = '{name}'"
    ))
    .await;
}

/// A job whose row cannot be decoded does not block its queue: a claim takes it out of
/// the way (it stays `Running` until its lease expires, then the reaper handles it) and
/// the next claim gets the jobs behind it, for single and batch claims alike.
async fn undecodable_rows_do_not_block_claims<Q, S, F>(queue: &Q, sql: S)
where
    Q: DatabaseQueue + Send + Sync,
    S: Fn(String) -> F,
    F: Future<Output = ()>,
{
    use hammerwork::priority::JobPriority;

    for batch in [false, true] {
        let name = test_utils::unique_queue("undecodable_claim");
        let bad = queue
            .enqueue(Job::new(name.clone(), json!({})).with_priority(JobPriority::Critical))
            .await
            .unwrap();
        let healthy = queue
            .enqueue(Job::new(name.clone(), json!({})))
            .await
            .unwrap();
        sql(format!(
            "UPDATE hammerwork_jobs SET timeout_seconds = -1 WHERE id = '{bad}'"
        ))
        .await;

        // The first claim takes the corrupt job (an error for a single claim, nothing
        // for a batch); the healthy job behind it is claimed next.
        let mut claimed: Vec<JobId> = Vec::new();
        for _ in 0..3 {
            let jobs = if batch {
                queue.dequeue_batch(&name, 1).await
            } else {
                queue
                    .dequeue(&name)
                    .await
                    .map(|job| job.into_iter().collect())
            };
            if let Ok(jobs) = jobs {
                claimed.extend(jobs.into_iter().map(|job| job.id));
            }
            if !claimed.is_empty() {
                break;
            }
        }
        assert_eq!(
            claimed,
            vec![healthy],
            "the corrupt job blocked the queue (batch: {batch})"
        );

        sql(format!(
            "DELETE FROM hammerwork_jobs WHERE queue_name = '{name}'"
        ))
        .await;
    }
}

#[cfg(feature = "postgres")]
fn postgres_sql(
    pool: sqlx::PgPool,
) -> impl Fn(String) -> std::pin::Pin<Box<dyn Future<Output = ()>>> {
    move |statement| {
        let pool = pool.clone();
        Box::pin(async move {
            sqlx::query(&statement).execute(&pool).await.unwrap();
        })
    }
}

#[cfg(feature = "mysql")]
fn mysql_sql(
    pool: sqlx::MySqlPool,
) -> impl Fn(String) -> std::pin::Pin<Box<dyn Future<Output = ()>>> {
    move |statement| {
        let pool = pool.clone();
        Box::pin(async move {
            sqlx::query(&statement).execute(&pool).await.unwrap();
        })
    }
}

#[cfg(feature = "postgres")]
#[tokio::test]
#[ignore = "requires PostgreSQL: DATABASE_URL"]
async fn postgres_reaper_reclaims_undecodable_rows() {
    let queue = test_utils::setup_postgres_queue().await;
    let pool = sqlx::PgPool::connect(&test_utils::postgres_url())
        .await
        .unwrap();
    reaper_reclaims_undecodable_rows(queue.as_ref(), postgres_sql(pool)).await;
}

#[cfg(feature = "mysql")]
#[tokio::test]
#[ignore = "requires MySQL: MYSQL_DATABASE_URL"]
async fn mysql_reaper_reclaims_undecodable_rows() {
    let queue = test_utils::setup_mysql_queue().await;
    let pool = sqlx::MySqlPool::connect(&test_utils::mysql_url())
        .await
        .unwrap();
    reaper_reclaims_undecodable_rows(queue.as_ref(), mysql_sql(pool)).await;
}

#[cfg(feature = "postgres")]
#[tokio::test]
#[ignore = "requires PostgreSQL: DATABASE_URL"]
async fn postgres_undecodable_rows_do_not_block_claims() {
    let queue = test_utils::setup_postgres_queue().await;
    let pool = sqlx::PgPool::connect(&test_utils::postgres_url())
        .await
        .unwrap();
    undecodable_rows_do_not_block_claims(queue.as_ref(), postgres_sql(pool)).await;
}

#[cfg(feature = "mysql")]
#[tokio::test]
#[ignore = "requires MySQL: MYSQL_DATABASE_URL"]
async fn mysql_undecodable_rows_do_not_block_claims() {
    let queue = test_utils::setup_mysql_queue().await;
    let pool = sqlx::MySqlPool::connect(&test_utils::mysql_url())
        .await
        .unwrap();
    undecodable_rows_do_not_block_claims(queue.as_ref(), mysql_sql(pool)).await;
}

/// A job whose row cannot be decoded does not stop the archiver: the healthy jobs of the
/// pass are archived and the corrupt row is skipped (and left in `hammerwork_jobs`),
/// instead of the whole pass failing and rolling back on every run.
async fn undecodable_rows_do_not_block_archival<Q, S, F>(queue: &Q, sql: S)
where
    Q: DatabaseQueue + Send + Sync,
    S: Fn(String) -> F,
    F: Future<Output = ()>,
{
    let name = test_utils::unique_queue("undecodable_archiver");
    // The corrupt jobs are the oldest and fill a whole batch, so they come first.
    let mut bad = Vec::new();
    for _ in 0..3 {
        bad.push(
            queue
                .enqueue(Job::new(name.clone(), json!({})))
                .await
                .unwrap(),
        );
    }
    let healthy = queue
        .enqueue(Job::new(name.clone(), json!({})))
        .await
        .unwrap();
    for id in bad.iter().chain([&healthy]) {
        queue.complete_job(*id).await.unwrap();
    }
    for id in &bad {
        sql(format!(
            "UPDATE hammerwork_jobs SET timeout_seconds = -1 WHERE id = '{id}'"
        ))
        .await;
    }

    // One batch: the undecodable rows are skipped and the batch topped up.
    let policy = ArchivalPolicy::new()
        .archive_completed_after(Duration::seconds(0))
        .with_batch_size(2)
        .enabled(true);
    let stats = queue
        .archive_jobs(
            Some(&name),
            &policy,
            &ArchivalConfig::new(),
            ArchivalReason::Automatic,
            None,
        )
        .await
        .expect("an undecodable row must not fail the archiver");
    assert_eq!(stats.jobs_archived, 1);
    let archived = queue
        .list_archived_jobs(Some(&name), Some(10), None)
        .await
        .unwrap();
    assert_eq!(
        archived.iter().map(|job| job.id).collect::<Vec<_>>(),
        vec![healthy]
    );

    sql(format!(
        "DELETE FROM hammerwork_jobs WHERE queue_name = '{name}'"
    ))
    .await;
    sql(format!(
        "DELETE FROM hammerwork_jobs_archive WHERE queue_name = '{name}'"
    ))
    .await;
}

#[cfg(feature = "postgres")]
#[tokio::test]
#[ignore = "requires PostgreSQL: DATABASE_URL"]
async fn postgres_undecodable_rows_do_not_block_archival() {
    let queue = test_utils::setup_postgres_queue().await;
    let pool = sqlx::PgPool::connect(&test_utils::postgres_url())
        .await
        .unwrap();
    undecodable_rows_do_not_block_archival(queue.as_ref(), postgres_sql(pool)).await;
}

#[cfg(feature = "mysql")]
#[tokio::test]
#[ignore = "requires MySQL: MYSQL_DATABASE_URL"]
async fn mysql_undecodable_rows_do_not_block_archival() {
    let queue = test_utils::setup_mysql_queue().await;
    let pool = sqlx::MySqlPool::connect(&test_utils::mysql_url())
        .await
        .unwrap();
    undecodable_rows_do_not_block_archival(queue.as_ref(), mysql_sql(pool)).await;
}