@@ -2,7 +2,7 @@ import assert from "node:assert";
2
2
import { and , asc , count , eq , gt , lt , or } from "drizzle-orm" ;
3
3
4
4
import { buildDBClient } from "./db" ;
5
- import { SqliteQueueOptions } from "./options" ;
5
+ import { EnqueueOptions , SqliteQueueOptions } from "./options" ;
6
6
import { Job , tasksTable } from "./schema" ;
7
7
8
8
// generate random id
@@ -29,19 +29,27 @@ export class SqliteQueue<T> {
29
29
return this . queueName ;
30
30
}
31
31
32
- async enqueue ( payload : T ) : Promise < Job > {
33
- const job = await this . db
32
+ /**
33
+ * Enqueue a job into the queue.
34
+ * If a job with the same idempotency key is already enqueued, it will be ignored and undefined will be returned.
35
+ */
36
+ async enqueue ( payload : T , options ?: EnqueueOptions ) : Promise < Job | undefined > {
37
+ const opts = options ?? { } ;
38
+ const numRetries = opts . numRetries ?? this . options . defaultJobArgs . numRetries ;
39
+ const [ job ] = await this . db
34
40
. insert ( tasksTable )
35
41
. values ( {
36
42
queue : this . queueName ,
37
43
payload : JSON . stringify ( payload ) ,
38
- numRunsLeft : this . options . defaultJobArgs . numRetries + 1 ,
39
- maxNumRuns : this . options . defaultJobArgs . numRetries + 1 ,
44
+ numRunsLeft : numRetries + 1 ,
45
+ maxNumRuns : numRetries + 1 ,
40
46
allocationId : generateAllocationId ( ) ,
47
+ idempotencyKey : opts . idempotencyKey ,
41
48
} )
49
+ . onConflictDoNothing ( { target : [ tasksTable . queue , tasksTable . idempotencyKey ] } )
42
50
. returning ( ) ;
43
51
44
- return job [ 0 ] ;
52
+ return job ;
45
53
}
46
54
47
55
async stats ( ) {
0 commit comments