hammerwork 2.2.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
//! How a job's terminal failure is applied, on both backends and on `TestQueue`:
//! `fail_job` is terminal, and a workflow's `FailurePolicy` decides what happens to
//! the other jobs (`fail_job`, a worker's `finish_job_run` and the explicit
//! `fail_job_dependencies` helper all apply it the same way).

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

mod test_utils;

use chrono::Utc;
use hammerwork::{
    Job, JobId, JobStatus,
    queue::{DatabaseQueue, JobOutcome},
    workflow::{FailurePolicy, JobGroup, WorkflowId, WorkflowStatus},
};
use serde_json::json;
use std::sync::Arc;

/// A workflow `a -> b -> c` plus an independent job `x`. Each job has its own queue,
/// so the scenarios dequeue exactly the job they mean.
struct Graph {
    workflow: WorkflowId,
    queue: String,
    a: JobId,
    b: JobId,
    c: JobId,
    x: JobId,
}

impl Graph {
    fn queue_of(&self, step: &str) -> String {
        format!("{}_{step}", self.queue)
    }

    fn ids(&self) -> [JobId; 4] {
        [self.a, self.b, self.c, self.x]
    }
}

async fn enqueue_graph<Q>(queue: &Arc<Q>, policy: FailurePolicy) -> Graph
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    let name = test_utils::unique_queue("failure_policy");
    let job = |step: &str| Job::new(format!("{name}_{step}"), json!({ "step": step }));
    let a = job("a");
    let x = job("x");
    let b = job("b").depends_on(&a.id);
    let c = job("c").depends_on(&b.id);
    let (a_id, b_id, c_id, x_id) = (a.id, b.id, c.id, x.id);
    let workflow = queue
        .enqueue_workflow(
            JobGroup::new(format!("policy-{}", policy.as_str()))
                .with_failure_policy(policy)
                .add_job(a)
                .add_job(x)
                .add_job(b)
                .add_job(c),
        )
        .await
        .unwrap();
    Graph {
        workflow,
        queue: name,
        a: a_id,
        b: b_id,
        c: c_id,
        x: x_id,
    }
}

async fn status_of<Q>(queue: &Arc<Q>, id: JobId) -> JobStatus
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    queue.get_job(id).await.unwrap().expect("job exists").status
}

async fn workflow_status<Q>(queue: &Arc<Q>, graph: &Graph) -> WorkflowStatus
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    queue
        .get_workflow_status(graph.workflow)
        .await
        .unwrap()
        .expect("workflow exists")
        .status
}

/// Dequeue the job of `step`, asserting it is `expected`.
async fn run<Q>(queue: &Arc<Q>, graph: &Graph, step: &str, expected: JobId) -> Job
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    let job = queue
        .dequeue(&graph.queue_of(step))
        .await
        .unwrap()
        .unwrap_or_else(|| panic!("job {step} is runnable"));
    assert_eq!(job.id, expected);
    job
}

/// Whether `step`'s queue has a runnable job.
async fn runnable<Q>(queue: &Arc<Q>, graph: &Graph, step: &str) -> bool
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    !queue
        .get_ready_jobs(&graph.queue_of(step), 10)
        .await
        .unwrap()
        .is_empty()
}

/// A time that is already due on every backend (`TestQueue`'s mock clock starts at
/// its creation and does not advance on its own).
fn past() -> chrono::DateTime<Utc> {
    Utc::now() - chrono::Duration::hours(1)
}

fn sorted(mut ids: Vec<JobId>) -> Vec<JobId> {
    ids.sort();
    ids
}

async fn cleanup<Q>(queue: &Arc<Q>, graph: &Graph)
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    for id in graph.ids() {
        queue.delete_job(id).await.unwrap();
    }
}

/// How the failure of `a` is recorded.
#[derive(Clone, Copy, Debug)]
enum Trigger {
    /// `fail_job`, the manual terminal failure.
    FailJob,
    /// A worker's last attempt failing: `finish_job_run` with a `Dead` outcome.
    WorkerDead,
    /// `fail_job_dependencies` called directly, while `a` is still running.
    Explicit,
}

