@@ -7,13 +7,14 @@ import {
7
7
MongoClientOptions ,
8
8
UpdateQuery ,
9
9
ObjectId ,
10
- SortOptionObject
10
+ SortOptionObject ,
11
+ FindOneAndUpdateOption
11
12
} from 'mongodb' ;
12
13
import type { Job } from './Job' ;
13
- import { hasMongoProtocol } from './utils/hasMongoProtocol' ;
14
14
import type { Agenda } from './index' ;
15
- import { IDatabaseOptions , IDbConfig , IMongoOptions } from './types/DbOptions' ;
16
- import { IJobParameters } from './types/JobParameters' ;
15
+ import type { IDatabaseOptions , IDbConfig , IMongoOptions } from './types/DbOptions' ;
16
+ import type { IJobParameters } from './types/JobParameters' ;
17
+ import { hasMongoProtocol } from './utils/hasMongoProtocol' ;
17
18
18
19
const log = debug ( 'agenda:db' ) ;
19
20
@@ -81,7 +82,7 @@ export class JobDbRepository {
81
82
82
83
async lockJob ( job : Job ) : Promise < IJobParameters | undefined > {
83
84
// Query to run against collection to see if we need to lock it
84
- const criteria = {
85
+ const criteria : FilterQuery < Omit < IJobParameters , 'lockedAt' > & { lockedAt ?: Date | null } > = {
85
86
_id : job . attrs . _id ,
86
87
name : job . attrs . name ,
87
88
lockedAt : null ,
@@ -90,11 +91,16 @@ export class JobDbRepository {
90
91
} ;
91
92
92
93
// Update / options for the MongoDB query
93
- const update = { $set : { lockedAt : new Date ( ) } } ;
94
- const options = { returnOriginal : false } ;
94
+ const update : UpdateQuery < IJobParameters > = { $set : { lockedAt : new Date ( ) } } ;
95
+ const options : FindOneAndUpdateOption < IJobParameters > = { returnOriginal : false } ;
95
96
96
97
// Lock the job in MongoDB!
97
- const resp = await this . collection . findOneAndUpdate ( criteria , update , options ) ;
98
+ const resp = await this . collection . findOneAndUpdate (
99
+ criteria as FilterQuery < IJobParameters > ,
100
+ update ,
101
+ options
102
+ ) ;
103
+
98
104
return resp ?. value ;
99
105
}
100
106
@@ -104,11 +110,13 @@ export class JobDbRepository {
104
110
lockDeadline : Date ,
105
111
now : Date = new Date ( )
106
112
) : Promise < IJobParameters | undefined > {
107
- // /**
108
- // * Query used to find job to run
109
- // * @type {{$and: [*]} }
110
- // */
111
- const JOB_PROCESS_WHERE_QUERY = {
113
+ /**
114
+ * Query used to find job to run
115
+ * @type {{$and: [*]} }
116
+ */
117
+ const JOB_PROCESS_WHERE_QUERY : FilterQuery <
118
+ Omit < IJobParameters , 'lockedAt' > & { lockedAt ?: Date | null }
119
+ > = {
112
120
$and : [
113
121
{
114
122
name : jobName ,
@@ -132,13 +140,16 @@ export class JobDbRepository {
132
140
* Query used to set a job as locked
133
141
* @type {{$set: {lockedAt: Date}} }
134
142
*/
135
- const JOB_PROCESS_SET_QUERY = { $set : { lockedAt : now } } ;
143
+ const JOB_PROCESS_SET_QUERY : UpdateQuery < IJobParameters > = { $set : { lockedAt : now } } ;
136
144
137
145
/**
138
146
* Query used to affect what gets returned
139
147
* @type {{returnOriginal: boolean, sort: object} }
140
148
*/
141
- const JOB_RETURN_QUERY = { returnOriginal : false , sort : this . connectOptions . sort } ;
149
+ const JOB_RETURN_QUERY : FindOneAndUpdateOption < IJobParameters > = {
150
+ returnOriginal : false ,
151
+ sort : this . connectOptions . sort
152
+ } ;
142
153
143
154
// Find ONE and ONLY ONE job and set the 'lockedAt' time so that job begins to be processed
144
155
const result = await this . collection . findOneAndUpdate (
@@ -208,7 +219,7 @@ export class JobDbRepository {
208
219
209
220
private processDbResult < DATA = unknown | void > (
210
221
job : Job < DATA > ,
211
- res : IJobParameters < DATA >
222
+ res ? : IJobParameters < DATA >
212
223
) : Job < DATA > {
213
224
log (
214
225
'processDbResult() called with success, checking whether to process job immediately or not'
@@ -243,7 +254,6 @@ export class JobDbRepository {
243
254
244
255
// Grab information needed to save job but that we don't want to persist in MongoDB
245
256
const id = job . attrs . _id ;
246
- // const { unique, uniqueOpts } = job.attrs;
247
257
248
258
// Store job as JSON and remove props we don't want to store from object
249
259
// _id, unique, uniqueOpts
@@ -284,7 +294,7 @@ export class JobDbRepository {
284
294
if ( props . nextRunAt && props . nextRunAt <= now ) {
285
295
log ( 'job has a scheduled nextRunAt time, protecting that field from upsert' ) ;
286
296
protect . nextRunAt = props . nextRunAt ;
287
- delete props . nextRunAt ;
297
+ delete ( props as Partial < IJobParameters > ) . nextRunAt ;
288
298
}
289
299
290
300
// If we have things to protect, set them in MongoDB using $setOnInsert
@@ -309,7 +319,7 @@ export class JobDbRepository {
309
319
update ,
310
320
{
311
321
upsert : true ,
312
- returnOriginal : false // same as new: true -> returns the final document
322
+ returnOriginal : false
313
323
}
314
324
) ;
315
325
log (
@@ -330,8 +340,6 @@ export class JobDbRepository {
330
340
update = { $setOnInsert : props } ;
331
341
}
332
342
333
- // console.log('update', query, update, uniqueOpts);
334
-
335
343
// Use the 'unique' query object to find an existing job or create a new one
336
344
log ( 'calling findOneAndUpdate() with unique object as query: \n%O' , query ) ;
337
345
const result = await this . collection . findOneAndUpdate ( query , update , {
0 commit comments