-
Notifications
You must be signed in to change notification settings - Fork 18
/
AbstractPeriodicJob.ts
255 lines (223 loc) · 7.15 KB
/
AbstractPeriodicJob.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { randomUUID } from 'node:crypto'
import type { ErrorReporter, TransactionObservabilityManager } from '@lokalise/node-core'
import { resolveGlobalErrorLogObject } from '@lokalise/node-core'
import type { FastifyBaseLogger } from 'fastify'
import type Redis from 'ioredis'
import { stdSerializers } from 'pino'
import type { LockOptions } from 'redis-semaphore'
import { Mutex } from 'redis-semaphore'
import type { ToadScheduler } from 'toad-scheduler'
import { AsyncTask, SimpleIntervalJob } from 'toad-scheduler'
import type { CommonDependencies } from '../commonDiConfig.js'
const DEFAULT_LOCK_NAME = 'exclusive'
const DEFAULT_JOB_INTERVAL = 60000
export type BackgroundJobConfiguration = {
/**
* Job unique name
*/
jobId: string
/**
* The interval in milliseconds at which the job should run.
*/
intervalInMs?: number
/**
* Allows to run the job exclusively in a single instance of the application.
* The first consumer that acquires the lock will be the only one to run the job until it stops refreshing the lock.
*/
singleConsumerMode?: {
enabled: boolean
/**
* By default, the lock TTL is 2 * intervalInMs, to prevent the lock from expiring before the next execution.
*/
lockTimeout?: number
/**
* Lock will be reset to this value after success, so that other node could potentially acquire the lock after it expires, but in order to prevent immediate acquire
*/
lockTimeoutAfterSuccess?: number
}
/**
* If true, the job will log when it starts and finishes.
*/
shouldLogExecution?: boolean
}
export type LockConfiguration = {
lockName?: string
identifier?: string
refreshInterval?: number
lockTimeout?: number
acquiredExternally?: true
}
export function createTask(logger: FastifyBaseLogger, job: AbstractPeriodicJob) {
const executorId = randomUUID()
logger.info({
msg: 'Periodic job registered',
jobId: job.options.jobId,
executorId,
})
return new AsyncTask(
job.options.jobId,
() => {
return job.process(executorId)
},
(error) => {
logger.error(
stdSerializers.err({
name: error.name,
message: error.message,
stack: error.stack,
}),
)
},
)
}
export abstract class AbstractPeriodicJob {
public readonly options: Required<BackgroundJobConfiguration>
protected readonly redis: Redis
protected readonly transactionObservabilityManager: TransactionObservabilityManager
protected readonly logger: FastifyBaseLogger
protected readonly errorReporter: ErrorReporter
protected readonly scheduler: ToadScheduler
private singleConsumerLock?: Mutex
protected constructor(
options: BackgroundJobConfiguration,
{
redis,
logger,
transactionObservabilityManager,
errorReporter,
scheduler,
}: CommonDependencies,
) {
this.options = {
intervalInMs: DEFAULT_JOB_INTERVAL,
shouldLogExecution: false,
...options,
singleConsumerMode: {
enabled: true,
lockTimeout: (options.intervalInMs ?? DEFAULT_JOB_INTERVAL) * 2,
...options.singleConsumerMode,
},
}
this.logger = logger
this.redis = redis
this.transactionObservabilityManager = transactionObservabilityManager
this.errorReporter = errorReporter
this.scheduler = scheduler
}
public register() {
const task = createTask(this.logger, this)
this.scheduler.addSimpleIntervalJob(
new SimpleIntervalJob(
{
milliseconds: this.options.intervalInMs,
runImmediately: true,
},
task,
{
id: this.options.jobId,
preventOverrun: true,
},
),
)
}
public async dispose() {
this.scheduler.stop()
await this.singleConsumerLock?.release()
}
protected abstract processInternal(executionUuid: string): Promise<unknown>
public async process(executionUuid: string) {
const logger = this.resolveExecutionLogger(executionUuid)
if (this.options.singleConsumerMode.enabled) {
// acquire or update lock
this.singleConsumerLock = await this.tryAcquireExclusiveLock({
lockTimeout: this.options.singleConsumerMode.lockTimeout,
identifier: executionUuid,
})
if (!this.singleConsumerLock) {
logger.debug(`Periodic job skipped: unable to acquire single consumer lock`)
return
}
}
try {
this.transactionObservabilityManager.start(this.options.jobId, executionUuid)
if (this.options.shouldLogExecution) logger.info(`Periodic job started`)
await this.processInternal(executionUuid)
} catch (err) {
logger.error({
...resolveGlobalErrorLogObject(err, executionUuid),
msg: 'Error during periodic job execution',
})
if (err instanceof Error) {
this.errorReporter.report({
error: err,
context: {
executorId: executionUuid,
},
})
}
} finally {
// stop auto-refreshing the lock to let it expire
if (this.singleConsumerLock) {
await this.updateMutex(
this.singleConsumerLock,
this.options.singleConsumerMode.lockTimeoutAfterSuccess ?? this.options.intervalInMs,
)
this.singleConsumerLock.stopRefresh()
}
if (this.options.shouldLogExecution) logger.info(`Periodic job finished`)
this.transactionObservabilityManager.stop(executionUuid)
}
}
protected getJobMutex(key: string, options: LockOptions) {
return new Mutex(this.redis, this.getJobLockName(key), options)
}
protected async tryAcquireExclusiveLock(lockConfiguration?: LockConfiguration) {
const mutexOptions = {
acquireAttemptsLimit: 1,
refreshInterval: lockConfiguration?.refreshInterval,
acquiredExternally: lockConfiguration?.acquiredExternally,
identifier: lockConfiguration?.identifier,
lockTimeout: lockConfiguration?.lockTimeout ?? this.options.singleConsumerMode.lockTimeout,
}
let lock = this.getJobMutex(lockConfiguration?.lockName ?? DEFAULT_LOCK_NAME, mutexOptions)
let acquired = await lock.tryAcquire()
// If lock has been acquired previously by this instance, try to refresh
if (!acquired && lockConfiguration?.identifier) {
lock = this.getJobMutex(lockConfiguration?.lockName ?? DEFAULT_LOCK_NAME, {
...mutexOptions,
acquiredExternally: true,
})
acquired = await lock.tryAcquire()
}
// If someone else already has this lock, skip
if (!acquired) {
return
}
return lock
}
protected async updateMutex(
mutex: Mutex,
newLockTimeout: number,
key: string = DEFAULT_LOCK_NAME,
) {
const newMutex = new Mutex(this.redis, this.getJobLockName(key), {
acquiredExternally: true,
identifier: mutex.identifier,
lockTimeout: newLockTimeout,
})
const lock = await newMutex.tryAcquire()
if (!lock) {
return
}
return newMutex
}
protected getJobLockName(key: string) {
return `${this.options.jobId}:locks:${key}`
}
protected resolveExecutionLogger(uuid: string): FastifyBaseLogger {
return this.logger.child({
executorId: uuid,
jobId: this.options.jobId,
})
}
}