@open-mercato/scheduler 0.6.7 → 0.6.8-develop.6875.1.871a4afc94

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.
Files changed (69) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/scheduler/__integration__/TC-SCHED-007.spec.js +4 -1
  3. package/dist/modules/scheduler/__integration__/TC-SCHED-007.spec.js.map +2 -2
  4. package/dist/modules/scheduler/api/jobs/[id]/executions/route.js +4 -2
  5. package/dist/modules/scheduler/api/jobs/[id]/executions/route.js.map +2 -2
  6. package/dist/modules/scheduler/api/queue-jobs/[jobId]/route.js +4 -2
  7. package/dist/modules/scheduler/api/queue-jobs/[jobId]/route.js.map +2 -2
  8. package/dist/modules/scheduler/api/targets/route.js +5 -4
  9. package/dist/modules/scheduler/api/targets/route.js.map +2 -2
  10. package/dist/modules/scheduler/commands/jobs.js +33 -14
  11. package/dist/modules/scheduler/commands/jobs.js.map +2 -2
  12. package/dist/modules/scheduler/components/ExecutionDetailsDialog.js +1 -1
  13. package/dist/modules/scheduler/components/ExecutionDetailsDialog.js.map +2 -2
  14. package/dist/modules/scheduler/components/JobLogsModal.js +1 -1
  15. package/dist/modules/scheduler/components/JobLogsModal.js.map +2 -2
  16. package/dist/modules/scheduler/data/validators.js +27 -1
  17. package/dist/modules/scheduler/data/validators.js.map +2 -2
  18. package/dist/modules/scheduler/i18n/de.json +2 -0
  19. package/dist/modules/scheduler/i18n/en.json +2 -0
  20. package/dist/modules/scheduler/i18n/es.json +2 -0
  21. package/dist/modules/scheduler/i18n/ko.json +157 -0
  22. package/dist/modules/scheduler/i18n/pl.json +2 -0
  23. package/dist/modules/scheduler/lib/activeScheduleLimits.js +44 -0
  24. package/dist/modules/scheduler/lib/activeScheduleLimits.js.map +7 -0
  25. package/dist/modules/scheduler/lib/intervalParser.js +8 -3
  26. package/dist/modules/scheduler/lib/intervalParser.js.map +2 -2
  27. package/dist/modules/scheduler/lib/nextRunCalculator.js +8 -1
  28. package/dist/modules/scheduler/lib/nextRunCalculator.js.map +2 -2
  29. package/dist/modules/scheduler/lib/scheduler-safe-commands.js +73 -0
  30. package/dist/modules/scheduler/lib/scheduler-safe-commands.js.map +7 -0
  31. package/dist/modules/scheduler/services/bullmqSchedulerService.js +92 -43
  32. package/dist/modules/scheduler/services/bullmqSchedulerService.js.map +2 -2
  33. package/dist/modules/scheduler/services/localSchedulerService.js +9 -0
  34. package/dist/modules/scheduler/services/localSchedulerService.js.map +2 -2
  35. package/dist/modules/scheduler/services/schedulerService.js +23 -11
  36. package/dist/modules/scheduler/services/schedulerService.js.map +2 -2
  37. package/dist/modules/scheduler/workers/execute-schedule.worker.js +9 -0
  38. package/dist/modules/scheduler/workers/execute-schedule.worker.js.map +2 -2
  39. package/package.json +10 -9
  40. package/src/modules/scheduler/__integration__/TC-SCHED-007.spec.ts +6 -4
  41. package/src/modules/scheduler/api/jobs/[id]/executions/route.ts +4 -2
  42. package/src/modules/scheduler/api/queue-jobs/[jobId]/route.ts +4 -2
  43. package/src/modules/scheduler/api/targets/__tests__/route.test.ts +76 -0
  44. package/src/modules/scheduler/api/targets/route.ts +7 -7
  45. package/src/modules/scheduler/commands/__tests__/jobs.active-limit.test.ts +184 -0
  46. package/src/modules/scheduler/commands/jobs.ts +40 -15
  47. package/src/modules/scheduler/components/ExecutionDetailsDialog.tsx +1 -1
  48. package/src/modules/scheduler/components/JobLogsModal.tsx +1 -1
  49. package/src/modules/scheduler/data/__tests__/validators.test.ts +69 -9
  50. package/src/modules/scheduler/data/validators.ts +30 -1
  51. package/src/modules/scheduler/i18n/de.json +2 -0
  52. package/src/modules/scheduler/i18n/en.json +2 -0
  53. package/src/modules/scheduler/i18n/es.json +2 -0
  54. package/src/modules/scheduler/i18n/ko.json +157 -0
  55. package/src/modules/scheduler/i18n/pl.json +2 -0
  56. package/src/modules/scheduler/lib/__tests__/intervalParser.test.ts +27 -9
  57. package/src/modules/scheduler/lib/__tests__/nextRunCalculator.test.ts +18 -7
  58. package/src/modules/scheduler/lib/activeScheduleLimits.ts +48 -0
  59. package/src/modules/scheduler/lib/intervalParser.ts +8 -3
  60. package/src/modules/scheduler/lib/nextRunCalculator.ts +14 -1
  61. package/src/modules/scheduler/lib/scheduler-safe-commands.ts +98 -0
  62. package/src/modules/scheduler/services/__tests__/bullmqSchedulerService.test.ts +246 -149
  63. package/src/modules/scheduler/services/__tests__/localSchedulerService.test.ts +97 -1
  64. package/src/modules/scheduler/services/__tests__/schedulerService.test.ts +101 -0
  65. package/src/modules/scheduler/services/bullmqSchedulerService.ts +135 -49
  66. package/src/modules/scheduler/services/localSchedulerService.ts +11 -0
  67. package/src/modules/scheduler/services/schedulerService.ts +27 -12
  68. package/src/modules/scheduler/workers/__tests__/execute-schedule.worker.test.ts +12 -0
  69. package/src/modules/scheduler/workers/execute-schedule.worker.ts +15 -0