/// Fail `a` with `trigger`. Returns the jobs the queue reports as failed with it,
/// when the trigger reports them.
async fn fail_a<Q>(queue: &Arc<Q>, graph: &Graph, trigger: Trigger) -> Option<Vec<JobId>>
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    let run_a = run(queue, graph, "a", graph.a).await;
    match trigger {
        Trigger::FailJob => {
            queue.fail_job(graph.a, "a failed").await.unwrap();
            assert_eq!(status_of(queue, graph.a).await, JobStatus::Failed);
            None
        }
        Trigger::WorkerDead => {
            let outcome = JobOutcome::Dead {
                error: "a failed".to_string(),
            };
            let recorded = queue
                .finish_job_run(&run_a, outcome)
                .await
                .unwrap()
                .expect("the run is current");
            assert_eq!(recorded.status, JobStatus::Dead);
            assert_eq!(status_of(queue, graph.a).await, JobStatus::Dead);
            Some(recorded.cancelled)
        }
        Trigger::Explicit => {
            let failed = queue.fail_job_dependencies(graph.a).await.unwrap();
            assert_eq!(status_of(queue, graph.a).await, JobStatus::Running);
            Some(failed)
        }
    }
}

/// `fail_job` is a terminal `Failed`: no automatic retry, `attempts` unchanged, and
/// `retry_job` re-runs it.
async fn fail_job_is_terminal<Q>(queue: Arc<Q>)
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    let queue_name = test_utils::unique_queue("fail_job_terminal");
    let id = queue
        .enqueue(Job::new(queue_name.clone(), json!({})).with_max_attempts(3))
        .await
        .unwrap();
    let job = queue.dequeue(&queue_name).await.unwrap().unwrap();
    assert_eq!(job.attempts, 1);
    queue.fail_job(id, "permanent").await.unwrap();

    let job = queue.get_job(id).await.unwrap().unwrap();
    assert_eq!(job.status, JobStatus::Failed);
    assert_eq!(job.attempts, 1);
    assert_eq!(job.error_message.as_deref(), Some("permanent"));
    assert!(job.failed_at.is_some());
    assert!(queue.dequeue(&queue_name).await.unwrap().is_none());
    assert!(
        queue.fail_job(id, "again").await.is_err(),
        "a Failed job cannot fail again"
    );

    queue.retry_job(id, past()).await.unwrap();
    let job = queue.dequeue(&queue_name).await.unwrap().unwrap();
    assert_eq!(job.id, id);
    assert_eq!(job.attempts, 2);
    queue.delete_job(id).await.unwrap();
}

/// `FailFast`: the whole workflow stops.
async fn fail_fast<Q>(queue: Arc<Q>)
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    for trigger in [Trigger::FailJob, Trigger::WorkerDead, Trigger::Explicit] {
        let graph = enqueue_graph(&queue, FailurePolicy::FailFast).await;
        let reported = fail_a(&queue, &graph, trigger).await;
        if let Some(reported) = reported {
            assert_eq!(
                sorted(reported),
                sorted(vec![graph.b, graph.c, graph.x]),
                "{trigger:?}"
            );
        }
        for id in [graph.b, graph.c, graph.x] {
            let job = queue.get_job(id).await.unwrap().unwrap();
            assert_eq!(job.status, JobStatus::Failed, "{trigger:?}");
            assert!(job.error_message.is_some(), "{trigger:?}");
        }
        for step in ["b", "c", "x"] {
            assert!(!runnable(&queue, &graph, step).await, "{trigger:?} {step}");
        }
        assert_eq!(
            workflow_status(&queue, &graph).await,
            WorkflowStatus::Failed,
            "{trigger:?}"
        );
        // Applying the failure again changes nothing.
        assert!(
            queue
                .fail_job_dependencies(graph.a)
                .await
                .unwrap()
                .is_empty(),
            "{trigger:?}"
        );
        cleanup(&queue, &graph).await;
    }
}

/// `ContinueOnFailure`: the failed job's dependents cannot run, the independent
/// branch continues, and the workflow fails once everything has finished.
async fn continue_on_failure<Q>(queue: Arc<Q>)
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    for trigger in [Trigger::FailJob, Trigger::WorkerDead, Trigger::Explicit] {
        let graph = enqueue_graph(&queue, FailurePolicy::ContinueOnFailure).await;
        let reported = fail_a(&queue, &graph, trigger).await;
        if let Some(reported) = reported {
            assert_eq!(
                sorted(reported),
                sorted(vec![graph.b, graph.c]),
                "{trigger:?}"
            );
        }
        for id in [graph.b, graph.c] {
            let job = queue.get_job(id).await.unwrap().unwrap();
            assert_eq!(job.status, JobStatus::Failed, "{trigger:?}");
            assert!(job.dependencies_failed(), "{trigger:?}");
        }
        assert_eq!(status_of(&queue, graph.x).await, JobStatus::Pending);
        assert_eq!(
            workflow_status(&queue, &graph).await,
            WorkflowStatus::Running,
            "{trigger:?}"
        );

        // The independent branch still runs.
        run(&queue, &graph, "x", graph.x).await;
        if matches!(trigger, Trigger::Explicit) {
            // `a` is still running in this scenario; finish it.
            queue.fail_job(graph.a, "a failed").await.unwrap();
        }
        queue.complete_job(graph.x).await.unwrap();
        assert_eq!(status_of(&queue, graph.x).await, JobStatus::Completed);
        assert_eq!(
            workflow_status(&queue, &graph).await,
            WorkflowStatus::Failed,
            "{trigger:?}: every job finished, one failed"
        );
        cleanup(&queue, &graph).await;
    }
}

