@open-mercato/scheduler 0.9.0 → 0.9.1-develop.7459.1.99e3accd30
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/api/jobs/[id]/executions/route.js +1 -1
- package/dist/modules/scheduler/api/jobs/[id]/executions/route.js.map +2 -2
- package/dist/modules/scheduler/api/jobs/route.js +1 -1
- package/dist/modules/scheduler/api/jobs/route.js.map +2 -2
- package/dist/modules/scheduler/cli.js +28 -6
- package/dist/modules/scheduler/cli.js.map +2 -2
- package/dist/modules/scheduler/data/validators.js +5 -5
- package/dist/modules/scheduler/data/validators.js.map +2 -2
- package/dist/modules/scheduler/lib/bullmqNextRun.js +61 -0
- package/dist/modules/scheduler/lib/bullmqNextRun.js.map +7 -0
- package/dist/modules/scheduler/lib/nextRunSync.js +80 -0
- package/dist/modules/scheduler/lib/nextRunSync.js.map +7 -0
- package/dist/modules/scheduler/workers/execute-schedule.worker.js +20 -0
- package/dist/modules/scheduler/workers/execute-schedule.worker.js.map +2 -2
- package/package.json +7 -6
- package/src/modules/scheduler/__tests__/cli.run.test.ts +135 -0
- package/src/modules/scheduler/api/jobs/[id]/executions/route.ts +1 -1
- package/src/modules/scheduler/api/jobs/route.ts +1 -1
- package/src/modules/scheduler/api/trigger/__tests__/route.test.ts +15 -0
- package/src/modules/scheduler/cli.ts +33 -7
- package/src/modules/scheduler/data/__tests__/validators.test.ts +29 -0
- package/src/modules/scheduler/data/validators.ts +5 -5
- package/src/modules/scheduler/lib/__tests__/bullmqNextRun.test.ts +200 -0
- package/src/modules/scheduler/lib/__tests__/nextRunSync.test.ts +222 -0
- package/src/modules/scheduler/lib/bullmqNextRun.ts +112 -0
- package/src/modules/scheduler/lib/nextRunSync.ts +131 -0
- package/src/modules/scheduler/workers/__tests__/execute-schedule.next-run.test.ts +468 -0
- package/src/modules/scheduler/workers/__tests__/execute-schedule.worker.test.ts +4 -0
- package/src/modules/scheduler/workers/execute-schedule.worker.ts +35 -0
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import { createQueue } from '@open-mercato/queue'
|
|
3
|
+
import { registerModules } from '@open-mercato/shared/lib/modules/registry'
|
|
4
|
+
import { ScheduledJob } from '../../data/entities'
|
|
5
|
+
import { registerSchedulerSafeCommands } from '../../lib/scheduler-safe-commands'
|
|
6
|
+
import { registerSchedulerQueuePayloadSchema } from '../../lib/safeQueueTargets'
|
|
7
|
+
import executeScheduleWorker from '../execute-schedule.worker'
|
|
8
|
+
|
|
9
|
+
registerModules([
|
|
10
|
+
{
|
|
11
|
+
id: 'next_run_test_module',
|
|
12
|
+
workers: [
|
|
13
|
+
{
|
|
14
|
+
id: 'next_run_test_module:workers:example',
|
|
15
|
+
queue: 'next-run-example',
|
|
16
|
+
concurrency: 1,
|
|
17
|
+
handler: async () => {},
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: 'next_run_test_module:workers:strict',
|
|
21
|
+
queue: 'next-run-strict',
|
|
22
|
+
concurrency: 1,
|
|
23
|
+
handler: async () => {},
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: 'next_run_test_module:workers:gated',
|
|
27
|
+
queue: 'next-run-gated',
|
|
28
|
+
concurrency: 1,
|
|
29
|
+
schedulerSafe: true,
|
|
30
|
+
schedulerRequiredFeatures: ['next_run.gated'],
|
|
31
|
+
handler: async () => {},
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
] as never)
|
|
36
|
+
|
|
37
|
+
registerSchedulerQueuePayloadSchema('next-run-strict', z.object({}).strict())
|
|
38
|
+
|
|
39
|
+
const mockCommandExecute = jest.fn()
|
|
40
|
+
|
|
41
|
+
jest.mock('@open-mercato/shared/lib/commands', () => ({
|
|
42
|
+
CommandBus: jest.fn().mockImplementation(() => ({
|
|
43
|
+
execute: mockCommandExecute,
|
|
44
|
+
})),
|
|
45
|
+
}))
|
|
46
|
+
|
|
47
|
+
jest.mock('@open-mercato/queue', () => ({
|
|
48
|
+
createQueue: jest.fn(),
|
|
49
|
+
}), { virtual: true })
|
|
50
|
+
|
|
51
|
+
jest.mock('@open-mercato/shared/lib/redis/connection', () => ({
|
|
52
|
+
getRedisUrlOrThrow: jest.fn(() => 'redis://localhost:6379'),
|
|
53
|
+
parseRedisUrl: jest.fn(() => ({ host: 'localhost', port: 6379 })),
|
|
54
|
+
}))
|
|
55
|
+
|
|
56
|
+
const emitSchedulerEvent = jest.fn(async () => undefined)
|
|
57
|
+
|
|
58
|
+
jest.mock('../../events', () => ({
|
|
59
|
+
emitSchedulerEvent: (...args: unknown[]) => emitSchedulerEvent(...(args as [])),
|
|
60
|
+
}))
|
|
61
|
+
|
|
62
|
+
const mockGetJobScheduler = jest.fn()
|
|
63
|
+
const mockBullQueueClose = jest.fn(async () => undefined)
|
|
64
|
+
const mockBullQueueDisconnect = jest.fn(async () => undefined)
|
|
65
|
+
const mockBullQueueConstructor = jest.fn(() => ({
|
|
66
|
+
getJobScheduler: mockGetJobScheduler,
|
|
67
|
+
on: jest.fn(),
|
|
68
|
+
close: mockBullQueueClose,
|
|
69
|
+
disconnect: mockBullQueueDisconnect,
|
|
70
|
+
}))
|
|
71
|
+
|
|
72
|
+
jest.mock('bullmq', () => ({
|
|
73
|
+
Queue: mockBullQueueConstructor,
|
|
74
|
+
}))
|
|
75
|
+
|
|
76
|
+
const scheduleId = '22222222-2222-4222-8222-222222222222'
|
|
77
|
+
const staleNextRunAt = new Date('2026-01-01T00:00:00.000Z')
|
|
78
|
+
const bullmqNextMillis = Date.UTC(2030, 0, 1, 6, 0, 0)
|
|
79
|
+
const bullmqNextRunAt = new Date(bullmqNextMillis)
|
|
80
|
+
|
|
81
|
+
function buildCommandSchedule(overrides: Partial<ScheduledJob> = {}): ScheduledJob {
|
|
82
|
+
const schedule = new ScheduledJob()
|
|
83
|
+
schedule.id = scheduleId
|
|
84
|
+
schedule.name = 'Next run schedule'
|
|
85
|
+
schedule.scopeType = 'organization'
|
|
86
|
+
schedule.tenantId = 'tenant-a'
|
|
87
|
+
schedule.organizationId = 'org-a'
|
|
88
|
+
schedule.isEnabled = true
|
|
89
|
+
schedule.targetType = 'command'
|
|
90
|
+
schedule.targetCommand = 'scheduler.test.next-run'
|
|
91
|
+
schedule.targetPayload = {}
|
|
92
|
+
schedule.scheduleType = 'cron'
|
|
93
|
+
schedule.scheduleValue = '0 6 * * *'
|
|
94
|
+
schedule.timezone = 'Europe/Warsaw'
|
|
95
|
+
schedule.sourceType = 'user'
|
|
96
|
+
schedule.createdByUserId = 'user-a'
|
|
97
|
+
schedule.nextRunAt = staleNextRunAt
|
|
98
|
+
Object.assign(schedule, overrides)
|
|
99
|
+
return schedule
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function buildQueueSchedule(overrides: Partial<ScheduledJob> = {}): ScheduledJob {
|
|
103
|
+
return buildCommandSchedule({
|
|
104
|
+
targetType: 'queue',
|
|
105
|
+
targetQueue: 'next-run-example',
|
|
106
|
+
targetCommand: null,
|
|
107
|
+
sourceType: 'module',
|
|
108
|
+
createdByUserId: null,
|
|
109
|
+
sourceModule: 'next_run_test_module',
|
|
110
|
+
targetPayload: { connectionId: 'connection-id' },
|
|
111
|
+
scheduleType: 'interval',
|
|
112
|
+
scheduleValue: '15m',
|
|
113
|
+
timezone: 'UTC',
|
|
114
|
+
...overrides,
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function buildWorkerContext(schedule: ScheduledJob | null) {
|
|
119
|
+
const execute = jest.fn(async () => ({ affectedRows: 1 }))
|
|
120
|
+
const em = {
|
|
121
|
+
findOne: jest.fn(async () => schedule),
|
|
122
|
+
flush: jest.fn(async () => undefined),
|
|
123
|
+
getConnection: jest.fn(() => ({ execute })),
|
|
124
|
+
}
|
|
125
|
+
const rbacService = {
|
|
126
|
+
tenantHasFeature: jest.fn(async () => true),
|
|
127
|
+
userHasAllFeatures: jest.fn(async () => true),
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
context: {
|
|
131
|
+
jobId: `repeat:schedule-${scheduleId}:1893477600000`,
|
|
132
|
+
attemptNumber: 1,
|
|
133
|
+
queueName: 'scheduler-execution',
|
|
134
|
+
resolve: jest.fn((name: string) => {
|
|
135
|
+
if (name === 'em') return em
|
|
136
|
+
if (name === 'rbacService') return rbacService
|
|
137
|
+
throw new Error(`Unexpected dependency: ${name}`)
|
|
138
|
+
}),
|
|
139
|
+
},
|
|
140
|
+
em,
|
|
141
|
+
execute,
|
|
142
|
+
rbacService,
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function runWorker(
|
|
147
|
+
schedule: ScheduledJob,
|
|
148
|
+
harness: ReturnType<typeof buildWorkerContext>,
|
|
149
|
+
payloadOverrides: Record<string, unknown> = {},
|
|
150
|
+
) {
|
|
151
|
+
return executeScheduleWorker(
|
|
152
|
+
{
|
|
153
|
+
id: 'queued-job-1',
|
|
154
|
+
payload: {
|
|
155
|
+
scheduleId,
|
|
156
|
+
tenantId: schedule.tenantId,
|
|
157
|
+
organizationId: schedule.organizationId,
|
|
158
|
+
scopeType: schedule.scopeType,
|
|
159
|
+
...payloadOverrides,
|
|
160
|
+
},
|
|
161
|
+
createdAt: new Date().toISOString(),
|
|
162
|
+
},
|
|
163
|
+
harness.context as never,
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function expectNextRunWrite(
|
|
168
|
+
execute: jest.Mock,
|
|
169
|
+
timing: { scheduleType: string; scheduleValue: string; timezone: string },
|
|
170
|
+
) {
|
|
171
|
+
expect(execute).toHaveBeenCalledTimes(1)
|
|
172
|
+
const [sql, params, method] = execute.mock.calls[0] as [string, unknown[], string]
|
|
173
|
+
expect(sql).toMatch(/^update scheduled_jobs\s+set next_run_at = \?/)
|
|
174
|
+
expect(params).toEqual([
|
|
175
|
+
bullmqNextRunAt,
|
|
176
|
+
scheduleId,
|
|
177
|
+
'tenant-a',
|
|
178
|
+
'org-a',
|
|
179
|
+
timing.scheduleType,
|
|
180
|
+
timing.scheduleValue,
|
|
181
|
+
timing.timezone,
|
|
182
|
+
bullmqNextRunAt,
|
|
183
|
+
bullmqNextRunAt,
|
|
184
|
+
])
|
|
185
|
+
expect(method).toBe('run')
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const cronTiming = { scheduleType: 'cron', scheduleValue: '0 6 * * *', timezone: 'Europe/Warsaw' }
|
|
189
|
+
const intervalTiming = { scheduleType: 'interval', scheduleValue: '15m', timezone: 'UTC' }
|
|
190
|
+
|
|
191
|
+
describe('executeScheduleWorker nextRunAt sync (async strategy)', () => {
|
|
192
|
+
const originalStrategy = process.env.QUEUE_STRATEGY
|
|
193
|
+
const enqueue = jest.fn(async () => 'target-job-1')
|
|
194
|
+
const close = jest.fn(async () => undefined)
|
|
195
|
+
|
|
196
|
+
beforeAll(() => {
|
|
197
|
+
registerSchedulerSafeCommands([
|
|
198
|
+
{ commandId: 'scheduler.test.next-run', requiredFeatures: ['scheduler.jobs.manage'] },
|
|
199
|
+
])
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
beforeEach(() => {
|
|
203
|
+
jest.clearAllMocks()
|
|
204
|
+
process.env.QUEUE_STRATEGY = 'async'
|
|
205
|
+
mockCommandExecute.mockResolvedValue({ result: { ok: true }, logEntry: null })
|
|
206
|
+
mockGetJobScheduler.mockResolvedValue({ next: bullmqNextMillis, pattern: '0 6 * * *', tz: 'Europe/Warsaw' })
|
|
207
|
+
;(createQueue as jest.Mock).mockReturnValue({ enqueue, close })
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
afterAll(() => {
|
|
211
|
+
if (originalStrategy === undefined) delete process.env.QUEUE_STRATEGY
|
|
212
|
+
else process.env.QUEUE_STRATEGY = originalStrategy
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('writes the BullMQ next slot after a completed command run', async () => {
|
|
216
|
+
const schedule = buildCommandSchedule()
|
|
217
|
+
const harness = buildWorkerContext(schedule)
|
|
218
|
+
|
|
219
|
+
await runWorker(schedule, harness)
|
|
220
|
+
|
|
221
|
+
expect(mockCommandExecute).toHaveBeenCalledTimes(1)
|
|
222
|
+
expect(mockBullQueueConstructor).toHaveBeenCalledWith('scheduler-execution', expect.anything())
|
|
223
|
+
expect(mockGetJobScheduler).toHaveBeenCalledWith(`schedule-${scheduleId}`)
|
|
224
|
+
expect(mockBullQueueClose).toHaveBeenCalledTimes(1)
|
|
225
|
+
expectNextRunWrite(harness.execute, cronTiming)
|
|
226
|
+
expect(harness.em.flush).toHaveBeenCalledTimes(1)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it('reads BullMQ only after the target has run', async () => {
|
|
230
|
+
const order: string[] = []
|
|
231
|
+
mockCommandExecute.mockImplementation(async () => {
|
|
232
|
+
order.push('command')
|
|
233
|
+
return { result: { ok: true }, logEntry: null }
|
|
234
|
+
})
|
|
235
|
+
mockGetJobScheduler.mockImplementation(async () => {
|
|
236
|
+
order.push('read')
|
|
237
|
+
return { next: bullmqNextMillis, pattern: '0 6 * * *', tz: 'Europe/Warsaw' }
|
|
238
|
+
})
|
|
239
|
+
const schedule = buildCommandSchedule()
|
|
240
|
+
|
|
241
|
+
await runWorker(schedule, buildWorkerContext(schedule))
|
|
242
|
+
|
|
243
|
+
expect(order).toEqual(['command', 'read'])
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
it('writes the BullMQ next slot after a completed queue-target run', async () => {
|
|
247
|
+
mockGetJobScheduler.mockResolvedValue({ next: bullmqNextMillis, every: 900000, tz: 'UTC' })
|
|
248
|
+
const schedule = buildQueueSchedule()
|
|
249
|
+
const harness = buildWorkerContext(schedule)
|
|
250
|
+
|
|
251
|
+
await runWorker(schedule, harness)
|
|
252
|
+
|
|
253
|
+
expect(enqueue).toHaveBeenCalledTimes(1)
|
|
254
|
+
expectNextRunWrite(harness.execute, intervalTiming)
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
it('writes the BullMQ next slot for a manual run too', async () => {
|
|
258
|
+
const schedule = buildCommandSchedule()
|
|
259
|
+
const harness = buildWorkerContext(schedule)
|
|
260
|
+
|
|
261
|
+
await runWorker(schedule, harness, { triggerType: 'manual', triggeredByUserId: 'user-b' })
|
|
262
|
+
|
|
263
|
+
expect(mockCommandExecute).toHaveBeenCalledTimes(1)
|
|
264
|
+
expectNextRunWrite(harness.execute, cronTiming)
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
it('writes the BullMQ next slot when the run is skipped for a missing required feature', async () => {
|
|
268
|
+
const schedule = buildCommandSchedule({ requireFeature: 'some.feature' })
|
|
269
|
+
const harness = buildWorkerContext(schedule)
|
|
270
|
+
harness.rbacService.tenantHasFeature.mockResolvedValue(false)
|
|
271
|
+
|
|
272
|
+
await runWorker(schedule, harness)
|
|
273
|
+
|
|
274
|
+
expect(mockCommandExecute).not.toHaveBeenCalled()
|
|
275
|
+
expect(emitSchedulerEvent).toHaveBeenCalledWith('scheduler.job.skipped', expect.objectContaining({ id: scheduleId }))
|
|
276
|
+
expectNextRunWrite(harness.execute, cronTiming)
|
|
277
|
+
expect(harness.em.flush).not.toHaveBeenCalled()
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
it('writes the BullMQ next slot when the queue target is refused as unapproved', async () => {
|
|
281
|
+
mockGetJobScheduler.mockResolvedValue({ next: bullmqNextMillis, every: 900000, tz: 'UTC' })
|
|
282
|
+
const schedule = buildQueueSchedule({ targetQueue: 'not-a-registered-queue' })
|
|
283
|
+
const harness = buildWorkerContext(schedule)
|
|
284
|
+
|
|
285
|
+
await runWorker(schedule, harness)
|
|
286
|
+
|
|
287
|
+
expect(enqueue).not.toHaveBeenCalled()
|
|
288
|
+
expectNextRunWrite(harness.execute, intervalTiming)
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
it('writes the BullMQ next slot when the stored payload is rejected by the queue target schema', async () => {
|
|
292
|
+
mockGetJobScheduler.mockResolvedValue({ next: bullmqNextMillis, every: 900000, tz: 'UTC' })
|
|
293
|
+
const schedule = buildQueueSchedule({ targetQueue: 'next-run-strict', targetPayload: { unexpected: true } })
|
|
294
|
+
const harness = buildWorkerContext(schedule)
|
|
295
|
+
|
|
296
|
+
await runWorker(schedule, harness)
|
|
297
|
+
|
|
298
|
+
expect(enqueue).not.toHaveBeenCalled()
|
|
299
|
+
expect(emitSchedulerEvent).toHaveBeenCalledWith('scheduler.job.skipped', expect.objectContaining({
|
|
300
|
+
reason: 'Invalid payload for scheduler queue next-run-strict',
|
|
301
|
+
}))
|
|
302
|
+
expectNextRunWrite(harness.execute, intervalTiming)
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
it('writes the BullMQ next slot when the tenant lacks a feature the queue target requires', async () => {
|
|
306
|
+
mockGetJobScheduler.mockResolvedValue({ next: bullmqNextMillis, every: 900000, tz: 'UTC' })
|
|
307
|
+
const schedule = buildQueueSchedule({ targetQueue: 'next-run-gated' })
|
|
308
|
+
const harness = buildWorkerContext(schedule)
|
|
309
|
+
harness.rbacService.tenantHasFeature.mockResolvedValue(false)
|
|
310
|
+
|
|
311
|
+
await runWorker(schedule, harness)
|
|
312
|
+
|
|
313
|
+
expect(enqueue).not.toHaveBeenCalled()
|
|
314
|
+
expect(emitSchedulerEvent).toHaveBeenCalledWith('scheduler.job.skipped', expect.objectContaining({
|
|
315
|
+
reason: 'Tenant lacks feature required by scheduler queue next-run-gated: next_run.gated',
|
|
316
|
+
}))
|
|
317
|
+
expectNextRunWrite(harness.execute, intervalTiming)
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
it('writes the BullMQ next slot when the scheduled command is refused', async () => {
|
|
321
|
+
const schedule = buildCommandSchedule()
|
|
322
|
+
const harness = buildWorkerContext(schedule)
|
|
323
|
+
harness.rbacService.userHasAllFeatures.mockResolvedValue(false)
|
|
324
|
+
|
|
325
|
+
await expect(runWorker(schedule, harness)).resolves.toBeUndefined()
|
|
326
|
+
|
|
327
|
+
expect(mockCommandExecute).not.toHaveBeenCalled()
|
|
328
|
+
expect(emitSchedulerEvent).toHaveBeenCalledWith('scheduler.job.failed', expect.objectContaining({ id: scheduleId }))
|
|
329
|
+
expectNextRunWrite(harness.execute, cronTiming)
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
it('writes the BullMQ next slot when the target throws, and rethrows the target error unchanged', async () => {
|
|
333
|
+
const failure = new Error('target exploded')
|
|
334
|
+
mockCommandExecute.mockRejectedValue(failure)
|
|
335
|
+
const schedule = buildCommandSchedule()
|
|
336
|
+
const harness = buildWorkerContext(schedule)
|
|
337
|
+
|
|
338
|
+
await expect(runWorker(schedule, harness)).rejects.toBe(failure)
|
|
339
|
+
|
|
340
|
+
expect(harness.em.flush).not.toHaveBeenCalled()
|
|
341
|
+
expectNextRunWrite(harness.execute, cronTiming)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
it('uses the timing loaded before the target ran, even if the entity is mutated during the run', async () => {
|
|
345
|
+
const schedule = buildCommandSchedule()
|
|
346
|
+
mockCommandExecute.mockImplementation(async () => {
|
|
347
|
+
schedule.scheduleValue = '0 0 1 1 *'
|
|
348
|
+
schedule.timezone = 'UTC'
|
|
349
|
+
return { result: { ok: true }, logEntry: null }
|
|
350
|
+
})
|
|
351
|
+
const harness = buildWorkerContext(schedule)
|
|
352
|
+
|
|
353
|
+
await runWorker(schedule, harness)
|
|
354
|
+
|
|
355
|
+
expectNextRunWrite(harness.execute, cronTiming)
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
it('writes nothing when BullMQ has no scheduler for the schedule', async () => {
|
|
359
|
+
mockGetJobScheduler.mockResolvedValue(undefined)
|
|
360
|
+
const schedule = buildCommandSchedule()
|
|
361
|
+
const harness = buildWorkerContext(schedule)
|
|
362
|
+
|
|
363
|
+
await runWorker(schedule, harness)
|
|
364
|
+
|
|
365
|
+
expect(mockCommandExecute).toHaveBeenCalledTimes(1)
|
|
366
|
+
expect(mockBullQueueClose).toHaveBeenCalledTimes(1)
|
|
367
|
+
expect(harness.execute).not.toHaveBeenCalled()
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
it('writes nothing when the BullMQ scheduler carries a different timing than the run loaded', async () => {
|
|
371
|
+
mockGetJobScheduler.mockResolvedValue({ next: bullmqNextMillis, pattern: '0 0 1 1 *', tz: 'Europe/Warsaw' })
|
|
372
|
+
const schedule = buildCommandSchedule()
|
|
373
|
+
const harness = buildWorkerContext(schedule)
|
|
374
|
+
|
|
375
|
+
await runWorker(schedule, harness)
|
|
376
|
+
|
|
377
|
+
expect(mockGetJobScheduler).toHaveBeenCalledTimes(1)
|
|
378
|
+
expect(harness.execute).not.toHaveBeenCalled()
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
it('keeps a completed run successful when the BullMQ read fails', async () => {
|
|
382
|
+
mockGetJobScheduler.mockRejectedValue(new Error('redis unavailable'))
|
|
383
|
+
const schedule = buildCommandSchedule()
|
|
384
|
+
const harness = buildWorkerContext(schedule)
|
|
385
|
+
|
|
386
|
+
await expect(runWorker(schedule, harness)).resolves.toBeUndefined()
|
|
387
|
+
|
|
388
|
+
expect(schedule.lastRunAt).toBeInstanceOf(Date)
|
|
389
|
+
expect(harness.em.flush).toHaveBeenCalledTimes(1)
|
|
390
|
+
expect(mockBullQueueClose).toHaveBeenCalledTimes(1)
|
|
391
|
+
expect(harness.execute).not.toHaveBeenCalled()
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
it('keeps a completed run successful when the nextRunAt write fails', async () => {
|
|
395
|
+
const schedule = buildCommandSchedule()
|
|
396
|
+
const harness = buildWorkerContext(schedule)
|
|
397
|
+
harness.execute.mockRejectedValue(new Error('connection terminated'))
|
|
398
|
+
|
|
399
|
+
await expect(runWorker(schedule, harness)).resolves.toBeUndefined()
|
|
400
|
+
|
|
401
|
+
expect(harness.execute).toHaveBeenCalledTimes(1)
|
|
402
|
+
expect(emitSchedulerEvent).toHaveBeenCalledWith('scheduler.job.completed', expect.objectContaining({ id: scheduleId }))
|
|
403
|
+
})
|
|
404
|
+
|
|
405
|
+
it('rethrows the target error, not the sync error, when both fail', async () => {
|
|
406
|
+
const failure = new Error('target exploded')
|
|
407
|
+
mockCommandExecute.mockRejectedValue(failure)
|
|
408
|
+
mockGetJobScheduler.mockRejectedValue(new Error('redis unavailable'))
|
|
409
|
+
const schedule = buildCommandSchedule()
|
|
410
|
+
|
|
411
|
+
await expect(runWorker(schedule, buildWorkerContext(schedule))).rejects.toBe(failure)
|
|
412
|
+
expect(mockGetJobScheduler).toHaveBeenCalledTimes(1)
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
it('does not read BullMQ or write when the schedule does not exist', async () => {
|
|
416
|
+
const harness = buildWorkerContext(null)
|
|
417
|
+
|
|
418
|
+
await executeScheduleWorker(
|
|
419
|
+
{
|
|
420
|
+
id: 'queued-job-1',
|
|
421
|
+
payload: { scheduleId, tenantId: 'tenant-a', organizationId: 'org-a', scopeType: 'organization' },
|
|
422
|
+
createdAt: new Date().toISOString(),
|
|
423
|
+
},
|
|
424
|
+
harness.context as never,
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
expect(mockBullQueueConstructor).not.toHaveBeenCalled()
|
|
428
|
+
expect(harness.execute).not.toHaveBeenCalled()
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
it('does not read BullMQ or write when the schedule is disabled', async () => {
|
|
432
|
+
const schedule = buildCommandSchedule({ isEnabled: false })
|
|
433
|
+
const harness = buildWorkerContext(schedule)
|
|
434
|
+
|
|
435
|
+
await runWorker(schedule, harness)
|
|
436
|
+
|
|
437
|
+
expect(mockCommandExecute).not.toHaveBeenCalled()
|
|
438
|
+
expect(mockBullQueueConstructor).not.toHaveBeenCalled()
|
|
439
|
+
expect(harness.execute).not.toHaveBeenCalled()
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
it.each([
|
|
443
|
+
['scope type', { scopeType: 'tenant' }],
|
|
444
|
+
['tenant', { tenantId: 'tenant-b' }],
|
|
445
|
+
['organization', { organizationId: 'org-b' }],
|
|
446
|
+
['scope carried by a CLI-shaped payload', { scopeType: undefined, tenantId: undefined, organizationId: undefined }],
|
|
447
|
+
])('does not read BullMQ or write when the payload %s does not match the schedule', async (_label, payloadOverrides) => {
|
|
448
|
+
const schedule = buildCommandSchedule()
|
|
449
|
+
const harness = buildWorkerContext(schedule)
|
|
450
|
+
|
|
451
|
+
await expect(runWorker(schedule, harness, payloadOverrides)).rejects.toThrow(/mismatch/)
|
|
452
|
+
|
|
453
|
+
expect(mockBullQueueConstructor).not.toHaveBeenCalled()
|
|
454
|
+
expect(harness.execute).not.toHaveBeenCalled()
|
|
455
|
+
})
|
|
456
|
+
|
|
457
|
+
it('does not read BullMQ or write under the local strategy', async () => {
|
|
458
|
+
process.env.QUEUE_STRATEGY = 'local'
|
|
459
|
+
const schedule = buildCommandSchedule()
|
|
460
|
+
const harness = buildWorkerContext(schedule)
|
|
461
|
+
|
|
462
|
+
await runWorker(schedule, harness)
|
|
463
|
+
|
|
464
|
+
expect(mockCommandExecute).toHaveBeenCalledTimes(1)
|
|
465
|
+
expect(mockBullQueueConstructor).not.toHaveBeenCalled()
|
|
466
|
+
expect(harness.execute).not.toHaveBeenCalled()
|
|
467
|
+
})
|
|
468
|
+
})
|
|
@@ -22,6 +22,10 @@ registerModules([
|
|
|
22
22
|
},
|
|
23
23
|
] as never)
|
|
24
24
|
|
|
25
|
+
// These suites cover the run itself; the nextRunAt sync that follows it is
|
|
26
|
+
// async-strategy only and has its own suite.
|
|
27
|
+
delete process.env.QUEUE_STRATEGY
|
|
28
|
+
|
|
25
29
|
const mockCommandExecute = jest.fn()
|
|
26
30
|
|
|
27
31
|
jest.mock('@open-mercato/shared/lib/commands', () => ({
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
sanitizeSchedulerTargetPayload,
|
|
20
20
|
validateSchedulerTargetPayload,
|
|
21
21
|
} from '../lib/safeQueueTargets'
|
|
22
|
+
import { syncScheduleNextRunAt, type ScheduleTimingSnapshot } from '../lib/nextRunSync.js'
|
|
22
23
|
|
|
23
24
|
const logger = createLogger('scheduler').child({ component: 'worker' })
|
|
24
25
|
|
|
@@ -69,10 +70,33 @@ type RbacServiceLike = {
|
|
|
69
70
|
* - Checking feature flags and conditions
|
|
70
71
|
* - Enqueuing target job or executing command
|
|
71
72
|
* - Updating last run time
|
|
73
|
+
* - Mirroring BullMQ's next fire time into nextRunAt once the run is over
|
|
72
74
|
*/
|
|
73
75
|
export default async function executeScheduleWorker(
|
|
74
76
|
job: QueuedJob<ExecuteSchedulePayload>,
|
|
75
77
|
ctx: JobContext & HandlerContext,
|
|
78
|
+
): Promise<void> {
|
|
79
|
+
const execution: ScheduleExecution = { timing: null }
|
|
80
|
+
try {
|
|
81
|
+
await executeSchedule(job, ctx, execution)
|
|
82
|
+
} finally {
|
|
83
|
+
// Runs on every outcome of a started execution — completed, skipped,
|
|
84
|
+
// refused or thrown — because BullMQ has consumed the slot either way.
|
|
85
|
+
// A job rejected before it started (unknown, mismatched or disabled
|
|
86
|
+
// schedule) never sets the timing and is left alone.
|
|
87
|
+
const { timing } = execution
|
|
88
|
+
if (timing) {
|
|
89
|
+
await syncScheduleNextRunAt(() => ctx.resolve<EntityManager>('em'), timing)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type ScheduleExecution = { timing: ScheduleTimingSnapshot | null }
|
|
95
|
+
|
|
96
|
+
async function executeSchedule(
|
|
97
|
+
job: QueuedJob<ExecuteSchedulePayload>,
|
|
98
|
+
ctx: JobContext & HandlerContext,
|
|
99
|
+
execution: ScheduleExecution,
|
|
76
100
|
): Promise<void> {
|
|
77
101
|
logger.debug('Processing job', {
|
|
78
102
|
jobId: ctx.jobId,
|
|
@@ -144,6 +168,17 @@ export default async function executeScheduleWorker(
|
|
|
144
168
|
return
|
|
145
169
|
}
|
|
146
170
|
|
|
171
|
+
// Captured before the target runs: the sync that follows the run must compare
|
|
172
|
+
// against the timing this execution loaded, not whatever the entity holds later.
|
|
173
|
+
execution.timing = {
|
|
174
|
+
scheduleId: schedule.id,
|
|
175
|
+
tenantId: schedule.tenantId ?? null,
|
|
176
|
+
organizationId: schedule.organizationId ?? null,
|
|
177
|
+
scheduleType: schedule.scheduleType,
|
|
178
|
+
scheduleValue: schedule.scheduleValue,
|
|
179
|
+
timezone: schedule.timezone,
|
|
180
|
+
}
|
|
181
|
+
|
|
147
182
|
// Emit started event
|
|
148
183
|
await emitSchedulerEvent('scheduler.job.started', {
|
|
149
184
|
id: schedule.id,
|