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