@@ -4,6 +4,7 @@ import { parsePriority } from './utils/priority';
4
4
import type { Agenda } from './index' ;
5
5
import { computeFromInterval , computeFromRepeatAt } from './utils/nextRunAt' ;
6
6
import { IJobParameters } from './types/JobParameters' ;
7
+ import type { DefinitionProcessor } from './types/JobDefinition' ;
7
8
8
9
const log = debug ( 'agenda:job' ) ;
9
10
@@ -13,12 +14,31 @@ const log = debug('agenda:job');
13
14
* @property {Object } agenda - The Agenda instance
14
15
* @property {Object } attrs
15
16
*/
16
- export class Job {
17
- readonly attrs : IJobParameters ;
17
+ export class Job < DATA = any | void > {
18
+ readonly attrs : IJobParameters < DATA > ;
18
19
20
+ constructor (
21
+ agenda : Agenda ,
22
+ args : Partial < IJobParameters < void > > & {
23
+ name : string ;
24
+ type : 'normal' | 'single' ;
25
+ }
26
+ ) ;
27
+ constructor (
28
+ agenda : Agenda ,
29
+ args : Partial < IJobParameters < DATA > > & {
30
+ name : string ;
31
+ type : 'normal' | 'single' ;
32
+ data : DATA ;
33
+ }
34
+ ) ;
19
35
constructor (
20
36
readonly agenda : Agenda ,
21
- args : Partial < IJobParameters > & { name : string ; type : 'normal' | 'single' }
37
+ args : Partial < IJobParameters < DATA > > & {
38
+ name : string ;
39
+ type : 'normal' | 'single' ;
40
+ data : any ;
41
+ }
22
42
) {
23
43
// Remove special args
24
44
@@ -35,10 +55,11 @@ export class Job {
35
55
} ;
36
56
}
37
57
38
- toJson ( ) {
58
+ toJson ( ) : Partial < IJobParameters > {
39
59
const attrs = this . attrs || { } ;
40
60
const result = { } ;
41
61
62
+ // eslint-disable-next-line no-restricted-syntax
42
63
for ( const prop in attrs ) {
43
64
if ( { } . hasOwnProperty . call ( attrs , prop ) ) {
44
65
result [ prop ] = attrs [ prop ] ;
@@ -155,18 +176,17 @@ export class Job {
155
176
return this . agenda . db . saveJob ( this ) ;
156
177
}
157
178
158
- remove ( ) {
179
+ remove ( ) : Promise < number > {
159
180
return this . agenda . cancel ( { _id : this . attrs . _id } ) ;
160
181
}
161
182
162
- async touch ( progress ?: number ) {
163
- // eslint-disable-next-line prefer-rest-params
183
+ async touch ( progress ?: number ) : Promise < void > {
164
184
this . attrs . lockedAt = new Date ( ) ;
165
185
this . attrs . progress = progress ;
166
- return this . save ( ) ;
186
+ await this . save ( ) ;
167
187
}
168
188
169
- computeNextRunAt ( ) {
189
+ private computeNextRunAt ( ) {
170
190
try {
171
191
if ( this . attrs . repeatInterval ) {
172
192
this . attrs . nextRunAt = computeFromInterval ( this . attrs ) ;
@@ -222,23 +242,26 @@ export class Job {
222
242
log ( '[%s:%s] process function being called' , this . attrs . name , this . attrs . _id ) ;
223
243
await new Promise ( ( resolve , reject ) => {
224
244
try {
225
- const result = definition . fn ( this , err => {
226
- if ( err ) {
227
- reject ( err ) ;
228
- return ;
245
+ const result = ( definition . fn as DefinitionProcessor < DATA , ( err ?) => void > ) (
246
+ this ,
247
+ err => {
248
+ if ( err ) {
249
+ reject ( err ) ;
250
+ return ;
251
+ }
252
+ resolve ( ) ;
229
253
}
230
- resolve ( ) ;
231
- } ) ;
254
+ ) ;
232
255
if ( this . isPromise ( result ) ) {
233
- result . catch ( err => reject ( err ) ) ;
256
+ ( result as any ) . catch ( err => reject ( err ) ) ;
234
257
}
235
258
} catch ( err ) {
236
259
reject ( err ) ;
237
260
}
238
261
} ) ;
239
262
} else {
240
263
log ( '[%s:%s] process function being called' , this . attrs . name , this . attrs . _id ) ;
241
- await definition . fn ( this ) ;
264
+ await ( definition . fn as DefinitionProcessor < DATA , void > ) ( this ) ;
242
265
}
243
266
244
267
this . attrs . lastFinishedAt = new Date ( ) ;
@@ -271,6 +294,6 @@ export class Job {
271
294
}
272
295
273
296
private isPromise ( value ) : value is Promise < void > {
274
- return Boolean ( value && typeof value . then === 'function' ) ;
297
+ return ! ! ( value && typeof value . then === 'function' ) ;
275
298
}
276
299
}
0 commit comments