@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
|
@@ -2,10 +2,6 @@ import { LocalSchedulerService } from '../localSchedulerService'
|
|
|
2
2
|
import type { EntityManager } from '@mikro-orm/core'
|
|
3
3
|
import type { Queue } from '@open-mercato/queue'
|
|
4
4
|
import { ScheduledJob } from '../../data/entities.js'
|
|
5
|
-
import {
|
|
6
|
-
clearSchedulerSafeCommandsForTests,
|
|
7
|
-
registerSchedulerSafeCommands,
|
|
8
|
-
} from '../../lib/scheduler-safe-commands'
|
|
9
5
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
10
6
|
|
|
11
7
|
jest.mock('@open-mercato/shared/lib/logger', () => {
|
|
@@ -58,19 +54,12 @@ describe('LocalSchedulerService', () => {
|
|
|
58
54
|
let mockForkedEm: jest.Mocked<EntityManager>
|
|
59
55
|
let mockQueue: jest.Mocked<Queue>
|
|
60
56
|
let mockQueueFactory: jest.Mock
|
|
61
|
-
let mockRbacService: { tenantHasFeature: jest.Mock
|
|
57
|
+
let mockRbacService: { tenantHasFeature: jest.Mock }
|
|
62
58
|
let mockLockStrategy: { runWithLock: jest.Mock }
|
|
63
59
|
|
|
64
60
|
beforeEach(() => {
|
|
65
61
|
jest.clearAllMocks()
|
|
66
62
|
jest.useFakeTimers()
|
|
67
|
-
clearSchedulerSafeCommandsForTests()
|
|
68
|
-
registerSchedulerSafeCommands([
|
|
69
|
-
{
|
|
70
|
-
commandId: 'test:command',
|
|
71
|
-
requiredFeatures: ['scheduler.jobs.manage'],
|
|
72
|
-
},
|
|
73
|
-
])
|
|
74
63
|
|
|
75
64
|
// Create mock queue
|
|
76
65
|
mockQueue = {
|
|
@@ -95,7 +84,6 @@ describe('LocalSchedulerService', () => {
|
|
|
95
84
|
// Create mock RBAC service
|
|
96
85
|
mockRbacService = {
|
|
97
86
|
tenantHasFeature: jest.fn(),
|
|
98
|
-
userHasAllFeatures: jest.fn().mockResolvedValue(true),
|
|
99
87
|
}
|
|
100
88
|
|
|
101
89
|
// Create service
|
|
@@ -125,6 +113,16 @@ describe('LocalSchedulerService', () => {
|
|
|
125
113
|
expect(loggerInfo).toHaveBeenCalledWith('Polling engine started')
|
|
126
114
|
})
|
|
127
115
|
|
|
116
|
+
it('should warn that the local strategy has no cross-process protection', async () => {
|
|
117
|
+
mockForkedEm.find.mockResolvedValue([])
|
|
118
|
+
|
|
119
|
+
await service.start()
|
|
120
|
+
|
|
121
|
+
expect(loggerWarn).toHaveBeenCalledWith(
|
|
122
|
+
expect.stringContaining('no cross-process protection'),
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
|
|
128
126
|
it('should run initial poll immediately', async () => {
|
|
129
127
|
mockForkedEm.find.mockResolvedValue([])
|
|
130
128
|
|
|
@@ -295,7 +293,6 @@ describe('LocalSchedulerService', () => {
|
|
|
295
293
|
targetType: 'command',
|
|
296
294
|
targetCommand: 'test:command',
|
|
297
295
|
targetQueue: null,
|
|
298
|
-
createdByUserId: 'user-1',
|
|
299
296
|
})
|
|
300
297
|
|
|
301
298
|
mockForkedEm.find.mockResolvedValue([schedule])
|
|
@@ -315,85 +312,8 @@ describe('LocalSchedulerService', () => {
|
|
|
315
312
|
tenantId: null,
|
|
316
313
|
organizationId: null,
|
|
317
314
|
}),
|
|
318
|
-
ctx: expect.objectContaining({
|
|
319
|
-
auth: expect.objectContaining({
|
|
320
|
-
sub: 'user-1',
|
|
321
|
-
userId: 'user-1',
|
|
322
|
-
tenantId: null,
|
|
323
|
-
orgId: null,
|
|
324
|
-
}),
|
|
325
|
-
}),
|
|
326
|
-
})
|
|
327
|
-
)
|
|
328
|
-
expect(mockRbacService.userHasAllFeatures).toHaveBeenCalledWith(
|
|
329
|
-
'user-1',
|
|
330
|
-
['scheduler.jobs.manage'],
|
|
331
|
-
{ tenantId: null, organizationId: null }
|
|
332
|
-
)
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
it('should reject command target that is not scheduler safe', async () => {
|
|
336
|
-
const schedule = createSchedule({
|
|
337
|
-
targetType: 'command',
|
|
338
|
-
targetCommand: 'unsafe.command',
|
|
339
|
-
targetQueue: null,
|
|
340
|
-
createdByUserId: 'user-1',
|
|
341
|
-
})
|
|
342
|
-
|
|
343
|
-
mockForkedEm.find.mockResolvedValue([schedule])
|
|
344
|
-
mockForkedEm.findOne.mockResolvedValue({ ...schedule })
|
|
345
|
-
mockLockStrategy.runWithLock.mockImplementation(async (_key: string, fn: () => Promise<unknown>) => {
|
|
346
|
-
await fn()
|
|
347
|
-
return { acquired: true }
|
|
348
|
-
})
|
|
349
|
-
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation()
|
|
350
|
-
|
|
351
|
-
await service.start()
|
|
352
|
-
|
|
353
|
-
expect(mockCommandBusInstance.execute).not.toHaveBeenCalled()
|
|
354
|
-
expect(mockEmitSchedulerEvent).toHaveBeenCalledWith(
|
|
355
|
-
'scheduler.job.failed',
|
|
356
|
-
expect.objectContaining({
|
|
357
|
-
id: 'test-1',
|
|
358
|
-
error: 'Scheduled command is not allowed',
|
|
359
|
-
})
|
|
360
|
-
)
|
|
361
|
-
consoleErrorSpy.mockRestore()
|
|
362
|
-
})
|
|
363
|
-
|
|
364
|
-
it('should reject command target when the schedule creator lacks required features', async () => {
|
|
365
|
-
const schedule = createSchedule({
|
|
366
|
-
targetType: 'command',
|
|
367
|
-
targetCommand: 'test:command',
|
|
368
|
-
targetQueue: null,
|
|
369
|
-
createdByUserId: 'user-1',
|
|
370
|
-
})
|
|
371
|
-
|
|
372
|
-
mockRbacService.userHasAllFeatures.mockResolvedValueOnce(false)
|
|
373
|
-
mockForkedEm.find.mockResolvedValue([schedule])
|
|
374
|
-
mockForkedEm.findOne.mockResolvedValue({ ...schedule })
|
|
375
|
-
mockLockStrategy.runWithLock.mockImplementation(async (_key: string, fn: () => Promise<unknown>) => {
|
|
376
|
-
await fn()
|
|
377
|
-
return { acquired: true }
|
|
378
|
-
})
|
|
379
|
-
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation()
|
|
380
|
-
|
|
381
|
-
await service.start()
|
|
382
|
-
|
|
383
|
-
expect(mockCommandBusInstance.execute).not.toHaveBeenCalled()
|
|
384
|
-
expect(mockRbacService.userHasAllFeatures).toHaveBeenCalledWith(
|
|
385
|
-
'user-1',
|
|
386
|
-
['scheduler.jobs.manage'],
|
|
387
|
-
{ tenantId: null, organizationId: null }
|
|
388
|
-
)
|
|
389
|
-
expect(mockEmitSchedulerEvent).toHaveBeenCalledWith(
|
|
390
|
-
'scheduler.job.failed',
|
|
391
|
-
expect.objectContaining({
|
|
392
|
-
id: 'test-1',
|
|
393
|
-
error: 'Scheduled command creator is not authorized',
|
|
394
315
|
})
|
|
395
316
|
)
|
|
396
|
-
consoleErrorSpy.mockRestore()
|
|
397
317
|
})
|
|
398
318
|
|
|
399
319
|
it('should emit started event', async () => {
|
|
@@ -677,7 +597,6 @@ describe('LocalSchedulerService', () => {
|
|
|
677
597
|
scopeType: 'organization',
|
|
678
598
|
tenantId: 'tenant-1',
|
|
679
599
|
organizationId: 'org-1',
|
|
680
|
-
createdByUserId: 'user-1',
|
|
681
600
|
})
|
|
682
601
|
|
|
683
602
|
mockForkedEm.find.mockResolvedValue([schedule])
|
|
@@ -699,11 +618,6 @@ describe('LocalSchedulerService', () => {
|
|
|
699
618
|
organizationId: 'org-1',
|
|
700
619
|
}),
|
|
701
620
|
ctx: expect.objectContaining({
|
|
702
|
-
auth: expect.objectContaining({
|
|
703
|
-
sub: 'user-1',
|
|
704
|
-
tenantId: 'tenant-1',
|
|
705
|
-
orgId: 'org-1',
|
|
706
|
-
}),
|
|
707
621
|
selectedOrganizationId: 'org-1',
|
|
708
622
|
organizationIds: ['org-1'],
|
|
709
623
|
}),
|
|
@@ -4,14 +4,6 @@ import { ScheduledJob } from '../../data/entities.js'
|
|
|
4
4
|
import type { BullMQSchedulerService } from '../bullmqSchedulerService'
|
|
5
5
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
6
6
|
|
|
7
|
-
jest.mock('@open-mercato/shared/lib/i18n/server', () => ({
|
|
8
|
-
resolveTranslations: jest.fn().mockResolvedValue({
|
|
9
|
-
translate: (key: string, _fallback?: string, params?: Record<string, unknown>) => (
|
|
10
|
-
params?.max === undefined ? `translated:${key}` : `translated:${key}:max=${params.max}`
|
|
11
|
-
),
|
|
12
|
-
}),
|
|
13
|
-
}))
|
|
14
|
-
|
|
15
7
|
jest.mock('@open-mercato/shared/lib/logger', () => {
|
|
16
8
|
const mocked = {
|
|
17
9
|
debug: jest.fn(),
|
|
@@ -192,37 +184,6 @@ describe('SchedulerService', () => {
|
|
|
192
184
|
timezone: 'America/New_York',
|
|
193
185
|
}))
|
|
194
186
|
})
|
|
195
|
-
|
|
196
|
-
it('should reject a new enabled tenant schedule when the tenant active cap is reached', async () => {
|
|
197
|
-
const originalLimit = process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT
|
|
198
|
-
process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT = '2'
|
|
199
|
-
try {
|
|
200
|
-
mockForkedEm.findOne.mockResolvedValue(null)
|
|
201
|
-
mockForkedEm.count.mockResolvedValue(2)
|
|
202
|
-
|
|
203
|
-
await expect(service.register({
|
|
204
|
-
...baseRegistration,
|
|
205
|
-
scopeType: 'tenant',
|
|
206
|
-
tenantId: 'tenant-1',
|
|
207
|
-
scheduleType: 'interval',
|
|
208
|
-
scheduleValue: '15m',
|
|
209
|
-
})).rejects.toMatchObject({
|
|
210
|
-
status: 422,
|
|
211
|
-
body: { error: 'translated:scheduler.error.active_schedule_limit:max=2' },
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
expect(mockForkedEm.count).toHaveBeenCalledWith(ScheduledJob, {
|
|
215
|
-
tenantId: 'tenant-1',
|
|
216
|
-
isEnabled: true,
|
|
217
|
-
deletedAt: null,
|
|
218
|
-
})
|
|
219
|
-
expect(mockForkedEm.create).not.toHaveBeenCalled()
|
|
220
|
-
expect(mockForkedEm.flush).not.toHaveBeenCalled()
|
|
221
|
-
} finally {
|
|
222
|
-
if (originalLimit === undefined) delete process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT
|
|
223
|
-
else process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT = originalLimit
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
187
|
})
|
|
227
188
|
|
|
228
189
|
describe('unregister', () => {
|
|
@@ -419,68 +380,6 @@ describe('SchedulerService', () => {
|
|
|
419
380
|
|
|
420
381
|
expect(mockBullMQService.unregister).toHaveBeenCalledWith('test-1')
|
|
421
382
|
})
|
|
422
|
-
|
|
423
|
-
it('should reject enabling a tenant schedule when the tenant active cap is reached', async () => {
|
|
424
|
-
const originalLimit = process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT
|
|
425
|
-
process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT = '2'
|
|
426
|
-
try {
|
|
427
|
-
const schedule = {
|
|
428
|
-
id: 'test-1',
|
|
429
|
-
tenantId: 'tenant-1',
|
|
430
|
-
organizationId: null,
|
|
431
|
-
scopeType: 'tenant',
|
|
432
|
-
isEnabled: false,
|
|
433
|
-
scheduleType: 'interval',
|
|
434
|
-
scheduleValue: '15m',
|
|
435
|
-
timezone: 'UTC',
|
|
436
|
-
} as ScheduledJob
|
|
437
|
-
|
|
438
|
-
mockForkedEm.findOne.mockResolvedValue(schedule)
|
|
439
|
-
mockForkedEm.count.mockResolvedValue(2)
|
|
440
|
-
|
|
441
|
-
await expect(service.update('test-1', { isEnabled: true })).rejects.toMatchObject({
|
|
442
|
-
status: 422,
|
|
443
|
-
body: { error: 'translated:scheduler.error.active_schedule_limit:max=2' },
|
|
444
|
-
})
|
|
445
|
-
|
|
446
|
-
expect(mockForkedEm.count).toHaveBeenCalledWith(ScheduledJob, {
|
|
447
|
-
tenantId: 'tenant-1',
|
|
448
|
-
isEnabled: true,
|
|
449
|
-
deletedAt: null,
|
|
450
|
-
})
|
|
451
|
-
expect(schedule.isEnabled).toBe(false)
|
|
452
|
-
expect(mockForkedEm.flush).not.toHaveBeenCalled()
|
|
453
|
-
} finally {
|
|
454
|
-
if (originalLimit === undefined) delete process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT
|
|
455
|
-
else process.env.OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT = originalLimit
|
|
456
|
-
}
|
|
457
|
-
})
|
|
458
|
-
|
|
459
|
-
it('should reject interval updates below one minute', async () => {
|
|
460
|
-
const nextRunAt = new Date('2026-01-01T00:15:00.000Z')
|
|
461
|
-
const schedule = {
|
|
462
|
-
id: 'test-1',
|
|
463
|
-
tenantId: 'tenant-1',
|
|
464
|
-
organizationId: null,
|
|
465
|
-
scopeType: 'tenant',
|
|
466
|
-
isEnabled: true,
|
|
467
|
-
scheduleType: 'interval',
|
|
468
|
-
scheduleValue: '15m',
|
|
469
|
-
timezone: 'UTC',
|
|
470
|
-
nextRunAt,
|
|
471
|
-
} as ScheduledJob
|
|
472
|
-
|
|
473
|
-
mockForkedEm.findOne.mockResolvedValue(schedule)
|
|
474
|
-
|
|
475
|
-
await expect(service.update('test-1', {
|
|
476
|
-
scheduleType: 'interval',
|
|
477
|
-
scheduleValue: '1s',
|
|
478
|
-
})).rejects.toThrow('Failed to calculate next run time')
|
|
479
|
-
|
|
480
|
-
expect(schedule.scheduleValue).toBe('15m')
|
|
481
|
-
expect(schedule.nextRunAt).toBe(nextRunAt)
|
|
482
|
-
expect(mockForkedEm.flush).not.toHaveBeenCalled()
|
|
483
|
-
})
|
|
484
383
|
})
|
|
485
384
|
|
|
486
385
|
describe('findByModule', () => {
|
|
@@ -2,18 +2,12 @@ import type { EntityManager } from '@mikro-orm/core'
|
|
|
2
2
|
import { ScheduledJob } from '../data/entities.js'
|
|
3
3
|
import { recalculateNextRun } from '../lib/nextRunCalculator'
|
|
4
4
|
import { parseCronExpression } from '../lib/cronParser'
|
|
5
|
-
import {
|
|
6
|
-
import { getRedisUrlOrThrow
|
|
5
|
+
import { parseInterval } from '../lib/intervalParser'
|
|
6
|
+
import { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'
|
|
7
7
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
8
8
|
|
|
9
9
|
const logger = createLogger('scheduler').child({ component: 'bullmq' })
|
|
10
10
|
|
|
11
|
-
interface BullJobScheduler {
|
|
12
|
-
key: string
|
|
13
|
-
name: string
|
|
14
|
-
id?: string | null
|
|
15
|
-
}
|
|
16
|
-
|
|
17
11
|
interface BullRepeatableJob {
|
|
18
12
|
key: string
|
|
19
13
|
name: string
|
|
@@ -27,32 +21,20 @@ interface BullRepeatOptions {
|
|
|
27
21
|
}
|
|
28
22
|
|
|
29
23
|
interface BullQueue {
|
|
30
|
-
add(name: string, data: unknown, opts?:
|
|
24
|
+
add(name: string, data: unknown, opts?: unknown): Promise<unknown>
|
|
31
25
|
getRepeatableJobs?(): Promise<BullRepeatableJob[]>
|
|
32
26
|
removeRepeatableByKey?(key: string): Promise<boolean>
|
|
33
|
-
upsertJobScheduler?(
|
|
34
|
-
id: string,
|
|
35
|
-
repeatOptions: BullRepeatOptions,
|
|
36
|
-
jobTemplate: { name: string; data: unknown; opts: Record<string, unknown> },
|
|
37
|
-
): Promise<unknown>
|
|
38
|
-
getJobSchedulers?(): Promise<BullJobScheduler[]>
|
|
39
|
-
removeJobScheduler?(id: string): Promise<boolean>
|
|
40
27
|
close(): Promise<void>
|
|
41
28
|
}
|
|
42
29
|
|
|
43
|
-
type BullJobSchedulerQueue = BullQueue & Required<Pick<
|
|
44
|
-
BullQueue,
|
|
45
|
-
'upsertJobScheduler' | 'getJobSchedulers' | 'removeJobScheduler'
|
|
46
|
-
>>
|
|
47
|
-
|
|
48
30
|
/**
|
|
49
|
-
* Production scheduler using BullMQ
|
|
31
|
+
* Production scheduler using BullMQ repeatable jobs.
|
|
50
32
|
*
|
|
51
33
|
* Requires Redis. Set QUEUE_STRATEGY=async to use this service.
|
|
52
34
|
*
|
|
53
|
-
* This service syncs database schedules with BullMQ's
|
|
35
|
+
* This service syncs database schedules with BullMQ's repeat mechanism.
|
|
54
36
|
* When a schedule is created/updated/deleted in the database, this service
|
|
55
|
-
* adds/updates/removes the corresponding BullMQ
|
|
37
|
+
* adds/updates/removes the corresponding BullMQ repeatable job.
|
|
56
38
|
*
|
|
57
39
|
* BullMQ handles:
|
|
58
40
|
* - Exact timing based on cron expressions or intervals
|
|
@@ -76,9 +58,7 @@ export class BullMQSchedulerService {
|
|
|
76
58
|
if (!this.queue) {
|
|
77
59
|
try {
|
|
78
60
|
const { Queue } = await import('bullmq')
|
|
79
|
-
this.queue = new Queue('scheduler-execution', {
|
|
80
|
-
connection: parseRedisUrl(getRedisUrlOrThrow('QUEUE')),
|
|
81
|
-
})
|
|
61
|
+
this.queue = new Queue('scheduler-execution', { connection: { url: getRedisUrlOrThrow('QUEUE') } })
|
|
82
62
|
} catch {
|
|
83
63
|
throw new Error('BullMQ is required for async scheduler. Install it with: npm install bullmq')
|
|
84
64
|
}
|
|
@@ -90,33 +70,22 @@ export class BullMQSchedulerService {
|
|
|
90
70
|
return `schedule-${scheduleId}`
|
|
91
71
|
}
|
|
92
72
|
|
|
93
|
-
private
|
|
94
|
-
const candidate =
|
|
95
|
-
.
|
|
73
|
+
private getScheduleIdFromRepeatableJob(job: BullRepeatableJob): string | null {
|
|
74
|
+
const candidate = job.id?.startsWith('schedule-')
|
|
75
|
+
? job.id
|
|
76
|
+
: job.name?.startsWith('schedule-')
|
|
77
|
+
? job.name
|
|
78
|
+
: null
|
|
96
79
|
|
|
97
80
|
return candidate ? candidate.slice('schedule-'.length) : null
|
|
98
81
|
}
|
|
99
82
|
|
|
100
|
-
private isJobSchedulerForSchedule(job: BullJobScheduler, scheduleId: string): boolean {
|
|
101
|
-
return this.getScheduleIdFromJobScheduler(job) === scheduleId
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
private isCanonicalJobScheduler(job: BullJobScheduler, scheduleId: string): boolean {
|
|
105
|
-
return job.key === this.getScheduleJobName(scheduleId)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private supportsJobSchedulers(queue: BullQueue): queue is BullJobSchedulerQueue {
|
|
109
|
-
return typeof queue.upsertJobScheduler === 'function'
|
|
110
|
-
&& typeof queue.getJobSchedulers === 'function'
|
|
111
|
-
&& typeof queue.removeJobScheduler === 'function'
|
|
112
|
-
}
|
|
113
|
-
|
|
114
83
|
private isRepeatableJobForSchedule(job: BullRepeatableJob, scheduleId: string): boolean {
|
|
115
84
|
const jobName = this.getScheduleJobName(scheduleId)
|
|
116
85
|
return job.id === jobName || job.name === jobName
|
|
117
86
|
}
|
|
118
87
|
|
|
119
|
-
private async
|
|
88
|
+
private async removeRepeatableJobsForSchedule(queue: BullQueue, scheduleId: string): Promise<number> {
|
|
120
89
|
const repeatableJobs = await queue.getRepeatableJobs?.()
|
|
121
90
|
if (!repeatableJobs || typeof queue.removeRepeatableByKey !== 'function') {
|
|
122
91
|
return 0
|
|
@@ -128,11 +97,12 @@ export class BullMQSchedulerService {
|
|
|
128
97
|
await queue.removeRepeatableByKey(job.key)
|
|
129
98
|
removed += 1
|
|
130
99
|
}
|
|
100
|
+
|
|
131
101
|
return removed
|
|
132
102
|
}
|
|
133
103
|
|
|
134
104
|
/**
|
|
135
|
-
* Register a schedule with BullMQ
|
|
105
|
+
* Register a schedule with BullMQ repeatable jobs
|
|
136
106
|
* @param schedule - The schedule to register
|
|
137
107
|
* @param options - Optional configuration
|
|
138
108
|
* @param options.skipNextRunUpdate - If true, skip updating nextRunAt (used when called from hooks)
|
|
@@ -161,14 +131,15 @@ export class BullMQSchedulerService {
|
|
|
161
131
|
// Build BullMQ repeat options based on schedule type
|
|
162
132
|
const repeatOpts = this.buildRepeatOptions(schedule)
|
|
163
133
|
|
|
164
|
-
//
|
|
134
|
+
// Add repeatable job to BullMQ
|
|
165
135
|
// IMPORTANT: Wrap in QueuedJob format to match queue strategy expectations
|
|
166
136
|
// BullMQ will store this in job.data, and the async strategy worker expects
|
|
167
137
|
// job.data to be a QueuedJob with id, payload, and createdAt
|
|
168
138
|
const queue = await this.getQueue()
|
|
169
139
|
const jobName = this.getScheduleJobName(schedule.id)
|
|
140
|
+
await this.removeRepeatableJobsForSchedule(queue, schedule.id)
|
|
170
141
|
|
|
171
|
-
//
|
|
142
|
+
// For repeatable jobs, we need to provide a stable ID in the data
|
|
172
143
|
// that will be used for each repeat instance
|
|
173
144
|
// CRITICAL: Include scope information (tenantId, organizationId, scopeType)
|
|
174
145
|
// for proper multi-tenant isolation and auditing
|
|
@@ -183,7 +154,7 @@ export class BullMQSchedulerService {
|
|
|
183
154
|
createdAt: new Date().toISOString(),
|
|
184
155
|
}
|
|
185
156
|
|
|
186
|
-
logger.debug('
|
|
157
|
+
logger.debug('Adding repeatable job', {
|
|
187
158
|
jobName,
|
|
188
159
|
scheduleId: schedule.id,
|
|
189
160
|
scopeType: schedule.scopeType,
|
|
@@ -192,40 +163,22 @@ export class BullMQSchedulerService {
|
|
|
192
163
|
repeatOpts,
|
|
193
164
|
})
|
|
194
165
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
await queue.upsertJobScheduler(
|
|
209
|
-
jobName,
|
|
210
|
-
repeatOpts,
|
|
211
|
-
{
|
|
212
|
-
name: jobName,
|
|
213
|
-
data: jobData,
|
|
214
|
-
opts: jobOptions,
|
|
166
|
+
await queue.add(
|
|
167
|
+
jobName, // Job name - used as part of repeatable job key
|
|
168
|
+
jobData, // Job data in QueuedJob format
|
|
169
|
+
{
|
|
170
|
+
repeat: repeatOpts,
|
|
171
|
+
// Don't set jobId for repeatable jobs - BullMQ generates unique IDs for each instance
|
|
172
|
+
removeOnComplete: {
|
|
173
|
+
age: 86400 * 30, // Keep completed jobs for 30 days (execution history)
|
|
174
|
+
count: 1000, // Keep last 1000 completed jobs
|
|
175
|
+
},
|
|
176
|
+
removeOnFail: {
|
|
177
|
+
age: 86400 * 90, // Keep failed jobs for 90 days (debugging/audit)
|
|
178
|
+
count: 5000, // Keep last 5000 failed jobs
|
|
215
179
|
},
|
|
216
|
-
)
|
|
217
|
-
for (const jobScheduler of existingJobSchedulers) {
|
|
218
|
-
if (
|
|
219
|
-
this.isJobSchedulerForSchedule(jobScheduler, schedule.id)
|
|
220
|
-
&& !this.isCanonicalJobScheduler(jobScheduler, schedule.id)
|
|
221
|
-
) {
|
|
222
|
-
await queue.removeJobScheduler(jobScheduler.key)
|
|
223
|
-
}
|
|
224
180
|
}
|
|
225
|
-
|
|
226
|
-
await this.removeLegacyRepeatableJobs(queue, schedule.id)
|
|
227
|
-
await queue.add(jobName, jobData, { repeat: repeatOpts, ...jobOptions })
|
|
228
|
-
}
|
|
181
|
+
)
|
|
229
182
|
|
|
230
183
|
logger.debug('Registered schedule', {
|
|
231
184
|
scheduleId: schedule.id,
|
|
@@ -241,28 +194,13 @@ export class BullMQSchedulerService {
|
|
|
241
194
|
}
|
|
242
195
|
|
|
243
196
|
/**
|
|
244
|
-
* Unregister a schedule from BullMQ
|
|
197
|
+
* Unregister a schedule from BullMQ repeatable jobs
|
|
245
198
|
*/
|
|
246
199
|
async unregister(scheduleId: string): Promise<void> {
|
|
247
200
|
try {
|
|
248
201
|
const queue = await this.getQueue()
|
|
249
|
-
if (this.supportsJobSchedulers(queue)) {
|
|
250
|
-
const jobSchedulers = await queue.getJobSchedulers()
|
|
251
|
-
const matchingJobSchedulers = jobSchedulers.filter((job) => (
|
|
252
|
-
this.isJobSchedulerForSchedule(job, scheduleId)
|
|
253
|
-
))
|
|
254
|
-
if (matchingJobSchedulers.length > 0) {
|
|
255
|
-
for (const jobScheduler of matchingJobSchedulers) {
|
|
256
|
-
await queue.removeJobScheduler(jobScheduler.key)
|
|
257
|
-
}
|
|
258
|
-
logger.debug('Unregistered schedule', { scheduleId })
|
|
259
|
-
} else {
|
|
260
|
-
logger.debug('No job scheduler found for schedule', { scheduleId })
|
|
261
|
-
}
|
|
262
|
-
return
|
|
263
|
-
}
|
|
264
202
|
|
|
265
|
-
const removed = await this.
|
|
203
|
+
const removed = await this.removeRepeatableJobsForSchedule(queue, scheduleId)
|
|
266
204
|
if (removed > 0) {
|
|
267
205
|
logger.debug('Unregistered schedule', { scheduleId })
|
|
268
206
|
} else {
|
|
@@ -284,33 +222,15 @@ export class BullMQSchedulerService {
|
|
|
284
222
|
|
|
285
223
|
logger.debug('Starting full sync')
|
|
286
224
|
|
|
225
|
+
// Get all BullMQ repeatable jobs
|
|
226
|
+
const repeatableJobs = await queue.getRepeatableJobs?.() || []
|
|
287
227
|
const bullmqScheduleIds = new Set<string>()
|
|
288
228
|
const bullmqScheduleCounts = new Map<string, number>()
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
if (!scheduleId) continue
|
|
295
|
-
bullmqScheduleIds.add(scheduleId)
|
|
296
|
-
bullmqScheduleCounts.set(scheduleId, (bullmqScheduleCounts.get(scheduleId) ?? 0) + 1)
|
|
297
|
-
if (this.isCanonicalJobScheduler(job, scheduleId)) {
|
|
298
|
-
canonicalJobSchedulerIds.add(scheduleId)
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
} else {
|
|
302
|
-
const repeatableJobs = await queue.getRepeatableJobs?.() ?? []
|
|
303
|
-
for (const job of repeatableJobs) {
|
|
304
|
-
const candidate = job.id?.startsWith('schedule-')
|
|
305
|
-
? job.id
|
|
306
|
-
: job.name?.startsWith('schedule-')
|
|
307
|
-
? job.name
|
|
308
|
-
: null
|
|
309
|
-
if (!candidate) continue
|
|
310
|
-
const scheduleId = candidate.slice('schedule-'.length)
|
|
311
|
-
bullmqScheduleIds.add(scheduleId)
|
|
312
|
-
bullmqScheduleCounts.set(scheduleId, (bullmqScheduleCounts.get(scheduleId) ?? 0) + 1)
|
|
313
|
-
}
|
|
229
|
+
for (const job of repeatableJobs) {
|
|
230
|
+
const scheduleId = this.getScheduleIdFromRepeatableJob(job)
|
|
231
|
+
if (!scheduleId) continue
|
|
232
|
+
bullmqScheduleIds.add(scheduleId)
|
|
233
|
+
bullmqScheduleCounts.set(scheduleId, (bullmqScheduleCounts.get(scheduleId) ?? 0) + 1)
|
|
314
234
|
}
|
|
315
235
|
|
|
316
236
|
// Get enabled schedules from database in batches to avoid unbounded loads
|
|
@@ -334,11 +254,8 @@ export class BullMQSchedulerService {
|
|
|
334
254
|
if (!bullmqScheduleIds.has(schedule.id)) {
|
|
335
255
|
logger.debug('Registering missing schedule', { scheduleId: schedule.id, scheduleName: schedule.name })
|
|
336
256
|
await this.register(schedule)
|
|
337
|
-
} else if (
|
|
338
|
-
|
|
339
|
-
|| !canonicalJobSchedulerIds.has(schedule.id)
|
|
340
|
-
) {
|
|
341
|
-
logger.info('Migrating legacy or duplicate job scheduler', { scheduleId: schedule.id })
|
|
257
|
+
} else if ((bullmqScheduleCounts.get(schedule.id) ?? 0) > 1) {
|
|
258
|
+
logger.info('Repairing duplicate repeatable jobs', { scheduleId: schedule.id })
|
|
342
259
|
await this.register(schedule)
|
|
343
260
|
}
|
|
344
261
|
}
|
|
@@ -371,7 +288,7 @@ export class BullMQSchedulerService {
|
|
|
371
288
|
opts.pattern = schedule.scheduleValue
|
|
372
289
|
} else if (schedule.scheduleType === 'interval') {
|
|
373
290
|
// Parse interval (e.g., "15m", "2h", "1d")
|
|
374
|
-
const intervalMs =
|
|
291
|
+
const intervalMs = parseInterval(schedule.scheduleValue)
|
|
375
292
|
opts.every = intervalMs
|
|
376
293
|
} else {
|
|
377
294
|
throw new Error(`Unsupported schedule type: ${schedule.scheduleType}`)
|
|
@@ -381,17 +298,14 @@ export class BullMQSchedulerService {
|
|
|
381
298
|
}
|
|
382
299
|
|
|
383
300
|
/**
|
|
384
|
-
* Get list of all
|
|
301
|
+
* Get list of all repeatable jobs from BullMQ
|
|
385
302
|
*/
|
|
386
303
|
async getRepeatableJobs(): Promise<unknown[]> {
|
|
387
304
|
try {
|
|
388
305
|
const queue = await this.getQueue()
|
|
389
|
-
|
|
390
|
-
return await queue.getJobSchedulers()
|
|
391
|
-
}
|
|
392
|
-
return await queue.getRepeatableJobs?.() ?? []
|
|
306
|
+
return await queue.getRepeatableJobs?.() || []
|
|
393
307
|
} catch (error) {
|
|
394
|
-
logger.error('Failed to get
|
|
308
|
+
logger.error('Failed to get repeatable jobs', { err: error })
|
|
395
309
|
return []
|
|
396
310
|
}
|
|
397
311
|
}
|
|
@@ -7,7 +7,6 @@ import { LocalLockStrategy } from '../lib/localLockStrategy'
|
|
|
7
7
|
import { recalculateNextRun } from '../lib/nextRunCalculator'
|
|
8
8
|
import { emitSchedulerEvent } from '../events.js'
|
|
9
9
|
import { getGlobalEventBus } from '@open-mercato/shared/modules/events'
|
|
10
|
-
import { assertSchedulerSafeCommandAuthorized } from '../lib/scheduler-safe-commands.js'
|
|
11
10
|
import { buildScheduledCommandContext } from '../lib/commandContext.js'
|
|
12
11
|
import { buildQueueTargetPayload, buildSchedulerIdempotencyKey } from '../lib/queueTargetPayload.js'
|
|
13
12
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
@@ -16,7 +15,6 @@ const logger = createLogger('scheduler').child({ component: 'local' })
|
|
|
16
15
|
|
|
17
16
|
export interface RbacServiceLike {
|
|
18
17
|
tenantHasFeature(tenantId: string | null | undefined, feature: string, opts?: { organizationId?: string | null }): Promise<boolean>
|
|
19
|
-
userHasAllFeatures(userId: string, required: readonly string[], scope: { tenantId: string | null; organizationId: string | null }): Promise<boolean>
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
export interface LocalSchedulerConfig {
|
|
@@ -31,14 +29,19 @@ export interface LocalSchedulerConfig {
|
|
|
31
29
|
*
|
|
32
30
|
* Features:
|
|
33
31
|
* - PostgreSQL polling (configurable interval, default 30s)
|
|
34
|
-
* -
|
|
32
|
+
* - In-process duplicate prevention across overlapping poll ticks
|
|
35
33
|
* - Supports both queue and command targets
|
|
36
34
|
* - Emits the same events as async strategy
|
|
37
35
|
* - Updates nextRunAt after execution
|
|
38
|
-
*
|
|
36
|
+
*
|
|
39
37
|
* Limitations:
|
|
40
38
|
* - Polling delay (up to configured interval)
|
|
41
|
-
* - Single instance only (no distributed locking across instances)
|
|
39
|
+
* - Single instance only (no distributed locking across instances). Duplicate
|
|
40
|
+
* prevention is in-process only: the advisory lock serialises the claim, not
|
|
41
|
+
* the execution, and nextRunAt is advanced only after the target has run, so
|
|
42
|
+
* two processes polling the same database will both execute a due schedule.
|
|
43
|
+
* Run exactly one process with QUEUE_STRATEGY=local, or use the async
|
|
44
|
+
* strategy, which locks across instances.
|
|
42
45
|
* - Higher database load than async strategy
|
|
43
46
|
*
|
|
44
47
|
* Set QUEUE_STRATEGY=local to use this service.
|
|
@@ -68,6 +71,9 @@ export class LocalSchedulerService {
|
|
|
68
71
|
|
|
69
72
|
this.isRunning = true
|
|
70
73
|
logger.info('Starting polling engine', { pollIntervalMs: this.config.pollIntervalMs })
|
|
74
|
+
logger.warn(
|
|
75
|
+
'Local scheduler strategy provides no cross-process protection: duplicate prevention is in-process only, so a second process polling the same database will execute the same due schedule. Run exactly one process with QUEUE_STRATEGY=local, or use the async strategy for distributed locking.',
|
|
76
|
+
)
|
|
71
77
|
|
|
72
78
|
// Run initial poll immediately
|
|
73
79
|
await this.poll()
|
|
@@ -278,14 +284,6 @@ export class LocalSchedulerService {
|
|
|
278
284
|
}
|
|
279
285
|
|
|
280
286
|
const commandBus = new CommandBus()
|
|
281
|
-
const actorUserId = typeof schedule.createdByUserId === 'string' ? schedule.createdByUserId.trim() : ''
|
|
282
|
-
await assertSchedulerSafeCommandAuthorized({
|
|
283
|
-
commandId: schedule.targetCommand,
|
|
284
|
-
actorUserId,
|
|
285
|
-
tenantId: schedule.tenantId,
|
|
286
|
-
organizationId: schedule.organizationId,
|
|
287
|
-
rbacService: this.rbacService,
|
|
288
|
-
})
|
|
289
287
|
|
|
290
288
|
const commandInput = {
|
|
291
289
|
...((schedule.targetPayload as Record<string, unknown>) || {}),
|
|
@@ -293,7 +291,6 @@ export class LocalSchedulerService {
|
|
|
293
291
|
organizationId: schedule.organizationId,
|
|
294
292
|
}
|
|
295
293
|
|
|
296
|
-
// Build the schedule-scoped command context after the allowlist/RBAC gate.
|
|
297
294
|
const commandCtx = buildScheduledCommandContext(
|
|
298
295
|
schedule,
|
|
299
296
|
{
|