@@ -2,6 +2,10 @@ 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'
5
9
  import { createLogger } from '@open-mercato/shared/lib/logger'
6
10
 
7
11
  jest.mock('@open-mercato/shared/lib/logger', () => {
@@ -54,12 +58,19 @@ describe('LocalSchedulerService', () => {
54
58
  let mockForkedEm: jest.Mocked<EntityManager>
55
59
  let mockQueue: jest.Mocked<Queue>
56
60
  let mockQueueFactory: jest.Mock
57
- let mockRbacService: { tenantHasFeature: jest.Mock }
61
+ let mockRbacService: { tenantHasFeature: jest.Mock; userHasAllFeatures: jest.Mock }
58
62
  let mockLockStrategy: { runWithLock: jest.Mock }
59
63
 
60
64
  beforeEach(() => {
61
65
  jest.clearAllMocks()
62
66
  jest.useFakeTimers()
67
+ clearSchedulerSafeCommandsForTests()
68
+ registerSchedulerSafeCommands([
69
+ {
70
+ commandId: 'test:command',
71
+ requiredFeatures: ['scheduler.jobs.manage'],
72
+ },
73
+ ])
63
74
 
64
75
  // Create mock queue
65
76
  mockQueue = {
@@ -84,6 +95,7 @@ describe('LocalSchedulerService', () => {
84
95
  // Create mock RBAC service
85
96
  mockRbacService = {
86
97
  tenantHasFeature: jest.fn(),
98
+ userHasAllFeatures: jest.fn().mockResolvedValue(true),
87
99
  }
88
100
 
89
101
  // Create service
@@ -293,6 +305,7 @@ describe('LocalSchedulerService', () => {
293
305
  targetType: 'command',
294
306
  targetCommand: 'test:command',
295
307
  targetQueue: null,
308
+ createdByUserId: 'user-1',
296
309
  })
297
310
 
298
311
  mockForkedEm.find.mockResolvedValue([schedule])
@@ -312,8 +325,85 @@ describe('LocalSchedulerService', () => {
312
325
  tenantId: null,
313
326
  organizationId: null,
314
327
  }),
328
+ ctx: expect.objectContaining({
329
+ auth: expect.objectContaining({
330
+ sub: 'user-1',
331
+ userId: 'user-1',
332
+ tenantId: null,
333
+ orgId: null,
334
+ }),
335
+ }),
336
+ })
337
+ )
338
+ expect(mockRbacService.userHasAllFeatures).toHaveBeenCalledWith(
339
+ 'user-1',
340
+ ['scheduler.jobs.manage'],
341
+ { tenantId: null, organizationId: null }
342
+ )
343
+ })
344
+
345
+ it('should reject command target that is not scheduler safe', async () => {
346
+ const schedule = createSchedule({
347
+ targetType: 'command',
348
+ targetCommand: 'unsafe.command',
349
+ targetQueue: null,
350
+ createdByUserId: 'user-1',
351
+ })
352
+
353
+ mockForkedEm.find.mockResolvedValue([schedule])
354
+ mockForkedEm.findOne.mockResolvedValue({ ...schedule })
355
+ mockLockStrategy.runWithLock.mockImplementation(async (_key: string, fn: () => Promise<unknown>) => {
356
+ await fn()
357
+ return { acquired: true }
358
+ })
359
+ const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation()
360
+
361
+ await service.start()
362
+
363
+ expect(mockCommandBusInstance.execute).not.toHaveBeenCalled()
364
+ expect(mockEmitSchedulerEvent).toHaveBeenCalledWith(
365
+ 'scheduler.job.failed',
366
+ expect.objectContaining({
367
+ id: 'test-1',
368
+ error: 'Scheduled command is not allowed',
369
+ })
370
+ )
371
+ consoleErrorSpy.mockRestore()
372
+ })
373
+
374
+ it('should reject command target when the schedule creator lacks required features', async () => {
375
+ const schedule = createSchedule({
376
+ targetType: 'command',
377
+ targetCommand: 'test:command',
378
+ targetQueue: null,
379
+ createdByUserId: 'user-1',
380
+ })
381
+
382
+ mockRbacService.userHasAllFeatures.mockResolvedValueOnce(false)
383
+ mockForkedEm.find.mockResolvedValue([schedule])
384
+ mockForkedEm.findOne.mockResolvedValue({ ...schedule })
385
+ mockLockStrategy.runWithLock.mockImplementation(async (_key: string, fn: () => Promise<unknown>) => {
386
+ await fn()
387
+ return { acquired: true }
388
+ })
389
+ const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation()
390
+
391
+ await service.start()
392
+
393
+ expect(mockCommandBusInstance.execute).not.toHaveBeenCalled()
394
+ expect(mockRbacService.userHasAllFeatures).toHaveBeenCalledWith(
395
+ 'user-1',
396
+ ['scheduler.jobs.manage'],
397
+ { tenantId: null, organizationId: null }
398
+ )
399
+ expect(mockEmitSchedulerEvent).toHaveBeenCalledWith(
400
+ 'scheduler.job.failed',
401
+ expect.objectContaining({
402
+ id: 'test-1',
403
+ error: 'Scheduled command creator is not authorized',
315
404
  })
316
405
  )
406
+ consoleErrorSpy.mockRestore()
317
407
  })
318
408
 
319
409
  it('should emit started event', async () => {
@@ -597,6 +687,7 @@ describe('LocalSchedulerService', () => {
597
687
  scopeType: 'organization',
598
688
  tenantId: 'tenant-1',
599
689
  organizationId: 'org-1',
690
+ createdByUserId: 'user-1',
600
691
  })
601
692
 
602
693
  mockForkedEm.find.mockResolvedValue([schedule])
@@ -618,6 +709,11 @@ describe('LocalSchedulerService', () => {
618
709
  organizationId: 'org-1',
619
710
  }),
620
711
  ctx: expect.objectContaining({
712
+ auth: expect.objectContaining({
713
+ sub: 'user-1',
714
+ tenantId: 'tenant-1',
715
+ orgId: 'org-1',
716
+ }),
621
717
  selectedOrganizationId: 'org-1',
622
718
  organizationIds: ['org-1'],
623
719
  }),
@@ -4,6 +4,14 @@ 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
+
7
15
  jest.mock('@open-mercato/shared/lib/logger', () => {
8
16
  const mocked = {
9
17
  debug: jest.fn(),
@@ -184,6 +192,37 @@ describe('SchedulerService', () => {
184
192
  timezone: 'America/New_York',
185
193
  }))
186
194
  })
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
+ })
187
226
  })
188
227
 
189
228
  describe('unregister', () => {
@@ -380,6 +419,68 @@ describe('SchedulerService', () => {
380
419
 
381
420
  expect(mockBullMQService.unregister).toHaveBeenCalledWith('test-1')
382
421
  })
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
+ })
383
484
  })
