1
1
use std:: { borrow:: Cow , time:: Duration } ;
2
2
3
3
use ahash:: { HashMap , HashSet } ;
4
+ use serde:: { Deserialize , Serialize } ;
4
5
use time:: OffsetDateTime ;
5
6
use tracing:: { instrument, Span } ;
6
7
use uuid:: Uuid ;
@@ -18,8 +19,13 @@ use crate::{
18
19
} ;
19
20
20
21
/// A job to be submitted to the queue.
21
- #[ derive( Debug , Clone ) ]
22
+ /// Jobs are uniquely identified by their `id`, so adding a job with the same ID twice will fail.
23
+ /// If you want to clone the same Job object and submit it multiple times, use [JobBuilder::clone_as_new]
24
+ /// to generate a new ID with each clone.
25
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
22
26
pub struct Job {
27
+ /// A unique identifier for the job.
28
+ pub id : Uuid ,
23
29
/// The name of the job, which matches the name used in the [JobRunner](crate::JobRunner) for the job.
24
30
pub job_type : Cow < ' static , str > ,
25
31
/// Jobs with higher `priority` will be executed first.
@@ -51,10 +57,17 @@ impl Job {
51
57
pub fn builder ( job_type : impl Into < Cow < ' static , str > > ) -> JobBuilder {
52
58
JobBuilder :: new ( job_type)
53
59
}
60
+
61
+ /// Clone the [Job] with a new `id`.
62
+ pub fn clone_as_new ( & self ) -> Self {
63
+ let mut job = self . clone ( ) ;
64
+ job. id = Uuid :: now_v7 ( ) ;
65
+ job
66
+ }
54
67
}
55
68
56
69
/// `Retries` controls the exponential backoff behavior when retrying failed jobs.
57
- #[ derive( Debug , Clone ) ]
70
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
58
71
pub struct Retries {
59
72
/// How many times to retry a job before it is considered to have failed permanently.
60
73
pub max_retries : u32 ,
@@ -83,6 +96,7 @@ impl Default for Retries {
83
96
impl Default for Job {
84
97
fn default ( ) -> Self {
85
98
Self {
99
+ id : Uuid :: now_v7 ( ) ,
86
100
job_type : Default :: default ( ) ,
87
101
priority : 0 ,
88
102
weight : 1 ,
@@ -204,7 +218,7 @@ impl JobBuilder {
204
218
/// Specified fields of a job to be updated, using the [Queue::update_job] method.
205
219
/// All of these fields except the job ID are optional, so the update can set
206
220
/// only the desired fields.
207
- #[ derive( Debug , Clone ) ]
221
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
208
222
pub struct JobUpdate {
209
223
/// The ID of the job to update
210
224
pub id : Uuid ,
@@ -474,7 +488,7 @@ mod tests {
474
488
use std:: { sync:: Arc , time:: Duration } ;
475
489
476
490
use temp_dir:: TempDir ;
477
- use ulid :: Ulid ;
491
+ use uuid :: Uuid ;
478
492
479
493
use crate :: {
480
494
test_util:: {
@@ -675,7 +689,7 @@ mod tests {
675
689
let result = test
676
690
. queue
677
691
. update_job (
678
- JobUpdate :: builder ( Ulid :: new ( ) . into ( ) )
692
+ JobUpdate :: builder ( Uuid :: now_v7 ( ) . into ( ) )
679
693
. run_at ( test. time . now ( ) )
680
694
. build ( ) ,
681
695
)
@@ -777,7 +791,7 @@ mod tests {
777
791
#[ tokio:: test]
778
792
async fn cancel_nonexistent_job ( ) {
779
793
let test = TestEnvironment :: new ( ) . await ;
780
- let result = test. queue . cancel_job ( Ulid :: new ( ) . into ( ) ) . await ;
794
+ let result = test. queue . cancel_job ( Uuid :: now_v7 ( ) . into ( ) ) . await ;
781
795
782
796
assert ! ( matches!( result, Err ( Error :: NotFound ) ) ) ;
783
797
}
0 commit comments