/// `Manual`: nothing is failed automatically. The dependents wait until an operator
/// re-runs the failed job, whose completion releases them.
async fn manual<Q>(queue: Arc<Q>)
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    for trigger in [Trigger::FailJob, Trigger::WorkerDead, Trigger::Explicit] {
        let graph = enqueue_graph(&queue, FailurePolicy::Manual).await;
        let reported = fail_a(&queue, &graph, trigger).await;
        if let Some(reported) = reported {
            assert!(reported.is_empty(), "{trigger:?}: {reported:?}");
        }
        for id in [graph.b, graph.c, graph.x] {
            assert_eq!(
                status_of(&queue, id).await,
                JobStatus::Pending,
                "{trigger:?}"
            );
        }
        for id in [graph.b, graph.c] {
            let job = queue.get_job(id).await.unwrap().unwrap();
            assert!(!job.dependencies_failed(), "{trigger:?}");
        }
        assert!(!runnable(&queue, &graph, "b").await, "{trigger:?}");
        assert!(runnable(&queue, &graph, "x").await, "{trigger:?}");
        assert_eq!(
            workflow_status(&queue, &graph).await,
            WorkflowStatus::Running,
            "{trigger:?}"
        );

        // The operator re-runs `a`; its completion releases `b`.
        match trigger {
            Trigger::FailJob => queue.retry_job(graph.a, past()).await.unwrap(),
            Trigger::WorkerDead => queue.retry_dead_job(graph.a).await.unwrap(),
            // `a` is still running in this scenario.
            Trigger::Explicit => {}
        }
        if !matches!(trigger, Trigger::Explicit) {
            run(&queue, &graph, "a", graph.a).await;
        }
        queue.complete_job(graph.a).await.unwrap();
        queue.resolve_job_dependencies(graph.a).await.unwrap();
        run(&queue, &graph, "b", graph.b).await;
        assert_eq!(
            workflow_status(&queue, &graph).await,
            WorkflowStatus::Running,
            "{trigger:?}"
        );
        cleanup(&queue, &graph).await;
    }
}

/// Jobs that depend on a failed job outside any workflow can no longer run.
async fn dependents_outside_workflows<Q>(queue: Arc<Q>)
where
    Q: DatabaseQueue + Send + Sync + 'static,
{
    let queue_name = test_utils::unique_queue("no_workflow");
    let parent = Job::new(queue_name.clone(), json!({ "step": "parent" }));
    let child = Job::new(format!("{queue_name}_child"), json!({})).depends_on(&parent.id);
    let (parent_id, child_id) = (parent.id, child.id);
    // A one-job group without a policy would still be a workflow; enqueue both jobs
    // as plain jobs instead.
    queue.enqueue(parent).await.unwrap();
    queue.enqueue(child).await.unwrap();

    queue.dequeue(&queue_name).await.unwrap().unwrap();
    let failed = queue.fail_job_dependencies(parent_id).await.unwrap();
    assert_eq!(failed, [child_id]);
    let child = queue.get_job(child_id).await.unwrap().unwrap();
    assert_eq!(child.status, JobStatus::Failed);
    assert!(child.dependencies_failed());
    for id in [parent_id, child_id] {
        queue.delete_job(id).await.unwrap();
    }
}

#[cfg(feature = "postgres")]
crate::test_utils::backend_tests!(
    postgres_tests,
    test_utils::setup_postgres_queue,
    ignore = "requires PostgreSQL: DATABASE_URL",
    [
        fail_job_is_terminal,
        fail_fast,
        continue_on_failure,
        manual,
        dependents_outside_workflows,
    ]
);

#[cfg(feature = "mysql")]
crate::test_utils::backend_tests!(
    mysql_tests,
    test_utils::setup_mysql_queue,
    ignore = "requires MySQL: MYSQL_DATABASE_URL",
    [
        fail_job_is_terminal,
        fail_fast,
        continue_on_failure,
        manual,
        dependents_outside_workflows,
    ]
);

#[cfg(feature = "test")]
crate::test_utils::backend_tests!(
    test_queue_tests,
    test_utils::test_queue,
    allow(unused_attributes),
    [
        fail_job_is_terminal,
        fail_fast,
        continue_on_failure,
        manual,
        dependents_outside_workflows,
    ]
);