@open-mercato/scheduler 0.6.7-develop.6834.1.e76f4b8cbc → 0.6.7-develop.6851.1.c1680b00d8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/modules/scheduler/api/jobs/[id]/executions/route.js +4 -2
- package/dist/modules/scheduler/api/jobs/[id]/executions/route.js.map +2 -2
- package/dist/modules/scheduler/api/queue-jobs/[jobId]/route.js +4 -2
- package/dist/modules/scheduler/api/queue-jobs/[jobId]/route.js.map +2 -2
- package/dist/modules/scheduler/i18n/ko.json +157 -0
- package/dist/modules/scheduler/services/bullmqSchedulerService.js +90 -41
- package/dist/modules/scheduler/services/bullmqSchedulerService.js.map +2 -2
- package/package.json +6 -6
- package/src/modules/scheduler/api/jobs/[id]/executions/route.ts +4 -2
- package/src/modules/scheduler/api/queue-jobs/[jobId]/route.ts +4 -2
- package/src/modules/scheduler/i18n/ko.json +157 -0
- package/src/modules/scheduler/services/__tests__/bullmqSchedulerService.test.ts +224 -163
- package/src/modules/scheduler/services/bullmqSchedulerService.ts +133 -47
|
@@ -22,12 +22,22 @@ const loggerError = mockedLogger.error as jest.Mock
|
|
|
22
22
|
|
|
23
23
|
// Mock BullMQ module
|
|
24
24
|
const mockQueue = {
|
|
25
|
+
upsertJobScheduler: jest.fn(),
|
|
26
|
+
getJobSchedulers: jest.fn(),
|
|
27
|
+
removeJobScheduler: jest.fn(),
|
|
28
|
+
close: jest.fn(),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const mockLegacyQueue = {
|
|
25
32
|
add: jest.fn(),
|
|
26
33
|
getRepeatableJobs: jest.fn(),
|
|
27
34
|
removeRepeatableByKey: jest.fn(),
|
|
35
|
+
close: jest.fn(),
|
|
28
36
|
}
|
|
29
37
|
|
|
30
|
-
|
|
38
|
+
let useLegacyQueue = false
|
|
39
|
+
|
|
40
|
+
const mockQueueConstructor = jest.fn(() => useLegacyQueue ? mockLegacyQueue : mockQueue)
|
|
31
41
|
|
|
32
42
|
jest.mock('bullmq', () => ({
|
|
33
43
|
Queue: mockQueueConstructor,
|
|
@@ -35,6 +45,7 @@ jest.mock('bullmq', () => ({
|
|
|
35
45
|
|
|
36
46
|
jest.mock('@open-mercato/shared/lib/redis/connection', () => ({
|
|
37
47
|
getRedisUrlOrThrow: jest.fn(() => 'redis://localhost:6379'),
|
|
48
|
+
parseRedisUrl: jest.requireActual('@open-mercato/shared/lib/redis/connection').parseRedisUrl,
|
|
38
49
|
}))
|
|
39
50
|
|
|
40
51
|
|
|
@@ -45,9 +56,15 @@ describe('BullMQSchedulerService', () => {
|
|
|
45
56
|
|
|
46
57
|
beforeEach(() => {
|
|
47
58
|
jest.clearAllMocks()
|
|
48
|
-
|
|
49
|
-
mockQueue.
|
|
50
|
-
mockQueue.
|
|
59
|
+
useLegacyQueue = false
|
|
60
|
+
mockQueue.upsertJobScheduler.mockResolvedValue({})
|
|
61
|
+
mockQueue.getJobSchedulers.mockResolvedValue([])
|
|
62
|
+
mockQueue.removeJobScheduler.mockResolvedValue(true)
|
|
63
|
+
mockQueue.close.mockResolvedValue(undefined)
|
|
64
|
+
mockLegacyQueue.add.mockResolvedValue({})
|
|
65
|
+
mockLegacyQueue.getRepeatableJobs.mockResolvedValue([])
|
|
66
|
+
mockLegacyQueue.removeRepeatableByKey.mockResolvedValue(true)
|
|
67
|
+
mockLegacyQueue.close.mockResolvedValue(undefined)
|
|
51
68
|
|
|
52
69
|
// Create mock forked EM
|
|
53
70
|
mockForkedEm = {
|
|
@@ -62,7 +79,50 @@ describe('BullMQSchedulerService', () => {
|
|
|
62
79
|
service = new BullMQSchedulerService(() => mockEm)
|
|
63
80
|
})
|
|
64
81
|
|
|
82
|
+
it('constructs BullMQ with parsed ioredis connection fields', async () => {
|
|
83
|
+
const schedule = {
|
|
84
|
+
id: 'test-connection',
|
|
85
|
+
name: 'Test Schedule',
|
|
86
|
+
isEnabled: true,
|
|
87
|
+
scheduleType: 'cron',
|
|
88
|
+
scheduleValue: '0 0 * * *',
|
|
89
|
+
timezone: 'UTC',
|
|
90
|
+
scopeType: 'system',
|
|
91
|
+
} as ScheduledJob
|
|
92
|
+
|
|
93
|
+
await service.register(schedule)
|
|
94
|
+
|
|
95
|
+
expect(mockQueueConstructor).toHaveBeenCalledWith('scheduler-execution', {
|
|
96
|
+
connection: expect.objectContaining({ host: 'localhost', port: 6379 }),
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
65
100
|
describe('register', () => {
|
|
101
|
+
it('falls back to repeatable jobs for BullMQ 5 releases without Job Schedulers', async () => {
|
|
102
|
+
useLegacyQueue = true
|
|
103
|
+
mockLegacyQueue.getRepeatableJobs.mockResolvedValue([
|
|
104
|
+
{ id: 'schedule-test-legacy', name: 'schedule-test-legacy', key: 'old-key' },
|
|
105
|
+
])
|
|
106
|
+
const schedule = {
|
|
107
|
+
id: 'test-legacy',
|
|
108
|
+
name: 'Legacy Schedule',
|
|
109
|
+
isEnabled: true,
|
|
110
|
+
scheduleType: 'cron',
|
|
111
|
+
scheduleValue: '0 0 * * *',
|
|
112
|
+
timezone: 'UTC',
|
|
113
|
+
scopeType: 'system',
|
|
114
|
+
} as ScheduledJob
|
|
115
|
+
|
|
116
|
+
await service.register(schedule)
|
|
117
|
+
|
|
118
|
+
expect(mockLegacyQueue.removeRepeatableByKey).toHaveBeenCalledWith('old-key')
|
|
119
|
+
expect(mockLegacyQueue.add).toHaveBeenCalledWith(
|
|
120
|
+
'schedule-test-legacy',
|
|
121
|
+
expect.objectContaining({ id: 'schedule-test-legacy' }),
|
|
122
|
+
expect.objectContaining({ repeat: { pattern: '0 0 * * *', tz: 'UTC' } }),
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
|
|
66
126
|
it('should skip disabled schedules', async () => {
|
|
67
127
|
const schedule = {
|
|
68
128
|
id: 'test-1',
|
|
@@ -76,7 +136,7 @@ describe('BullMQSchedulerService', () => {
|
|
|
76
136
|
'Skipping disabled schedule',
|
|
77
137
|
{ scheduleId: 'test-1' }
|
|
78
138
|
)
|
|
79
|
-
expect(mockQueue.
|
|
139
|
+
expect(mockQueue.upsertJobScheduler).not.toHaveBeenCalled()
|
|
80
140
|
})
|
|
81
141
|
|
|
82
142
|
it('should register cron schedule with BullMQ', async () => {
|
|
@@ -92,23 +152,22 @@ describe('BullMQSchedulerService', () => {
|
|
|
92
152
|
organizationId: null,
|
|
93
153
|
} as ScheduledJob
|
|
94
154
|
|
|
95
|
-
mockQueue.add.mockResolvedValue({})
|
|
96
|
-
|
|
97
155
|
await service.register(schedule)
|
|
98
156
|
|
|
99
|
-
expect(mockQueue.
|
|
157
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
100
158
|
'schedule-test-1',
|
|
101
159
|
expect.objectContaining({
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
scheduleId: 'test-1',
|
|
105
|
-
scopeType: 'system',
|
|
106
|
-
}),
|
|
160
|
+
pattern: '0 0 * * *',
|
|
161
|
+
tz: 'UTC',
|
|
107
162
|
}),
|
|
108
163
|
expect.objectContaining({
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
164
|
+
name: 'schedule-test-1',
|
|
165
|
+
data: expect.objectContaining({
|
|
166
|
+
id: 'schedule-test-1',
|
|
167
|
+
payload: expect.objectContaining({
|
|
168
|
+
scheduleId: 'test-1',
|
|
169
|
+
scopeType: 'system',
|
|
170
|
+
}),
|
|
112
171
|
}),
|
|
113
172
|
})
|
|
114
173
|
)
|
|
@@ -127,23 +186,21 @@ describe('BullMQSchedulerService', () => {
|
|
|
127
186
|
organizationId: null,
|
|
128
187
|
} as ScheduledJob
|
|
129
188
|
|
|
130
|
-
mockQueue.add.mockResolvedValue({})
|
|
131
|
-
|
|
132
189
|
await service.register(schedule)
|
|
133
190
|
|
|
134
|
-
expect(mockQueue.
|
|
191
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
135
192
|
'schedule-test-2',
|
|
136
193
|
expect.objectContaining({
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
tenantId: 'tenant-1',
|
|
140
|
-
scopeType: 'tenant',
|
|
141
|
-
}),
|
|
194
|
+
every: 15 * 60 * 1000,
|
|
195
|
+
tz: 'UTC',
|
|
142
196
|
}),
|
|
143
197
|
expect.objectContaining({
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
198
|
+
data: expect.objectContaining({
|
|
199
|
+
payload: expect.objectContaining({
|
|
200
|
+
scheduleId: 'test-2',
|
|
201
|
+
tenantId: 'tenant-1',
|
|
202
|
+
scopeType: 'tenant',
|
|
203
|
+
}),
|
|
147
204
|
}),
|
|
148
205
|
})
|
|
149
206
|
)
|
|
@@ -162,25 +219,25 @@ describe('BullMQSchedulerService', () => {
|
|
|
162
219
|
organizationId: 'org-1',
|
|
163
220
|
} as ScheduledJob
|
|
164
221
|
|
|
165
|
-
mockQueue.add.mockResolvedValue({})
|
|
166
|
-
|
|
167
222
|
await service.register(schedule)
|
|
168
223
|
|
|
169
|
-
expect(mockQueue.
|
|
224
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
170
225
|
expect.any(String),
|
|
226
|
+
expect.any(Object),
|
|
171
227
|
expect.objectContaining({
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
228
|
+
data: expect.objectContaining({
|
|
229
|
+
payload: expect.objectContaining({
|
|
230
|
+
scheduleId: 'test-3',
|
|
231
|
+
tenantId: 'tenant-1',
|
|
232
|
+
organizationId: 'org-1',
|
|
233
|
+
scopeType: 'organization',
|
|
234
|
+
}),
|
|
177
235
|
}),
|
|
178
|
-
})
|
|
179
|
-
expect.any(Object)
|
|
236
|
+
})
|
|
180
237
|
)
|
|
181
238
|
})
|
|
182
239
|
|
|
183
|
-
it('should
|
|
240
|
+
it('should upsert an updated schedule by stable scheduler id', async () => {
|
|
184
241
|
const schedule = {
|
|
185
242
|
id: 'test-1',
|
|
186
243
|
name: 'Test',
|
|
@@ -191,31 +248,39 @@ describe('BullMQSchedulerService', () => {
|
|
|
191
248
|
scopeType: 'system',
|
|
192
249
|
} as ScheduledJob
|
|
193
250
|
|
|
194
|
-
mockQueue.getRepeatableJobs.mockResolvedValue([
|
|
195
|
-
{ id: 'schedule-test-1', name: 'schedule-test-1', key: 'old-cron-key' },
|
|
196
|
-
{ id: 'schedule-test-1', name: 'schedule-test-1', key: 'older-cron-key' },
|
|
197
|
-
{ id: 'schedule-other', name: 'schedule-other', key: 'other-key' },
|
|
198
|
-
])
|
|
199
|
-
mockQueue.removeRepeatableByKey.mockResolvedValue(true)
|
|
200
|
-
mockQueue.add.mockResolvedValue({})
|
|
201
|
-
|
|
202
251
|
await service.register(schedule)
|
|
203
252
|
|
|
204
|
-
expect(mockQueue.
|
|
205
|
-
expect(mockQueue.removeRepeatableByKey).toHaveBeenNthCalledWith(1, 'old-cron-key')
|
|
206
|
-
expect(mockQueue.removeRepeatableByKey).toHaveBeenNthCalledWith(2, 'older-cron-key')
|
|
207
|
-
expect(mockQueue.add).toHaveBeenCalledWith(
|
|
253
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
208
254
|
'schedule-test-1',
|
|
209
|
-
expect.any(Object),
|
|
210
255
|
expect.objectContaining({
|
|
211
|
-
|
|
212
|
-
pattern: '*/15 * * * *',
|
|
213
|
-
}),
|
|
256
|
+
pattern: '*/15 * * * *',
|
|
214
257
|
}),
|
|
258
|
+
expect.any(Object),
|
|
215
259
|
)
|
|
216
|
-
|
|
217
|
-
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
it('migrates BullMQ 5 repeatable metadata to the stable scheduler id', async () => {
|
|
263
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
264
|
+
{ key: 'legacy-repeat-hash', name: 'schedule-test-1' },
|
|
265
|
+
])
|
|
266
|
+
const schedule = {
|
|
267
|
+
id: 'test-1',
|
|
268
|
+
name: 'Test',
|
|
269
|
+
isEnabled: true,
|
|
270
|
+
scheduleType: 'cron',
|
|
271
|
+
scheduleValue: '0 0 * * *',
|
|
272
|
+
timezone: 'UTC',
|
|
273
|
+
scopeType: 'system',
|
|
274
|
+
} as ScheduledJob
|
|
275
|
+
|
|
276
|
+
await service.register(schedule)
|
|
277
|
+
|
|
278
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
279
|
+
'schedule-test-1',
|
|
280
|
+
expect.any(Object),
|
|
281
|
+
expect.any(Object),
|
|
218
282
|
)
|
|
283
|
+
expect(mockQueue.removeJobScheduler).toHaveBeenCalledWith('legacy-repeat-hash')
|
|
219
284
|
})
|
|
220
285
|
|
|
221
286
|
it('should update nextRunAt when skipNextRunUpdate is false', async () => {
|
|
@@ -230,8 +295,6 @@ describe('BullMQSchedulerService', () => {
|
|
|
230
295
|
nextRunAt: new Date('2020-01-01'),
|
|
231
296
|
} as ScheduledJob
|
|
232
297
|
|
|
233
|
-
mockQueue.add.mockResolvedValue({})
|
|
234
|
-
|
|
235
298
|
await service.register(schedule, { skipNextRunUpdate: false })
|
|
236
299
|
|
|
237
300
|
// nextRunAt should be updated
|
|
@@ -251,8 +314,6 @@ describe('BullMQSchedulerService', () => {
|
|
|
251
314
|
nextRunAt: originalDate,
|
|
252
315
|
} as ScheduledJob
|
|
253
316
|
|
|
254
|
-
mockQueue.add.mockResolvedValue({})
|
|
255
|
-
|
|
256
317
|
await service.register(schedule, { skipNextRunUpdate: true })
|
|
257
318
|
|
|
258
319
|
// nextRunAt should not be updated
|
|
@@ -271,7 +332,7 @@ describe('BullMQSchedulerService', () => {
|
|
|
271
332
|
} as ScheduledJob
|
|
272
333
|
|
|
273
334
|
const error = new Error('BullMQ connection failed')
|
|
274
|
-
mockQueue.
|
|
335
|
+
mockQueue.upsertJobScheduler.mockRejectedValue(error)
|
|
275
336
|
|
|
276
337
|
await expect(service.register(schedule)).rejects.toThrow('BullMQ connection failed')
|
|
277
338
|
|
|
@@ -311,68 +372,73 @@ describe('BullMQSchedulerService', () => {
|
|
|
311
372
|
})
|
|
312
373
|
|
|
313
374
|
describe('unregister', () => {
|
|
314
|
-
it('
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
{ id: 'schedule-test-
|
|
375
|
+
it('removes legacy BullMQ 5 repeatable jobs when Job Schedulers are unavailable', async () => {
|
|
376
|
+
useLegacyQueue = true
|
|
377
|
+
mockLegacyQueue.getRepeatableJobs.mockResolvedValue([
|
|
378
|
+
{ id: 'schedule-test-legacy', name: 'schedule-test-legacy', key: 'legacy-key' },
|
|
379
|
+
])
|
|
380
|
+
|
|
381
|
+
await service.unregister('test-legacy')
|
|
382
|
+
|
|
383
|
+
expect(mockLegacyQueue.removeRepeatableByKey).toHaveBeenCalledWith('legacy-key')
|
|
384
|
+
expect(loggerDebug).toHaveBeenCalledWith('Unregistered schedule', {
|
|
385
|
+
scheduleId: 'test-legacy',
|
|
386
|
+
})
|
|
387
|
+
})
|
|
388
|
+
|
|
389
|
+
it('should remove a job scheduler by stable id', async () => {
|
|
390
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
391
|
+
{ id: 'schedule-test-1', name: 'schedule-test-1', key: 'schedule-test-1' },
|
|
392
|
+
{ id: 'schedule-test-2', name: 'schedule-test-2', key: 'schedule-test-2' },
|
|
318
393
|
])
|
|
319
|
-
mockQueue.removeRepeatableByKey.mockResolvedValue(true)
|
|
320
394
|
|
|
321
395
|
await service.unregister('test-1')
|
|
322
396
|
|
|
323
|
-
expect(mockQueue.
|
|
324
|
-
expect(mockQueue.
|
|
397
|
+
expect(mockQueue.getJobSchedulers).toHaveBeenCalled()
|
|
398
|
+
expect(mockQueue.removeJobScheduler).toHaveBeenCalledWith('schedule-test-1')
|
|
325
399
|
expect(loggerDebug).toHaveBeenCalledWith(
|
|
326
400
|
'Unregistered schedule',
|
|
327
401
|
{ scheduleId: 'test-1' }
|
|
328
402
|
)
|
|
329
403
|
})
|
|
330
404
|
|
|
331
|
-
it('should
|
|
332
|
-
mockQueue.
|
|
333
|
-
{ id: 'schedule-
|
|
334
|
-
{ id: 'schedule-test-1', name: 'schedule-test-1', key: 'key-2' },
|
|
335
|
-
{ name: 'schedule-test-1', key: 'key-3' },
|
|
336
|
-
{ id: 'schedule-other', name: 'schedule-other', key: 'other-key' },
|
|
405
|
+
it('should handle schedule not found', async () => {
|
|
406
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
407
|
+
{ id: 'schedule-other', name: 'schedule-other', key: 'schedule-other' },
|
|
337
408
|
])
|
|
338
|
-
mockQueue.removeRepeatableByKey.mockResolvedValue(true)
|
|
339
409
|
|
|
340
410
|
await service.unregister('test-1')
|
|
341
411
|
|
|
342
|
-
expect(mockQueue.
|
|
343
|
-
expect(
|
|
344
|
-
|
|
345
|
-
|
|
412
|
+
expect(mockQueue.removeJobScheduler).not.toHaveBeenCalled()
|
|
413
|
+
expect(loggerDebug).toHaveBeenCalledWith(
|
|
414
|
+
'No job scheduler found for schedule',
|
|
415
|
+
{ scheduleId: 'test-1' }
|
|
416
|
+
)
|
|
346
417
|
})
|
|
347
418
|
|
|
348
|
-
it('should
|
|
349
|
-
mockQueue.
|
|
350
|
-
{
|
|
419
|
+
it('should match by scheduler key if id is not present', async () => {
|
|
420
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
421
|
+
{ name: 'test', key: 'schedule-test-1' },
|
|
351
422
|
])
|
|
352
423
|
|
|
353
424
|
await service.unregister('test-1')
|
|
354
425
|
|
|
355
|
-
expect(mockQueue.
|
|
356
|
-
expect(loggerDebug).toHaveBeenCalledWith(
|
|
357
|
-
'No repeatable job found for schedule',
|
|
358
|
-
{ scheduleId: 'test-1' }
|
|
359
|
-
)
|
|
426
|
+
expect(mockQueue.removeJobScheduler).toHaveBeenCalledWith('schedule-test-1')
|
|
360
427
|
})
|
|
361
428
|
|
|
362
|
-
it('
|
|
363
|
-
mockQueue.
|
|
364
|
-
{ name: 'schedule-test-1', key: '
|
|
429
|
+
it('removes BullMQ 5 repeatable metadata by its generated key', async () => {
|
|
430
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
431
|
+
{ name: 'schedule-test-1', key: 'legacy-repeat-hash' },
|
|
365
432
|
])
|
|
366
|
-
mockQueue.removeRepeatableByKey.mockResolvedValue(true)
|
|
367
433
|
|
|
368
434
|
await service.unregister('test-1')
|
|
369
435
|
|
|
370
|
-
expect(mockQueue.
|
|
436
|
+
expect(mockQueue.removeJobScheduler).toHaveBeenCalledWith('legacy-repeat-hash')
|
|
371
437
|
})
|
|
372
438
|
|
|
373
439
|
it('should throw on BullMQ error', async () => {
|
|
374
440
|
const error = new Error('BullMQ error')
|
|
375
|
-
mockQueue.
|
|
441
|
+
mockQueue.getJobSchedulers.mockRejectedValue(error)
|
|
376
442
|
|
|
377
443
|
await expect(service.unregister('test-1')).rejects.toThrow('BullMQ error')
|
|
378
444
|
|
|
@@ -391,10 +457,9 @@ describe('BullMQSchedulerService', () => {
|
|
|
391
457
|
] as ScheduledJob[]
|
|
392
458
|
|
|
393
459
|
mockForkedEm.find.mockResolvedValue(dbSchedules)
|
|
394
|
-
mockQueue.
|
|
395
|
-
{ id: 'schedule-schedule-1', name: 'schedule-schedule-1', key: '
|
|
460
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
461
|
+
{ id: 'schedule-schedule-1', name: 'schedule-schedule-1', key: 'schedule-schedule-1' },
|
|
396
462
|
])
|
|
397
|
-
mockQueue.add.mockResolvedValue({})
|
|
398
463
|
|
|
399
464
|
await service.syncAll()
|
|
400
465
|
|
|
@@ -402,7 +467,7 @@ describe('BullMQSchedulerService', () => {
|
|
|
402
467
|
isEnabled: true,
|
|
403
468
|
deletedAt: null,
|
|
404
469
|
}, { limit: 500, offset: 0 })
|
|
405
|
-
expect(mockQueue.
|
|
470
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
406
471
|
'schedule-schedule-2',
|
|
407
472
|
expect.any(Object),
|
|
408
473
|
expect.any(Object)
|
|
@@ -420,32 +485,28 @@ describe('BullMQSchedulerService', () => {
|
|
|
420
485
|
] as ScheduledJob[]
|
|
421
486
|
|
|
422
487
|
mockForkedEm.find.mockResolvedValue(dbSchedules)
|
|
423
|
-
mockQueue.
|
|
488
|
+
mockQueue.getJobSchedulers.mockResolvedValue([])
|
|
424
489
|
|
|
425
490
|
await service.syncAll()
|
|
426
491
|
|
|
427
|
-
expect(mockQueue.
|
|
428
|
-
expect(mockQueue.
|
|
492
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledTimes(2)
|
|
493
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenNthCalledWith(
|
|
429
494
|
1,
|
|
430
495
|
'schedule-legacy',
|
|
431
|
-
expect.any(Object),
|
|
432
496
|
expect.objectContaining({
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
tz: 'UTC',
|
|
436
|
-
},
|
|
497
|
+
every: 60 * 1000,
|
|
498
|
+
tz: 'UTC',
|
|
437
499
|
}),
|
|
500
|
+
expect.any(Object),
|
|
438
501
|
)
|
|
439
|
-
expect(mockQueue.
|
|
502
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenNthCalledWith(
|
|
440
503
|
2,
|
|
441
504
|
'schedule-current',
|
|
442
|
-
expect.any(Object),
|
|
443
505
|
expect.objectContaining({
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
tz: 'UTC',
|
|
447
|
-
},
|
|
506
|
+
pattern: '0 0 * * *',
|
|
507
|
+
tz: 'UTC',
|
|
448
508
|
}),
|
|
509
|
+
expect.any(Object),
|
|
449
510
|
)
|
|
450
511
|
})
|
|
451
512
|
|
|
@@ -455,53 +516,65 @@ describe('BullMQSchedulerService', () => {
|
|
|
455
516
|
] as ScheduledJob[]
|
|
456
517
|
|
|
457
518
|
mockForkedEm.find.mockResolvedValue(dbSchedules)
|
|
458
|
-
mockQueue.
|
|
459
|
-
{ id: 'schedule-schedule-1', name: 'schedule-schedule-1', key: '
|
|
460
|
-
{ id: 'schedule-schedule-2', name: 'schedule-schedule-2', key: '
|
|
519
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
520
|
+
{ id: 'schedule-schedule-1', name: 'schedule-schedule-1', key: 'schedule-schedule-1' },
|
|
521
|
+
{ id: 'schedule-schedule-2', name: 'schedule-schedule-2', key: 'schedule-schedule-2' },
|
|
461
522
|
])
|
|
462
|
-
mockQueue.removeRepeatableByKey.mockResolvedValue(true)
|
|
463
523
|
|
|
464
524
|
await service.syncAll()
|
|
465
525
|
|
|
466
|
-
expect(mockQueue.
|
|
526
|
+
expect(mockQueue.removeJobScheduler).toHaveBeenCalledWith('schedule-schedule-2')
|
|
467
527
|
expect(loggerInfo).toHaveBeenCalledWith(
|
|
468
528
|
'Removing orphaned schedule',
|
|
469
529
|
{ scheduleId: 'schedule-2' }
|
|
470
530
|
)
|
|
471
531
|
})
|
|
472
532
|
|
|
473
|
-
it('should
|
|
533
|
+
it('should leave existing job schedulers unchanged', async () => {
|
|
534
|
+
const dbSchedules = [
|
|
535
|
+
{ id: 'schedule-1', name: 'Schedule 1', isEnabled: true, scheduleType: 'cron', scheduleValue: '0 0 * * *', timezone: 'UTC', scopeType: 'system' },
|
|
536
|
+
] as ScheduledJob[]
|
|
537
|
+
|
|
538
|
+
mockForkedEm.find.mockResolvedValue(dbSchedules)
|
|
539
|
+
mockQueue.getJobSchedulers.mockResolvedValue([
|
|
540
|
+
{ id: 'schedule-schedule-1', name: 'schedule-schedule-1', key: 'schedule-schedule-1' },
|
|
541
|
+
])
|
|
542
|
+
|
|
543
|
+
await service.syncAll()
|
|
544
|
+
|
|
545
|
+
expect(mockQueue.upsertJobScheduler).not.toHaveBeenCalled()
|
|
546
|
+
expect(mockQueue.removeJobScheduler).not.toHaveBeenCalled()
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
it('migrates a BullMQ 5 repeatable definition during sync', async () => {
|
|
474
550
|
const dbSchedules = [
|
|
475
551
|
{ id: 'schedule-1', name: 'Schedule 1', isEnabled: true, scheduleType: 'cron', scheduleValue: '0 0 * * *', timezone: 'UTC', scopeType: 'system' },
|
|
476
552
|
] as ScheduledJob[]
|
|
477
553
|
|
|
478
554
|
mockForkedEm.find.mockResolvedValue(dbSchedules)
|
|
479
|
-
mockQueue.
|
|
480
|
-
{
|
|
481
|
-
|
|
555
|
+
mockQueue.getJobSchedulers.mockResolvedValueOnce([
|
|
556
|
+
{ name: 'schedule-schedule-1', key: 'legacy-repeat-hash' },
|
|
557
|
+
]).mockResolvedValueOnce([
|
|
558
|
+
{ name: 'schedule-schedule-1', key: 'legacy-repeat-hash' },
|
|
482
559
|
])
|
|
483
|
-
mockQueue.removeRepeatableByKey.mockResolvedValue(true)
|
|
484
|
-
mockQueue.add.mockResolvedValue({})
|
|
485
560
|
|
|
486
561
|
await service.syncAll()
|
|
487
562
|
|
|
488
563
|
expect(loggerInfo).toHaveBeenCalledWith(
|
|
489
|
-
'
|
|
490
|
-
{ scheduleId: 'schedule-1' }
|
|
564
|
+
'Migrating legacy or duplicate job scheduler',
|
|
565
|
+
{ scheduleId: 'schedule-1' },
|
|
491
566
|
)
|
|
492
|
-
expect(mockQueue.
|
|
493
|
-
expect(mockQueue.removeRepeatableByKey).toHaveBeenNthCalledWith(1, 'old-key-1')
|
|
494
|
-
expect(mockQueue.removeRepeatableByKey).toHaveBeenNthCalledWith(2, 'old-key-2')
|
|
495
|
-
expect(mockQueue.add).toHaveBeenCalledWith(
|
|
567
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
496
568
|
'schedule-schedule-1',
|
|
497
569
|
expect.any(Object),
|
|
498
570
|
expect.any(Object),
|
|
499
571
|
)
|
|
572
|
+
expect(mockQueue.removeJobScheduler).toHaveBeenCalledWith('legacy-repeat-hash')
|
|
500
573
|
})
|
|
501
574
|
|
|
502
575
|
it('should log sync completion', async () => {
|
|
503
576
|
mockForkedEm.find.mockResolvedValue([])
|
|
504
|
-
mockQueue.
|
|
577
|
+
mockQueue.getJobSchedulers.mockResolvedValue([])
|
|
505
578
|
|
|
506
579
|
await service.syncAll()
|
|
507
580
|
|
|
@@ -514,28 +587,28 @@ describe('BullMQSchedulerService', () => {
|
|
|
514
587
|
})
|
|
515
588
|
|
|
516
589
|
describe('getRepeatableJobs', () => {
|
|
517
|
-
it('should return
|
|
590
|
+
it('should return job schedulers', async () => {
|
|
518
591
|
const jobs = [
|
|
519
|
-
{ id: 'schedule-1', key: '
|
|
520
|
-
{ id: 'schedule-2', key: '
|
|
592
|
+
{ id: 'schedule-1', name: 'schedule-1', key: 'schedule-1' },
|
|
593
|
+
{ id: 'schedule-2', name: 'schedule-2', key: 'schedule-2' },
|
|
521
594
|
]
|
|
522
595
|
|
|
523
|
-
mockQueue.
|
|
596
|
+
mockQueue.getJobSchedulers.mockResolvedValue(jobs)
|
|
524
597
|
|
|
525
598
|
const result = await service.getRepeatableJobs()
|
|
526
599
|
|
|
527
600
|
expect(result).toEqual(jobs)
|
|
528
|
-
expect(mockQueue.
|
|
601
|
+
expect(mockQueue.getJobSchedulers).toHaveBeenCalled()
|
|
529
602
|
})
|
|
530
603
|
|
|
531
604
|
it('should return empty array on error', async () => {
|
|
532
|
-
mockQueue.
|
|
605
|
+
mockQueue.getJobSchedulers.mockRejectedValue(new Error('BullMQ error'))
|
|
533
606
|
|
|
534
607
|
const result = await service.getRepeatableJobs()
|
|
535
608
|
|
|
536
609
|
expect(result).toEqual([])
|
|
537
610
|
expect(loggerError).toHaveBeenCalledWith(
|
|
538
|
-
'Failed to get
|
|
611
|
+
'Failed to get job schedulers',
|
|
539
612
|
{ err: expect.any(Error) }
|
|
540
613
|
)
|
|
541
614
|
})
|
|
@@ -553,19 +626,15 @@ describe('BullMQSchedulerService', () => {
|
|
|
553
626
|
scopeType: 'system',
|
|
554
627
|
} as ScheduledJob
|
|
555
628
|
|
|
556
|
-
mockQueue.add.mockResolvedValue({})
|
|
557
|
-
|
|
558
629
|
await service.register(schedule)
|
|
559
630
|
|
|
560
|
-
expect(mockQueue.
|
|
631
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
561
632
|
expect.any(String),
|
|
562
|
-
expect.any(Object),
|
|
563
633
|
expect.objectContaining({
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
})
|
|
634
|
+
pattern: '0 0 * * *',
|
|
635
|
+
tz: 'America/New_York',
|
|
636
|
+
}),
|
|
637
|
+
expect.any(Object),
|
|
569
638
|
)
|
|
570
639
|
})
|
|
571
640
|
|
|
@@ -580,19 +649,15 @@ describe('BullMQSchedulerService', () => {
|
|
|
580
649
|
scopeType: 'system',
|
|
581
650
|
} as ScheduledJob
|
|
582
651
|
|
|
583
|
-
mockQueue.add.mockResolvedValue({})
|
|
584
|
-
|
|
585
652
|
await service.register(schedule)
|
|
586
653
|
|
|
587
|
-
expect(mockQueue.
|
|
654
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
588
655
|
expect.any(String),
|
|
589
|
-
expect.any(Object),
|
|
590
656
|
expect.objectContaining({
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
})
|
|
657
|
+
every: 2 * 60 * 60 * 1000,
|
|
658
|
+
tz: 'UTC',
|
|
659
|
+
}),
|
|
660
|
+
expect.any(Object),
|
|
596
661
|
)
|
|
597
662
|
})
|
|
598
663
|
|
|
@@ -609,19 +674,15 @@ describe('BullMQSchedulerService', () => {
|
|
|
609
674
|
targetQueue: 'test',
|
|
610
675
|
} as ScheduledJob
|
|
611
676
|
|
|
612
|
-
mockQueue.add.mockResolvedValue({})
|
|
613
|
-
|
|
614
677
|
await service.register(schedule)
|
|
615
678
|
|
|
616
|
-
expect(mockQueue.
|
|
679
|
+
expect(mockQueue.upsertJobScheduler).toHaveBeenCalledWith(
|
|
617
680
|
expect.any(String),
|
|
618
|
-
expect.any(Object),
|
|
619
681
|
expect.objectContaining({
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
})
|
|
682
|
+
pattern: '0 0 * * *',
|
|
683
|
+
tz: 'UTC',
|
|
684
|
+
}),
|
|
685
|
+
expect.any(Object),
|
|
625
686
|
)
|
|
626
687
|
})
|
|
627
688
|
|