10
10
*
11
11
**/
12
12
13
- import { Collection , Db , Filter , FindOneAndUpdateOptions , ObjectId , Sort , UpdateFilter , WithId } from 'mongodb' ;
13
+ import { Collection , CreateIndexesOptions , Db , Filter , FindOneAndUpdateOptions , ObjectId , Sort , UpdateFilter , WithId } from 'mongodb' ;
14
14
15
15
function now ( ) : string {
16
16
return ( new Date ( ) ) . toISOString ( ) ;
@@ -25,6 +25,7 @@ export type QueueOptions = {
25
25
delay ?: number ;
26
26
deadQueue ?: MongoDBQueue ;
27
27
maxRetries ?: number ;
28
+ expireAfterSeconds ?: number ;
28
29
} ;
29
30
30
31
export type AddOptions = {
@@ -64,6 +65,7 @@ export class MongoDBQueue<T = any> {
64
65
private readonly delay : number ;
65
66
private readonly maxRetries : number ;
66
67
private readonly deadQueue : MongoDBQueue ;
68
+ private readonly expireAfterSeconds : number ;
67
69
68
70
public constructor ( db : Db , name : string , opts : QueueOptions = { } ) {
69
71
if ( ! db ) {
@@ -76,6 +78,7 @@ export class MongoDBQueue<T = any> {
76
78
this . col = db . collection ( name ) ;
77
79
this . visibility = opts . visibility || 30 ;
78
80
this . delay = opts . delay || 0 ;
81
+ this . expireAfterSeconds = opts . expireAfterSeconds ;
79
82
80
83
if ( opts . deadQueue ) {
81
84
this . deadQueue = opts . deadQueue ;
@@ -84,10 +87,15 @@ export class MongoDBQueue<T = any> {
84
87
}
85
88
86
89
public async createIndexes ( ) : Promise < void > {
90
+ const deletedOptions : CreateIndexesOptions = { sparse : true } ;
91
+ if ( typeof this . expireAfterSeconds === 'number' ) {
92
+ deletedOptions . expireAfterSeconds = this . expireAfterSeconds ;
93
+ }
94
+
87
95
await Promise . all ( [
88
96
this . col . createIndex ( { visible : 1 } , { sparse : true } ) ,
89
97
this . col . createIndex ( { ack : 1 } , { unique : true , sparse : true } ) ,
90
- this . col . createIndex ( { deleted : 1 } , { sparse : true } ) ,
98
+ this . col . createIndex ( { deleted : 1 } , deletedOptions ) ,
91
99
92
100
// Index for efficient counts on in-flight
93
101
this . col . createIndex ( { visible : 1 , ack : 1 } , {
0 commit comments