1
1
/* eslint-disable @typescript-eslint/no-misused-promises */
2
+ import type { Processor } from "bullmq" ;
2
3
import { Queue , Worker } from "bullmq" ;
3
4
import type { Redis } from "ioredis" ;
4
5
5
6
import { createModuleLogger } from "@blobscan/logger" ;
6
7
import type { Logger } from "@blobscan/logger" ;
7
8
8
- import { ErrorException } from "./errors" ;
9
- import { createRedis } from "./redis" ;
9
+ import { ErrorException } from ".. /errors" ;
10
+ import { createRedis } from ".. /redis" ;
10
11
11
12
export interface CommonCronJobConfig {
12
13
redisUriOrConnection : Redis | string ;
@@ -15,11 +16,12 @@ export interface CommonCronJobConfig {
15
16
16
17
export interface BaseCronJobConfig extends CommonCronJobConfig {
17
18
name : string ;
18
- jobFn : ( ) => Promise < void > ;
19
+ processor : Processor ;
20
+ jobData ?: Record < string , unknown > ;
19
21
}
20
22
21
23
export class CronJobError extends ErrorException {
22
- constructor ( cronJobName : string , message : string , cause : unknown ) {
24
+ constructor ( cronJobName : string , message : string , cause ? : unknown ) {
23
25
super ( `Cron job "${ cronJobName } " failed: ${ message } ` , cause ) ;
24
26
}
25
27
}
@@ -28,22 +30,25 @@ export class BaseCronJob {
28
30
name : string ;
29
31
cronPattern : string ;
30
32
31
- protected jobFn : ( ) => Promise < void > ;
32
33
protected logger : Logger ;
33
34
34
35
protected connection : Redis ;
35
- protected worker : Worker | undefined ;
36
- protected queue : Queue | undefined ;
36
+ protected worker ?: Worker ;
37
+ protected queue ?: Queue ;
38
+
39
+ protected jobData ?: Record < string , unknown > ;
37
40
38
41
constructor ( {
39
42
name,
40
43
cronPattern,
41
44
redisUriOrConnection,
42
- jobFn,
45
+ processor : processorFile ,
46
+ jobData,
43
47
} : BaseCronJobConfig ) {
44
48
this . name = `${ name } -cron-job` ;
45
49
this . cronPattern = cronPattern ;
46
50
this . logger = createModuleLogger ( this . name ) ;
51
+ this . jobData = jobData ;
47
52
48
53
let connection : Redis ;
49
54
@@ -57,7 +62,7 @@ export class BaseCronJob {
57
62
connection,
58
63
} ) ;
59
64
60
- this . worker = new Worker ( this . queue . name , jobFn , {
65
+ this . worker = new Worker ( this . queue . name , processorFile , {
61
66
connection,
62
67
} ) ;
63
68
@@ -70,13 +75,12 @@ export class BaseCronJob {
70
75
} ) ;
71
76
72
77
this . connection = connection ;
73
- this . jobFn = jobFn ;
74
78
}
75
79
76
80
async start ( ) {
77
81
try {
78
82
const jobName = `${ this . name } -job` ;
79
- const repeatableJob = await this . queue ?. add ( jobName , null , {
83
+ const repeatableJob = await this . queue ?. add ( jobName , this . jobData , {
80
84
repeat : {
81
85
pattern : this . cronPattern ,
82
86
} ,
0 commit comments