@open-mercato/scheduler 0.6.7-develop.6860.1.f796b22e80 → 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
|
@@ -73,9 +73,9 @@ describe('nextRunCalculator', () => {
|
|
|
73
73
|
|
|
74
74
|
it('should handle seconds', () => {
|
|
75
75
|
const fromDate = new Date('2025-01-27T12:00:00Z')
|
|
76
|
-
const result = calculateNextRun('interval', '
|
|
76
|
+
const result = calculateNextRun('interval', '30s', 'UTC', fromDate)
|
|
77
77
|
|
|
78
|
-
expect(result).toEqual(new Date('2025-01-27T12:
|
|
78
|
+
expect(result).toEqual(new Date('2025-01-27T12:00:30Z'))
|
|
79
79
|
})
|
|
80
80
|
|
|
81
81
|
it('should handle minutes', () => {
|
|
@@ -223,10 +223,10 @@ describe('nextRunCalculator', () => {
|
|
|
223
223
|
it('should handle various intervals', () => {
|
|
224
224
|
const before = Date.now()
|
|
225
225
|
|
|
226
|
-
const
|
|
227
|
-
const
|
|
228
|
-
expect(
|
|
229
|
-
expect(
|
|
226
|
+
const result30s = recalculateNextRun('interval', '30s')
|
|
227
|
+
const diff30s = result30s!.getTime() - before
|
|
228
|
+
expect(diff30s).toBeGreaterThanOrEqual(30 * 1000 - 500)
|
|
229
|
+
expect(diff30s).toBeLessThanOrEqual(30 * 1000 + 500)
|
|
230
230
|
|
|
231
231
|
const result15m = recalculateNextRun('interval', '15m')
|
|
232
232
|
const diff15m = result15m!.getTime() - before
|
|
@@ -310,17 +310,6 @@ describe('nextRunCalculator', () => {
|
|
|
310
310
|
|
|
311
311
|
expect(result).toBeNull()
|
|
312
312
|
})
|
|
313
|
-
|
|
314
|
-
it('should clamp legacy interval values below one minute', () => {
|
|
315
|
-
const before = Date.now()
|
|
316
|
-
|
|
317
|
-
for (const scheduleValue of ['0s', '1s', '59s']) {
|
|
318
|
-
const result = recalculateNextRun('interval', scheduleValue)
|
|
319
|
-
const intervalMs = result!.getTime() - before
|
|
320
|
-
expect(intervalMs).toBeGreaterThanOrEqual(60 * 1000 - 500)
|
|
321
|
-
expect(intervalMs).toBeLessThanOrEqual(60 * 1000 + 500)
|
|
322
|
-
}
|
|
323
|
-
})
|
|
324
313
|
})
|
|
325
314
|
})
|
|
326
315
|
|
|
@@ -352,7 +341,7 @@ describe('nextRunCalculator', () => {
|
|
|
352
341
|
{ type: 'cron' as const, value: '0 0 * * *' }, // Daily at midnight
|
|
353
342
|
{ type: 'cron' as const, value: '*/5 * * * *' }, // Every 5 minutes
|
|
354
343
|
{ type: 'cron' as const, value: '0 9-17 * * 1-5' }, // Weekdays 9am-5pm
|
|
355
|
-
{ type: 'interval' as const, value: '
|
|
344
|
+
{ type: 'interval' as const, value: '30s' },
|
|
356
345
|
{ type: 'interval' as const, value: '15m' },
|
|
357
346
|
{ type: 'interval' as const, value: '2h' },
|
|
358
347
|
{ type: 'interval' as const, value: '1d' },
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Parse simple interval strings like '15m', '2h', '1d' into milliseconds
|
|
3
3
|
*/
|
|
4
|
-
export const MIN_SCHEDULE_INTERVAL_MS = 60 * 1000
|
|
5
|
-
|
|
6
4
|
export function parseInterval(interval: string): number {
|
|
7
5
|
const regex = /^(\d+)(s|m|h|d)$/
|
|
8
6
|
const match = interval.match(regex)
|
|
@@ -24,10 +22,6 @@ export function parseInterval(interval: string): number {
|
|
|
24
22
|
return value * multipliers[unit]
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
export function resolveScheduleIntervalMs(interval: string): number {
|
|
28
|
-
return Math.max(parseInterval(interval), MIN_SCHEDULE_INTERVAL_MS)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
25
|
/**
|
|
32
26
|
* Calculate next run time based on interval
|
|
33
27
|
*/
|
|
@@ -35,7 +29,7 @@ export function calculateNextRunFromInterval(
|
|
|
35
29
|
interval: string,
|
|
36
30
|
fromDate: Date = new Date()
|
|
37
31
|
): Date {
|
|
38
|
-
const ms =
|
|
32
|
+
const ms = parseInterval(interval)
|
|
39
33
|
return new Date(fromDate.getTime() + ms)
|
|
40
34
|
}
|
|
41
35
|
|
|
@@ -44,7 +38,8 @@ export function calculateNextRunFromInterval(
|
|
|
44
38
|
*/
|
|
45
39
|
export function validateInterval(interval: string): boolean {
|
|
46
40
|
try {
|
|
47
|
-
|
|
41
|
+
parseInterval(interval)
|
|
42
|
+
return true
|
|
48
43
|
} catch {
|
|
49
44
|
return false
|
|
50
45
|
}
|
|
@@ -4,44 +4,78 @@ import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
|
4
4
|
const logger = createLogger('scheduler').child({ component: 'local' })
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* Mutual-exclusion strategy for single-instance or local development.
|
|
8
|
+
*
|
|
9
|
+
* Exclusion for the duration of the guarded run is IN-PROCESS (`heldKeys`).
|
|
10
|
+
* The PostgreSQL advisory lock only serialises concurrent *claims* — it is
|
|
11
|
+
* transaction-scoped and released as soon as the short claim transaction
|
|
12
|
+
* commits, before the guarded function runs. It therefore does NOT protect
|
|
13
|
+
* against two processes executing the same key concurrently; that matches the
|
|
14
|
+
* documented single-instance contract of the local scheduler strategy, and
|
|
15
|
+
* `LocalSchedulerService.start()` warns about it at startup.
|
|
8
16
|
*/
|
|
9
17
|
export class LocalLockStrategy {
|
|
18
|
+
// Process-local by construction, so it only guards a single strategy
|
|
19
|
+
// instance. Invariant relied upon: exactly one LocalLockStrategy exists per
|
|
20
|
+
// process, because the only construction site is LocalSchedulerService
|
|
21
|
+
// (an Awilix singleton) and only the CLI entry points start the polling
|
|
22
|
+
// engine. A caller that resolves a second container would silently get an
|
|
23
|
+
// independent guard — see the single-instance warning in
|
|
24
|
+
// LocalSchedulerService.start().
|
|
25
|
+
private heldKeys = new Set<string>()
|
|
26
|
+
|
|
10
27
|
constructor(private em: () => EntityManager) {}
|
|
11
28
|
|
|
12
29
|
/**
|
|
13
|
-
* Execute a function under a
|
|
30
|
+
* Execute a function under a mutual-exclusion lock.
|
|
31
|
+
*
|
|
32
|
+
* In-process exclusion for the whole duration of `fn` comes from `heldKeys`
|
|
33
|
+
* (this strategy is single-instance by contract). The transaction-scoped
|
|
34
|
+
* advisory lock (`pg_try_advisory_xact_lock`) only guards the claim itself
|
|
35
|
+
* against a concurrently claiming process and is released when the short
|
|
36
|
+
* claim transaction commits, BEFORE `fn` runs.
|
|
14
37
|
*
|
|
15
|
-
* IMPORTANT:
|
|
16
|
-
*
|
|
17
|
-
*
|
|
38
|
+
* IMPORTANT: `fn` must NOT run inside that transaction. Holding the
|
|
39
|
+
* transaction open across `fn` leaves the connection idle-in-transaction
|
|
40
|
+
* while `fn` awaits non-DB work; the pool's default
|
|
41
|
+
* `idle_in_transaction_session_timeout` (120s) then makes Postgres kill the
|
|
42
|
+
* connection (FATAL 25P03), which crashes the scheduler process.
|
|
18
43
|
*/
|
|
19
44
|
async runWithLock<T>(key: string, fn: () => Promise<T>): Promise<{ acquired: boolean; result?: T }> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
45
|
+
if (this.heldKeys.has(key)) {
|
|
46
|
+
return { acquired: false }
|
|
47
|
+
}
|
|
48
|
+
// Reserve synchronously before the async claim so a concurrent in-process
|
|
49
|
+
// caller cannot slip in between our claim transaction committing (which
|
|
50
|
+
// releases the advisory xact lock) and the start of `fn`.
|
|
51
|
+
this.heldKeys.add(key)
|
|
23
52
|
|
|
24
53
|
try {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
throw error
|
|
54
|
+
const em = this.em().fork()
|
|
55
|
+
const hash = this.hashString(key)
|
|
56
|
+
let claimed = false
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
claimed = await em.transactional(async (txEm) => {
|
|
60
|
+
const result = await txEm.getConnection().execute<{ acquired: boolean }[]>(
|
|
61
|
+
`SELECT pg_try_advisory_xact_lock(?) as acquired`,
|
|
62
|
+
[hash],
|
|
63
|
+
)
|
|
64
|
+
return result[0]?.acquired === true
|
|
65
|
+
})
|
|
66
|
+
} catch (error) {
|
|
67
|
+
logger.error('Failed to acquire lock', { lockKey: key, err: error })
|
|
68
|
+
return { acquired: false }
|
|
41
69
|
}
|
|
42
70
|
|
|
43
|
-
|
|
44
|
-
|
|
71
|
+
if (!claimed) {
|
|
72
|
+
return { acquired: false }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const fnResult = await fn()
|
|
76
|
+
return { acquired: true, result: fnResult }
|
|
77
|
+
} finally {
|
|
78
|
+
this.heldKeys.delete(key)
|
|
45
79
|
}
|
|
46
80
|
}
|
|
47
81
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseCronExpression } from './cronParser.js'
|
|
2
|
-
import { calculateNextRunFromInterval
|
|
2
|
+
import { calculateNextRunFromInterval } from './intervalParser.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Calculate the next run time for a schedule
|
|
@@ -28,19 +28,6 @@ export function calculateNextRun(
|
|
|
28
28
|
return null
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export function calculateNextRunForWrite(
|
|
32
|
-
scheduleType: 'cron' | 'interval',
|
|
33
|
-
scheduleValue: string,
|
|
34
|
-
timezone: string = 'UTC',
|
|
35
|
-
fromDate?: Date,
|
|
36
|
-
): Date | null {
|
|
37
|
-
if (scheduleType === 'interval' && !validateInterval(scheduleValue)) {
|
|
38
|
-
return null
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return calculateNextRun(scheduleType, scheduleValue, timezone, fromDate)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
31
|
/**
|
|
45
32
|
* Recalculate next run for a schedule after execution
|
|
46
33
|
*/
|