384
485
 
385
486
  describe('findByModule', () => {
@@ -2,12 +2,18 @@ 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 { parseInterval } from '../lib/intervalParser'
6
- import { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'
5
+ import { resolveScheduleIntervalMs } from '../lib/intervalParser'
6
+ import { getRedisUrlOrThrow, parseRedisUrl } 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
+
11
17
  interface BullRepeatableJob {
12
18
  key: string
13
19
  name: string
@@ -21,20 +27,32 @@ interface BullRepeatOptions {
21
27
  }
22
28
 
23
29
  interface BullQueue {
24
- add(name: string, data: unknown, opts?: unknown): Promise<unknown>
30
+ add(name: string, data: unknown, opts?: Record<string, unknown>): Promise<unknown>
25
31
  getRepeatableJobs?(): Promise<BullRepeatableJob[]>
26
32
  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>
27
40
  close(): Promise<void>
28
41
  }
29
42
 
43
+ type BullJobSchedulerQueue = BullQueue & Required<Pick<
44
+ BullQueue,
45
+ 'upsertJobScheduler' | 'getJobSchedulers' | 'removeJobScheduler'
46
+ >>
47
+
30
48
  /**
31
- * Production scheduler using BullMQ repeatable jobs.
49
+ * Production scheduler using BullMQ Job Schedulers.
32
50
  *
33
51
  * Requires Redis. Set QUEUE_STRATEGY=async to use this service.
34
52
  *
35
- * This service syncs database schedules with BullMQ's repeat mechanism.
53
+ * This service syncs database schedules with BullMQ's Job Scheduler mechanism.
36
54
  * When a schedule is created/updated/deleted in the database, this service
37
- * adds/updates/removes the corresponding BullMQ repeatable job.
55
+ * adds/updates/removes the corresponding BullMQ Job Scheduler.
38
56
  *
39
57
  * BullMQ handles:
40
58
  * - Exact timing based on cron expressions or intervals
@@ -58,7 +76,9 @@ export class BullMQSchedulerService {
58
76
  if (!this.queue) {
59
77
  try {
60
78
  const { Queue } = await import('bullmq')
61
- this.queue = new Queue('scheduler-execution', { connection: { url: getRedisUrlOrThrow('QUEUE') } })
79
+ this.queue = new Queue('scheduler-execution', {
80
+ connection: parseRedisUrl(getRedisUrlOrThrow('QUEUE')),
81
+ })
62
82
  } catch {
63
83
  throw new Error('BullMQ is required for async scheduler. Install it with: npm install bullmq')
64
84
  }
@@ -70,22 +90,33 @@ export class BullMQSchedulerService {
70
90
  return `schedule-${scheduleId}`
71
91
  }
72
92
 
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
93
+ private getScheduleIdFromJobScheduler(job: BullJobScheduler): string | null {
94
+ const candidate = [job.key, job.id, job.name]
95
+ .find((value) => value?.startsWith('schedule-'))
79
96
 
80
97
  return candidate ? candidate.slice('schedule-'.length) : null
81
98
  }
82
99
 
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
+
83
114
  private isRepeatableJobForSchedule(job: BullRepeatableJob, scheduleId: string): boolean {
84
115
  const jobName = this.getScheduleJobName(scheduleId)
85
116
  return job.id === jobName || job.name === jobName
86
117
  }
87
118
 
88
- private async removeRepeatableJobsForSchedule(queue: BullQueue, scheduleId: string): Promise<number> {
119
+ private async removeLegacyRepeatableJobs(queue: BullQueue, scheduleId: string): Promise<number> {
89
120
  const repeatableJobs = await queue.getRepeatableJobs?.()
90
121
  if (!repeatableJobs || typeof queue.removeRepeatableByKey !== 'function') {
91
122
  return 0
@@ -97,12 +128,11 @@ export class BullMQSchedulerService {
97
128
  await queue.removeRepeatableByKey(job.key)
98
129
  removed += 1
99
130
  }
100
-
101
131
  return removed
102
132
  }
103
133
 
104
134
  /**
105
- * Register a schedule with BullMQ repeatable jobs
135
+ * Register a schedule with BullMQ Job Schedulers
106
136
  * @param schedule - The schedule to register
107
137
  * @param options - Optional configuration
108
138
  * @param options.skipNextRunUpdate - If true, skip updating nextRunAt (used when called from hooks)
@@ -131,15 +161,14 @@ export class BullMQSchedulerService {
131
161
  // Build BullMQ repeat options based on schedule type
132
162
  const repeatOpts = this.buildRepeatOptions(schedule)
133
163
 
134
- // Add repeatable job to BullMQ
164
+ // Upsert the BullMQ Job Scheduler
135
165
  // IMPORTANT: Wrap in QueuedJob format to match queue strategy expectations
136
166
  // BullMQ will store this in job.data, and the async strategy worker expects
137
167
  // job.data to be a QueuedJob with id, payload, and createdAt
138
168
  const queue = await this.getQueue()
139
169
  const jobName = this.getScheduleJobName(schedule.id)
140
- await this.removeRepeatableJobsForSchedule(queue, schedule.id)
141
170
 
142
- // For repeatable jobs, we need to provide a stable ID in the data
171
+ // Job Scheduler instances use a stable ID in the data
143
172
  // that will be used for each repeat instance
144
173
  // CRITICAL: Include scope information (tenantId, organizationId, scopeType)
145
174
  // for proper multi-tenant isolation and auditing
@@ -154,7 +183,7 @@ export class BullMQSchedulerService {
154
183
  createdAt: new Date().toISOString(),
155
184
  }
156
185
 
157
- logger.debug('Adding repeatable job', {
186
+ logger.debug('Upserting job scheduler', {
158
187
  jobName,
159
188
  scheduleId: schedule.id,
160
189
  scopeType: schedule.scopeType,
@@ -163,22 +192,40 @@ export class BullMQSchedulerService {
163
192
  repeatOpts,
164
193
  })
165
194
 
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
195
+ const jobOptions = {
196
+ removeOnComplete: {
197
+ age: 86400 * 30,
198
+ count: 1000,
199
+ },
200
+ removeOnFail: {
201
+ age: 86400 * 90,
202
+ count: 5000,
203
+ },
204
+ }
205
+
206
+ if (this.supportsJobSchedulers(queue)) {
207
+ const existingJobSchedulers = await queue.getJobSchedulers()
208
+ await queue.upsertJobScheduler(
209
+ jobName,
210
+ repeatOpts,
211
+ {
212
+ name: jobName,
213
+ data: jobData,
214
+ opts: jobOptions,
179
215
  },
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
+ }
180
224
  }
181
- )
225
+ } else {
226
+ await this.removeLegacyRepeatableJobs(queue, schedule.id)
227
+ await queue.add(jobName, jobData, { repeat: repeatOpts, ...jobOptions })
228
+ }
182
229
 
183
230
  logger.debug('Registered schedule', {
184
231
  scheduleId: schedule.id,
@@ -194,13 +241,28 @@ export class BullMQSchedulerService {
194
241
  }
195
242
 
196
243
  /**
197
- * Unregister a schedule from BullMQ repeatable jobs
244
+ * Unregister a schedule from BullMQ Job Schedulers
198
245
  */
199
246
  async unregister(scheduleId: string): Promise<void> {
200
247
  try {
201
248
  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
+ }
202
264
 
203
- const removed = await this.removeRepeatableJobsForSchedule(queue, scheduleId)
265
+ const removed = await this.removeLegacyRepeatableJobs(queue, scheduleId)
204
266
  if (removed > 0) {
205
267
  logger.debug('Unregistered schedule', { scheduleId })
206
268
  } else {
@@ -222,15 +284,33 @@ export class BullMQSchedulerService {
222
284
 
223
285
  logger.debug('Starting full sync')
224
286
 
225
- // Get all BullMQ repeatable jobs
226
- const repeatableJobs = await queue.getRepeatableJobs?.() || []
227
287
  const bullmqScheduleIds = new Set<string>()
228
288
  const bullmqScheduleCounts = new Map<string, number>()
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)
289
+ const canonicalJobSchedulerIds = new Set<string>()
290
+ if (this.supportsJobSchedulers(queue)) {
291
+ const jobSchedulers = await queue.getJobSchedulers()
292
+ for (const job of jobSchedulers) {
293
+ const scheduleId = this.getScheduleIdFromJobScheduler(job)
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
+ }
234
314
  }
235
315
 
236
316
  // Get enabled schedules from database in batches to avoid unbounded loads
@@ -254,8 +334,11 @@ export class BullMQSchedulerService {
254
334
  if (!bullmqScheduleIds.has(schedule.id)) {
255
335
  logger.debug('Registering missing schedule', { scheduleId: schedule.id, scheduleName: schedule.name })
256
336
  await this.register(schedule)
257
- } else if ((bullmqScheduleCounts.get(schedule.id) ?? 0) > 1) {
258
- logger.info('Repairing duplicate repeatable jobs', { scheduleId: schedule.id })
337
+ } else if (
338
+ (bullmqScheduleCounts.get(schedule.id) ?? 0) > 1
339
+ || !canonicalJobSchedulerIds.has(schedule.id)
340
+ ) {
341
+ logger.info('Migrating legacy or duplicate job scheduler', { scheduleId: schedule.id })
259
342
  await this.register(schedule)
260
343
  }
261
344
  }
@@ -288,7 +371,7 @@ export class BullMQSchedulerService {
288
371
  opts.pattern = schedule.scheduleValue
289
372
  } else if (schedule.scheduleType === 'interval') {
290
373
  // Parse interval (e.g., "15m", "2h", "1d")
291
- const intervalMs = parseInterval(schedule.scheduleValue)
374
+ const intervalMs = resolveScheduleIntervalMs(schedule.scheduleValue)
292
375
  opts.every = intervalMs
293
376
  } else {
294
377
  throw new Error(`Unsupported schedule type: ${schedule.scheduleType}`)
@@ -298,14 +381,17 @@ export class BullMQSchedulerService {
298
381
  }
299
382
 
300
383
  /**
301
- * Get list of all repeatable jobs from BullMQ
384
+ * Get list of all scheduled jobs from BullMQ
302
385
  */
303
386
  async getRepeatableJobs(): Promise<unknown[]> {
304
387
  try {
305
388
  const queue = await this.getQueue()
306
- return await queue.getRepeatableJobs?.() || []
389
+ if (this.supportsJobSchedulers(queue)) {
390
+ return await queue.getJobSchedulers()
391
+ }
392
+ return await queue.getRepeatableJobs?.() ?? []
307
393
  } catch (error) {
308
- logger.error('Failed to get repeatable jobs', { err: error })
394
+ logger.error('Failed to get job schedulers', { err: error })
309
395
  return []
310
396
  }
311
397
  }
@@ -7,6 +7,7 @@ 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'
10
11
  import { buildScheduledCommandContext } from '../lib/commandContext.js'
11
12
  import { buildQueueTargetPayload, buildSchedulerIdempotencyKey } from '../lib/queueTargetPayload.js'
12
13
  import { createLogger } from '@open-mercato/shared/lib/logger'
@@ -15,6 +16,7 @@ const logger = createLogger('scheduler').child({ component: 'local' })
15
16
 
16
17
  export interface RbacServiceLike {
17
18
  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>
18
20
  }
19
21
 
20
22
  export interface LocalSchedulerConfig {
@@ -284,6 +286,14 @@ export class LocalSchedulerService {
284
286
  }
285
287
 
286
288
  const commandBus = new CommandBus()
289
+ const actorUserId = typeof schedule.createdByUserId === 'string' ? schedule.createdByUserId.trim() : ''
290
+ await assertSchedulerSafeCommandAuthorized({
291
+ commandId: schedule.targetCommand,
292
+ actorUserId,
293
+ tenantId: schedule.tenantId,
294
+ organizationId: schedule.organizationId,
295
+ rbacService: this.rbacService,
296
+ })
287
297
 
288
298
  const commandInput = {
289
299
  ...((schedule.targetPayload as Record<string, unknown>) || {}),
@@ -291,6 +301,7 @@ export class LocalSchedulerService {
291
301
  organizationId: schedule.organizationId,
292
302
  }
293
303
 
304
+ // Build the schedule-scoped command context after the allowlist/RBAC gate.
294
305
  const commandCtx = buildScheduledCommandContext(
295
306
  schedule,
296
307
  {