-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
base_job.ts
145 lines (133 loc) · 4.59 KB
/
base_job.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import app from "@adonisjs/core/services/app"
import { ResqueConfig, ResqueFailure } from "./types.js"
import { Plugin } from "node-resque"
export default class BaseJob {
interval?: string | number
cron?: string
plugins: [typeof Plugin, any][] = []
delayMs: number = 0
runAtMs?: number
/**
* the default JobName is this class name
* it **MUST be a unique name**
*/
jobName?: string
/**
* set a queueName for this job
* default configured in `config/resque.ts`
*/
queueName?: string
args: any[] = []
allArgs: any[][] = []
hasEnqueued: boolean = false
hasEnqueuedAll: boolean = false
app = app
constructor(..._args: any[]) {
}
queue(queueName: string) {
this.queueName = queueName
return this
}
static async enqueueAll<T extends typeof BaseJob>(this: T, args: Parameters<T['prototype']['perform']>[]) {
const job = await app.container.make(this)
return job.enqueueAll(args)
}
async enqueueAll<T extends BaseJob>(this: T, args: Parameters<T['perform']>[]) {
this.allArgs = args
this.hasEnqueuedAll = true
return this.execute()
}
perform(..._args: any[]): any {
}
handleError(error: unknown) {
throw error
}
onFailure(_failure: ResqueFailure): void | Promise<void> {}
private async execute() {
const resqueConfig = app.config.get<ResqueConfig>('resque')
const jobName = this.jobName ?? this.constructor.name
const queueName = this.queueName ?? resqueConfig.queueNameForJobs
const queue = await app.container.make('queue')
let logger = await app.container.make('logger')
if (resqueConfig.logger) {
logger.use(resqueConfig.logger)
}
if (this.hasEnqueued) {
const getTips = () => {
if (!resqueConfig.verbose) {
return undefined
}
const tips = `enqueued to queue ${queueName}, job ${jobName}`
if (this.delayMs) {
return `${tips}, delay ${this.delayMs}ms`
} else if (this.runAtMs) {
return `${tips}, run at ${this.runAtMs}`
} else {
return tips
}
}
const tips = getTips()
if (tips) {
logger.info(tips)
}
if (this.delayMs) {
return queue.enqueueIn(this.delayMs, queueName, jobName, this.args)
} else if (this.runAtMs) {
return queue.enqueueAt(this.runAtMs, queueName, jobName, this.args)
} else {
return queue.enqueue(queueName, jobName, this.args)
}
} else if (this.hasEnqueuedAll) {
return Promise.all(this.allArgs.map(arg => queue.enqueue(queueName, jobName, arg)))
} else {
return false
}
}
private push<T extends BaseJob>({ args, delayMs, runAtMs }: {
args: Parameters<T['perform']>;
delayMs?: number
runAtMs?: number
}) {
this.args = args
this.hasEnqueued = true
this.delayMs = delayMs ? delayMs : 0
this.runAtMs = runAtMs
return this.execute()
}
static async enqueue<T extends typeof BaseJob>(this: T, ...args: Parameters<InstanceType<T>['perform']>) {
const job = await app.container.make(this)
return job.enqueue(...args)
}
async enqueue<T extends BaseJob>(this: T, ...args: Parameters<T['perform']>) {
return this.push({
args
})
}
/**
*
* @param this
* @param delayMs In ms, the number of ms to delay before this job is able to start being worked on
* @param args
* @returns
*/
static async enqueueIn<T extends typeof BaseJob>(this: T, delayMs: number, ...args: Parameters<InstanceType<T>['perform']>) {
const job = await app.container.make(this)
return job.enqueueIn(delayMs, ...args)
}
async enqueueIn<T extends BaseJob>(this: T, delayMs: number, ...args: Parameters<T['perform']>) {
return this.push({
args,
delayMs,
})
}
static async enqueueAt<T extends typeof BaseJob>(this: T, runAtMs: number, ...args: Parameters<InstanceType<T>['perform']>) {
const job = await app.container.make(this)
return job.enqueueAt(runAtMs, ...args)
}
async enqueueAt<T extends BaseJob>(this: T, runAtMs: number, ...args: Parameters<T['perform']>) {
return this.push({
args,
runAtMs,
})
}
}