@open-mercato/scheduler 0.6.7 → 0.6.8-develop.6874.1.982d6097d8
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 +4 -1
- package/dist/modules/scheduler/__integration__/TC-SCHED-007.spec.js.map +2 -2
- package/dist/modules/scheduler/api/jobs/[id]/executions/route.js +4 -2
- package/dist/modules/scheduler/api/jobs/[id]/executions/route.js.map +2 -2
- package/dist/modules/scheduler/api/queue-jobs/[jobId]/route.js +4 -2
- package/dist/modules/scheduler/api/queue-jobs/[jobId]/route.js.map +2 -2
- package/dist/modules/scheduler/api/targets/route.js +5 -4
- package/dist/modules/scheduler/api/targets/route.js.map +2 -2
- package/dist/modules/scheduler/commands/jobs.js +33 -14
- package/dist/modules/scheduler/commands/jobs.js.map +2 -2
- package/dist/modules/scheduler/components/ExecutionDetailsDialog.js +1 -1
- package/dist/modules/scheduler/components/ExecutionDetailsDialog.js.map +2 -2
- package/dist/modules/scheduler/components/JobLogsModal.js +1 -1
- package/dist/modules/scheduler/components/JobLogsModal.js.map +2 -2
- package/dist/modules/scheduler/data/validators.js +27 -1
- package/dist/modules/scheduler/data/validators.js.map +2 -2
- package/dist/modules/scheduler/i18n/de.json +2 -0
- package/dist/modules/scheduler/i18n/en.json +2 -0
- package/dist/modules/scheduler/i18n/es.json +2 -0
- package/dist/modules/scheduler/i18n/ko.json +157 -0
- package/dist/modules/scheduler/i18n/pl.json +2 -0
- package/dist/modules/scheduler/lib/activeScheduleLimits.js +44 -0
- package/dist/modules/scheduler/lib/activeScheduleLimits.js.map +7 -0
- package/dist/modules/scheduler/lib/intervalParser.js +8 -3
- package/dist/modules/scheduler/lib/intervalParser.js.map +2 -2
- package/dist/modules/scheduler/lib/nextRunCalculator.js +8 -1
- package/dist/modules/scheduler/lib/nextRunCalculator.js.map +2 -2
- package/dist/modules/scheduler/lib/scheduler-safe-commands.js +73 -0
- package/dist/modules/scheduler/lib/scheduler-safe-commands.js.map +7 -0
- package/dist/modules/scheduler/services/bullmqSchedulerService.js +92 -43
- package/dist/modules/scheduler/services/bullmqSchedulerService.js.map +2 -2
- package/dist/modules/scheduler/services/localSchedulerService.js +9 -0
- package/dist/modules/scheduler/services/localSchedulerService.js.map +2 -2
- package/dist/modules/scheduler/services/schedulerService.js +23 -11
- package/dist/modules/scheduler/services/schedulerService.js.map +2 -2
- package/dist/modules/scheduler/workers/execute-schedule.worker.js +9 -0
- package/dist/modules/scheduler/workers/execute-schedule.worker.js.map +2 -2
- package/package.json +10 -9
- package/src/modules/scheduler/__integration__/TC-SCHED-007.spec.ts +6 -4
- package/src/modules/scheduler/api/jobs/[id]/executions/route.ts +4 -2
- package/src/modules/scheduler/api/queue-jobs/[jobId]/route.ts +4 -2
- package/src/modules/scheduler/api/targets/__tests__/route.test.ts +76 -0
- package/src/modules/scheduler/api/targets/route.ts +7 -7
- package/src/modules/scheduler/commands/__tests__/jobs.active-limit.test.ts +184 -0
- package/src/modules/scheduler/commands/jobs.ts +40 -15
- package/src/modules/scheduler/components/ExecutionDetailsDialog.tsx +1 -1
- package/src/modules/scheduler/components/JobLogsModal.tsx +1 -1
- package/src/modules/scheduler/data/__tests__/validators.test.ts +69 -9
- package/src/modules/scheduler/data/validators.ts +30 -1
- package/src/modules/scheduler/i18n/de.json +2 -0
- package/src/modules/scheduler/i18n/en.json +2 -0
- package/src/modules/scheduler/i18n/es.json +2 -0
- package/src/modules/scheduler/i18n/ko.json +157 -0
- package/src/modules/scheduler/i18n/pl.json +2 -0
- package/src/modules/scheduler/lib/__tests__/intervalParser.test.ts +27 -9
- package/src/modules/scheduler/lib/__tests__/nextRunCalculator.test.ts +18 -7
- package/src/modules/scheduler/lib/activeScheduleLimits.ts +48 -0
- package/src/modules/scheduler/lib/intervalParser.ts +8 -3
- package/src/modules/scheduler/lib/nextRunCalculator.ts +14 -1
- package/src/modules/scheduler/lib/scheduler-safe-commands.ts +98 -0
- package/src/modules/scheduler/services/__tests__/bullmqSchedulerService.test.ts +246 -149
- package/src/modules/scheduler/services/__tests__/localSchedulerService.test.ts +97 -1
- package/src/modules/scheduler/services/__tests__/schedulerService.test.ts +101 -0
- package/src/modules/scheduler/services/bullmqSchedulerService.ts +135 -49
- package/src/modules/scheduler/services/localSchedulerService.ts +11 -0
- package/src/modules/scheduler/services/schedulerService.ts +27 -12
- package/src/modules/scheduler/workers/__tests__/execute-schedule.worker.test.ts +12 -0
- package/src/modules/scheduler/workers/execute-schedule.worker.ts +15 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
{
|
|
2
|
+
"scheduler.action.create": "새 일정",
|
|
3
|
+
"scheduler.action.delete": "삭제",
|
|
4
|
+
"scheduler.action.edit": "편집",
|
|
5
|
+
"scheduler.action.refresh": "새로고침",
|
|
6
|
+
"scheduler.action.trigger": "지금 실행",
|
|
7
|
+
"scheduler.action.view": "보기",
|
|
8
|
+
"scheduler.audit.create": "일정 생성",
|
|
9
|
+
"scheduler.audit.delete": "일정 삭제",
|
|
10
|
+
"scheduler.audit.update": "일정 수정",
|
|
11
|
+
"scheduler.confirm.delete": "이 일정을 삭제하시겠습니까?",
|
|
12
|
+
"scheduler.confirm.trigger": "지금 \"{name}\"을(를) 실행하시겠습니까?\n\n{targetType}이(가) 즉시 실행됩니다.",
|
|
13
|
+
"scheduler.create.title": "일정 생성",
|
|
14
|
+
"scheduler.details.configuration": "구성",
|
|
15
|
+
"scheduler.details.error": "오류",
|
|
16
|
+
"scheduler.details.executions_async_only": "실행 기록 및 수동 실행은 비동기 큐 전략(QUEUE_STRATEGY=async)이 필요합니다.",
|
|
17
|
+
"scheduler.details.last_run": "마지막 실행",
|
|
18
|
+
"scheduler.details.no_executions": "아직 실행 기록이 없습니다",
|
|
19
|
+
"scheduler.details.not_found": "찾을 수 없음",
|
|
20
|
+
"scheduler.details.payload": "페이로드",
|
|
21
|
+
"scheduler.details.recent_executions": "최근 실행",
|
|
22
|
+
"scheduler.details.required_feature": "필요한 기능",
|
|
23
|
+
"scheduler.details.scope": "범위",
|
|
24
|
+
"scheduler.details.source_module": "모듈",
|
|
25
|
+
"scheduler.details.source_user": "사용자 생성",
|
|
26
|
+
"scheduler.details.triggering": "실행 중...",
|
|
27
|
+
"scheduler.edit.title": "일정 편집",
|
|
28
|
+
"scheduler.error.access_denied": "접근이 거부되었습니다",
|
|
29
|
+
"scheduler.error.active_schedule_limit": "이 테넌트({max})에 대한 활성 예약 작업 제한에 도달했습니다.",
|
|
30
|
+
"scheduler.error.async_strategy_hint": "실행 기록을 보려면 QUEUE_STRATEGY=async를 설정하세요",
|
|
31
|
+
"scheduler.error.async_strategy_required": "실행 기록에는 QUEUE_STRATEGY=async가 필요합니다",
|
|
32
|
+
"scheduler.error.bullmq_required": "BullMQ 작업 로그는 QUEUE_STRATEGY=async에서만 사용할 수 있습니다",
|
|
33
|
+
"scheduler.error.delete_failed": "일정 삭제에 실패했습니다",
|
|
34
|
+
"scheduler.error.fetch_executions_failed": "실행 기록을 가져오지 못했습니다",
|
|
35
|
+
"scheduler.error.fetch_failed": "일정을 불러오지 못했습니다",
|
|
36
|
+
"scheduler.error.fetch_job_failed": "작업 상세 정보를 가져오지 못했습니다",
|
|
37
|
+
"scheduler.error.forbidden": "접근이 거부되었습니다",
|
|
38
|
+
"scheduler.error.invalidPayload": "잘못된 JSON 페이로드입니다",
|
|
39
|
+
"scheduler.error.invalid_queue_name": "잘못된 큐 이름입니다",
|
|
40
|
+
"scheduler.error.invalid_schedule_value": "일정 값이 잘못되었습니다.",
|
|
41
|
+
"scheduler.error.job_not_found": "BullMQ에서 작업을 찾을 수 없습니다 (제거되었을 수 있음)",
|
|
42
|
+
"scheduler.error.load_failed": "일정을 불러오지 못했습니다",
|
|
43
|
+
"scheduler.error.not_found": "일정을 찾을 수 없습니다",
|
|
44
|
+
"scheduler.error.queue_param_required": "queue 매개변수가 필요합니다",
|
|
45
|
+
"scheduler.error.trigger_async_hint": "실행 기록 및 수동 실행은 BullMQ(비동기 전략)에서만 사용할 수 있습니다",
|
|
46
|
+
"scheduler.error.trigger_async_required": "수동 실행에는 QUEUE_STRATEGY=async가 필요합니다",
|
|
47
|
+
"scheduler.error.trigger_failed": "일정 실행에 실패했습니다",
|
|
48
|
+
"scheduler.error.unauthorized": "권한이 없습니다",
|
|
49
|
+
"scheduler.error.update_failed": "일정 수정에 실패했습니다",
|
|
50
|
+
"scheduler.errors.id_required": "일정 식별자는 필수입니다.",
|
|
51
|
+
"scheduler.execution.actions": "작업",
|
|
52
|
+
"scheduler.execution.completed_message": "이 실행은 오류 없이 성공적으로 완료되었습니다.",
|
|
53
|
+
"scheduler.execution.details_title": "실행 상세 정보",
|
|
54
|
+
"scheduler.execution.duration": "소요 시간",
|
|
55
|
+
"scheduler.execution.error_message": "오류 메시지",
|
|
56
|
+
"scheduler.execution.finished_at": "완료 시각",
|
|
57
|
+
"scheduler.execution.na": "해당 없음",
|
|
58
|
+
"scheduler.execution.queue_job_id": "큐 작업 ID",
|
|
59
|
+
"scheduler.execution.queue_name": "큐 이름",
|
|
60
|
+
"scheduler.execution.run_id": "실행 ID",
|
|
61
|
+
"scheduler.execution.running_message": "이 실행은 현재 진행 중입니다.",
|
|
62
|
+
"scheduler.execution.skipped_message": "이 실행은 건너뛰어졌습니다.",
|
|
63
|
+
"scheduler.execution.stack_trace": "스택 추적",
|
|
64
|
+
"scheduler.execution.started": "시작됨",
|
|
65
|
+
"scheduler.execution.started_at": "시작 시각",
|
|
66
|
+
"scheduler.execution.status": "상태",
|
|
67
|
+
"scheduler.execution.trigger": "트리거",
|
|
68
|
+
"scheduler.execution.trigger_type": "트리거 유형",
|
|
69
|
+
"scheduler.execution.view_details": "상세 정보 보기",
|
|
70
|
+
"scheduler.execution.view_logs": "로그 보기",
|
|
71
|
+
"scheduler.execution_status.completed": "완료됨",
|
|
72
|
+
"scheduler.execution_status.failed": "실패",
|
|
73
|
+
"scheduler.execution_status.running": "실행 중",
|
|
74
|
+
"scheduler.execution_status.skipped": "건너뜀",
|
|
75
|
+
"scheduler.execution_status.waiting": "대기 중",
|
|
76
|
+
"scheduler.field.active": "활성",
|
|
77
|
+
"scheduler.field.description": "설명",
|
|
78
|
+
"scheduler.field.name": "이름",
|
|
79
|
+
"scheduler.field.next_run": "다음 실행",
|
|
80
|
+
"scheduler.field.schedule": "일정",
|
|
81
|
+
"scheduler.field.schedule_type": "유형",
|
|
82
|
+
"scheduler.field.schedule_value": "일정 값",
|
|
83
|
+
"scheduler.field.source": "소스",
|
|
84
|
+
"scheduler.field.status": "상태",
|
|
85
|
+
"scheduler.field.target": "대상",
|
|
86
|
+
"scheduler.field.timezone": "표준 시간대",
|
|
87
|
+
"scheduler.form.description": "설명",
|
|
88
|
+
"scheduler.form.disabled": "비활성화됨",
|
|
89
|
+
"scheduler.form.enabled": "활성화됨",
|
|
90
|
+
"scheduler.form.group.basic": "기본 정보",
|
|
91
|
+
"scheduler.form.group.schedule": "일정 구성",
|
|
92
|
+
"scheduler.form.group.target": "대상 구성",
|
|
93
|
+
"scheduler.form.is_enabled": "활성화됨",
|
|
94
|
+
"scheduler.form.name": "이름",
|
|
95
|
+
"scheduler.form.name.required": "이름은 필수입니다",
|
|
96
|
+
"scheduler.form.save": "변경 사항 저장",
|
|
97
|
+
"scheduler.form.schedule": "일정",
|
|
98
|
+
"scheduler.form.schedule.required": "일정은 필수입니다",
|
|
99
|
+
"scheduler.form.schedule_type": "일정 유형",
|
|
100
|
+
"scheduler.form.schedule_value": "일정 값",
|
|
101
|
+
"scheduler.form.schedule_value.description": "cron: cron 표현식을 사용하세요 (예: \"0 0 * * *\"). interval: \"15m\", \"2h\", \"1d\"와 같은 형식을 사용하세요 (s=초, m=분, h=시간, d=일)",
|
|
102
|
+
"scheduler.form.schedule_value.placeholder": "예: 0 */6 * * * 또는 15m",
|
|
103
|
+
"scheduler.form.scope_type": "범위",
|
|
104
|
+
"scheduler.form.scope_type.locked_description": "범위는 일정이 생성될 때 설정되며 이후에는 변경할 수 없습니다.",
|
|
105
|
+
"scheduler.form.submit": "일정 생성",
|
|
106
|
+
"scheduler.form.target_command": "대상 명령",
|
|
107
|
+
"scheduler.form.target_command.placeholder": "명령 검색...",
|
|
108
|
+
"scheduler.form.target_payload": "작업 인수 (JSON)",
|
|
109
|
+
"scheduler.form.target_payload.description": "명령이나 큐 작업에 전달할 인수가 담긴 선택적 JSON 객체입니다. 유효한 JSON 형식이어야 합니다.",
|
|
110
|
+
"scheduler.form.target_payload.invalid_json": "유효한 JSON이어야 합니다",
|
|
111
|
+
"scheduler.form.target_payload.placeholder": "{\n \"message\": \"Hello\",\n \"value\": 42\n}",
|
|
112
|
+
"scheduler.form.target_queue": "대상 큐",
|
|
113
|
+
"scheduler.form.target_queue.placeholder": "큐 검색...",
|
|
114
|
+
"scheduler.form.target_type": "대상 유형",
|
|
115
|
+
"scheduler.form.timezone": "표준 시간대",
|
|
116
|
+
"scheduler.form.timezone.placeholder": "표준 시간대 검색...",
|
|
117
|
+
"scheduler.job_logs.attempts": "시도 횟수",
|
|
118
|
+
"scheduler.job_logs.duration": "소요 시간",
|
|
119
|
+
"scheduler.job_logs.error_message": "오류 메시지",
|
|
120
|
+
"scheduler.job_logs.finished": "완료됨",
|
|
121
|
+
"scheduler.job_logs.job_id": "작업 ID",
|
|
122
|
+
"scheduler.job_logs.load_failed": "작업 상세 정보를 불러오지 못했습니다",
|
|
123
|
+
"scheduler.job_logs.no_logs": "사용 가능한 로그가 없습니다",
|
|
124
|
+
"scheduler.job_logs.processed": "처리됨",
|
|
125
|
+
"scheduler.job_logs.progress": "진행 상황",
|
|
126
|
+
"scheduler.job_logs.return_value": "반환 값",
|
|
127
|
+
"scheduler.job_logs.stack_trace": "스택 추적",
|
|
128
|
+
"scheduler.job_logs.state": "상태",
|
|
129
|
+
"scheduler.job_logs.tab_error": "오류 상세 정보",
|
|
130
|
+
"scheduler.job_logs.tab_logs": "로그",
|
|
131
|
+
"scheduler.job_logs.tab_overview": "개요",
|
|
132
|
+
"scheduler.job_logs.tab_payload": "페이로드",
|
|
133
|
+
"scheduler.job_logs.title": "큐 작업",
|
|
134
|
+
"scheduler.loading": "일정을 불러오는 중...",
|
|
135
|
+
"scheduler.module.description": "cron 표현식이나 간단한 간격으로 자동화된 예약 작업을 관리합니다.",
|
|
136
|
+
"scheduler.module.title": "예약된 작업",
|
|
137
|
+
"scheduler.nav.backToList": "예약된 작업으로 돌아가기",
|
|
138
|
+
"scheduler.scope.organization": "조직",
|
|
139
|
+
"scheduler.scope.system": "시스템",
|
|
140
|
+
"scheduler.scope.tenant": "테넌트",
|
|
141
|
+
"scheduler.search.placeholder": "일정 검색...",
|
|
142
|
+
"scheduler.status.disabled": "비활성화됨",
|
|
143
|
+
"scheduler.status.enabled": "활성화됨",
|
|
144
|
+
"scheduler.success.created": "일정이 성공적으로 생성되었습니다",
|
|
145
|
+
"scheduler.success.deleted": "일정이 성공적으로 삭제되었습니다",
|
|
146
|
+
"scheduler.success.disabled": "일정이 비활성화되었습니다",
|
|
147
|
+
"scheduler.success.enabled": "일정이 활성화되었습니다",
|
|
148
|
+
"scheduler.success.triggered": "일정이 성공적으로 실행되었습니다",
|
|
149
|
+
"scheduler.success.updated": "일정이 성공적으로 수정되었습니다",
|
|
150
|
+
"scheduler.target.command": "명령",
|
|
151
|
+
"scheduler.target.queue": "큐",
|
|
152
|
+
"scheduler.title": "예약된 작업",
|
|
153
|
+
"scheduler.trigger_type.manual": "수동",
|
|
154
|
+
"scheduler.trigger_type.scheduled": "예약됨",
|
|
155
|
+
"scheduler.type.cron": "Cron 표현식",
|
|
156
|
+
"scheduler.type.interval": "간단한 간격"
|
|
157
|
+
}
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"scheduler.details.triggering": "Uruchamianie...",
|
|
27
27
|
"scheduler.edit.title": "Edytuj harmonogram",
|
|
28
28
|
"scheduler.error.access_denied": "Odmowa dostępu",
|
|
29
|
+
"scheduler.error.active_schedule_limit": "Osiągnięto limit aktywnych zaplanowanych zadań dla tego najemcy ({max}).",
|
|
29
30
|
"scheduler.error.async_strategy_hint": "Ustaw QUEUE_STRATEGY=async, aby wyświetlić historię wykonań",
|
|
30
31
|
"scheduler.error.async_strategy_required": "Historia wykonań wymaga QUEUE_STRATEGY=async",
|
|
31
32
|
"scheduler.error.bullmq_required": "Logi zadań BullMQ są dostępne tylko z QUEUE_STRATEGY=async",
|
|
@@ -36,6 +37,7 @@
|
|
|
36
37
|
"scheduler.error.forbidden": "Zabronione",
|
|
37
38
|
"scheduler.error.invalidPayload": "Nieprawidłowy ładunek JSON",
|
|
38
39
|
"scheduler.error.invalid_queue_name": "Nieprawidłowa nazwa kolejki",
|
|
40
|
+
"scheduler.error.invalid_schedule_value": "Nieprawidłowa wartość harmonogramu.",
|
|
39
41
|
"scheduler.error.job_not_found": "Zadanie nie zostało znalezione w BullMQ (mogło zostać usunięte)",
|
|
40
42
|
"scheduler.error.load_failed": "Nie udało się załadować harmonogramu",
|
|
41
43
|
"scheduler.error.not_found": "Harmonogram nie znaleziony",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect } from '@jest/globals'
|
|
2
2
|
import {
|
|
3
3
|
parseInterval,
|
|
4
|
+
resolveScheduleIntervalMs,
|
|
4
5
|
calculateNextRunFromInterval,
|
|
5
6
|
validateInterval,
|
|
6
7
|
intervalToHuman,
|
|
@@ -10,9 +11,8 @@ describe('intervalParser', () => {
|
|
|
10
11
|
describe('parseInterval', () => {
|
|
11
12
|
describe('seconds', () => {
|
|
12
13
|
it('should parse seconds correctly', () => {
|
|
13
|
-
expect(parseInterval('30s')).toBe(30 * 1000)
|
|
14
|
-
expect(parseInterval('1s')).toBe(1000)
|
|
15
14
|
expect(parseInterval('60s')).toBe(60 * 1000)
|
|
15
|
+
expect(parseInterval('90s')).toBe(90 * 1000)
|
|
16
16
|
})
|
|
17
17
|
})
|
|
18
18
|
|
|
@@ -86,8 +86,10 @@ describe('intervalParser', () => {
|
|
|
86
86
|
})
|
|
87
87
|
|
|
88
88
|
describe('edge cases', () => {
|
|
89
|
-
it('should
|
|
89
|
+
it('should preserve parsing for legacy intervals below one minute', () => {
|
|
90
90
|
expect(parseInterval('0s')).toBe(0)
|
|
91
|
+
expect(parseInterval('1s')).toBe(1000)
|
|
92
|
+
expect(parseInterval('59s')).toBe(59 * 1000)
|
|
91
93
|
expect(parseInterval('0m')).toBe(0)
|
|
92
94
|
expect(parseInterval('0h')).toBe(0)
|
|
93
95
|
expect(parseInterval('0d')).toBe(0)
|
|
@@ -102,6 +104,16 @@ describe('intervalParser', () => {
|
|
|
102
104
|
})
|
|
103
105
|
})
|
|
104
106
|
|
|
107
|
+
describe('resolveScheduleIntervalMs', () => {
|
|
108
|
+
it('should clamp legacy sub-minute intervals to one minute', () => {
|
|
109
|
+
expect(resolveScheduleIntervalMs('0s')).toBe(60 * 1000)
|
|
110
|
+
expect(resolveScheduleIntervalMs('10s')).toBe(60 * 1000)
|
|
111
|
+
expect(resolveScheduleIntervalMs('59s')).toBe(60 * 1000)
|
|
112
|
+
expect(resolveScheduleIntervalMs('60s')).toBe(60 * 1000)
|
|
113
|
+
expect(resolveScheduleIntervalMs('15m')).toBe(15 * 60 * 1000)
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
105
117
|
describe('calculateNextRunFromInterval', () => {
|
|
106
118
|
it('should calculate next run from current time', () => {
|
|
107
119
|
const before = new Date()
|
|
@@ -122,9 +134,9 @@ describe('intervalParser', () => {
|
|
|
122
134
|
|
|
123
135
|
it('should handle seconds intervals', () => {
|
|
124
136
|
const baseDate = new Date('2025-01-27T12:00:00Z')
|
|
125
|
-
const nextRun = calculateNextRunFromInterval('
|
|
137
|
+
const nextRun = calculateNextRunFromInterval('60s', baseDate)
|
|
126
138
|
|
|
127
|
-
expect(nextRun).toEqual(new Date('2025-01-27T12:
|
|
139
|
+
expect(nextRun).toEqual(new Date('2025-01-27T12:01:00Z'))
|
|
128
140
|
})
|
|
129
141
|
|
|
130
142
|
it('should handle minute intervals', () => {
|
|
@@ -179,15 +191,19 @@ describe('intervalParser', () => {
|
|
|
179
191
|
|
|
180
192
|
describe('validateInterval', () => {
|
|
181
193
|
it('should return true for valid intervals', () => {
|
|
182
|
-
expect(validateInterval('
|
|
194
|
+
expect(validateInterval('60s')).toBe(true)
|
|
195
|
+
expect(validateInterval('1m')).toBe(true)
|
|
183
196
|
expect(validateInterval('15m')).toBe(true)
|
|
184
197
|
expect(validateInterval('2h')).toBe(true)
|
|
185
198
|
expect(validateInterval('1d')).toBe(true)
|
|
186
|
-
expect(validateInterval('0s')).toBe(true)
|
|
187
199
|
expect(validateInterval('999d')).toBe(true)
|
|
188
200
|
})
|
|
189
201
|
|
|
190
202
|
it('should return false for invalid intervals', () => {
|
|
203
|
+
expect(validateInterval('0s')).toBe(false)
|
|
204
|
+
expect(validateInterval('1s')).toBe(false)
|
|
205
|
+
expect(validateInterval('59s')).toBe(false)
|
|
206
|
+
expect(validateInterval('0m')).toBe(false)
|
|
191
207
|
expect(validateInterval('invalid')).toBe(false)
|
|
192
208
|
expect(validateInterval('')).toBe(false)
|
|
193
209
|
expect(validateInterval('15')).toBe(false)
|
|
@@ -204,7 +220,8 @@ describe('intervalParser', () => {
|
|
|
204
220
|
it('should convert seconds to human readable', () => {
|
|
205
221
|
expect(intervalToHuman('1s')).toBe('1 second')
|
|
206
222
|
expect(intervalToHuman('30s')).toBe('30 seconds')
|
|
207
|
-
expect(intervalToHuman('
|
|
223
|
+
expect(intervalToHuman('60s')).toBe('60 seconds')
|
|
224
|
+
expect(intervalToHuman('90s')).toBe('90 seconds')
|
|
208
225
|
})
|
|
209
226
|
})
|
|
210
227
|
|
|
@@ -260,6 +277,7 @@ describe('intervalParser', () => {
|
|
|
260
277
|
it('should use plural for other numbers', () => {
|
|
261
278
|
expect(intervalToHuman('0s')).toBe('0 seconds')
|
|
262
279
|
expect(intervalToHuman('2s')).toBe('2 seconds')
|
|
280
|
+
expect(intervalToHuman('60s')).toBe('60 seconds')
|
|
263
281
|
expect(intervalToHuman('2m')).toBe('2 minutes')
|
|
264
282
|
expect(intervalToHuman('2h')).toBe('2 hours')
|
|
265
283
|
expect(intervalToHuman('2d')).toBe('2 days')
|
|
@@ -300,7 +318,7 @@ describe('intervalParser', () => {
|
|
|
300
318
|
|
|
301
319
|
describe('integration', () => {
|
|
302
320
|
it('should work end-to-end for various intervals', () => {
|
|
303
|
-
const intervals = ['
|
|
321
|
+
const intervals = ['60s', '15m', '2h', '1d']
|
|
304
322
|
|
|
305
323
|
intervals.forEach(interval => {
|
|
306
324
|
expect(validateInterval(interval)).toBe(true)
|
|
@@ -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', '60s', 'UTC', fromDate)
|
|
77
77
|
|
|
78
|
-
expect(result).toEqual(new Date('2025-01-27T12:
|
|
78
|
+
expect(result).toEqual(new Date('2025-01-27T12:01:00Z'))
|
|
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 result60s = recalculateNextRun('interval', '60s')
|
|
227
|
+
const diff60s = result60s!.getTime() - before
|
|
228
|
+
expect(diff60s).toBeGreaterThanOrEqual(60 * 1000 - 500)
|
|
229
|
+
expect(diff60s).toBeLessThanOrEqual(60 * 1000 + 500)
|
|
230
230
|
|
|
231
231
|
const result15m = recalculateNextRun('interval', '15m')
|
|
232
232
|
const diff15m = result15m!.getTime() - before
|
|
@@ -310,6 +310,17 @@ 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
|
+
})
|
|
313
324
|
})
|
|
314
325
|
})
|
|
315
326
|
|
|
@@ -341,7 +352,7 @@ describe('nextRunCalculator', () => {
|
|
|
341
352
|
{ type: 'cron' as const, value: '0 0 * * *' }, // Daily at midnight
|
|
342
353
|
{ type: 'cron' as const, value: '*/5 * * * *' }, // Every 5 minutes
|
|
343
354
|
{ type: 'cron' as const, value: '0 9-17 * * 1-5' }, // Weekdays 9am-5pm
|
|
344
|
-
{ type: 'interval' as const, value: '
|
|
355
|
+
{ type: 'interval' as const, value: '60s' },
|
|
345
356
|
{ type: 'interval' as const, value: '15m' },
|
|
346
357
|
{ type: 'interval' as const, value: '2h' },
|
|
347
358
|
{ type: 'interval' as const, value: '1d' },
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { EntityManager } from '@mikro-orm/core'
|
|
2
|
+
import { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'
|
|
3
|
+
import { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'
|
|
4
|
+
import { createFallbackTranslator, type TranslateWithFallbackFn } from '@open-mercato/shared/lib/i18n/translate'
|
|
5
|
+
import { ScheduledJob } from '../data/entities.js'
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_MAX_ACTIVE_SCHEDULES_PER_TENANT = 100
|
|
8
|
+
const ACTIVE_SCHEDULE_LIMIT_ENV = 'OM_SCHEDULER_MAX_ACTIVE_SCHEDULES_PER_TENANT'
|
|
9
|
+
|
|
10
|
+
export function getMaxActiveSchedulesPerTenant(): number {
|
|
11
|
+
const parsed = Number.parseInt(process.env[ACTIVE_SCHEDULE_LIMIT_ENV] ?? '', 10)
|
|
12
|
+
if (Number.isFinite(parsed) && parsed > 0) return parsed
|
|
13
|
+
return DEFAULT_MAX_ACTIVE_SCHEDULES_PER_TENANT
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function resolveActiveScheduleLimitTranslator(): Promise<TranslateWithFallbackFn> {
|
|
17
|
+
try {
|
|
18
|
+
const { translate } = await resolveTranslations()
|
|
19
|
+
return translate
|
|
20
|
+
} catch {
|
|
21
|
+
return createFallbackTranslator({})
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function enforceTenantActiveScheduleLimit(
|
|
26
|
+
em: EntityManager,
|
|
27
|
+
tenantId: string | null | undefined,
|
|
28
|
+
): Promise<void> {
|
|
29
|
+
if (!tenantId) return
|
|
30
|
+
|
|
31
|
+
const maxActiveSchedules = getMaxActiveSchedulesPerTenant()
|
|
32
|
+
const activeScheduleCount = await em.count(ScheduledJob, {
|
|
33
|
+
tenantId,
|
|
34
|
+
isEnabled: true,
|
|
35
|
+
deletedAt: null,
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
if (activeScheduleCount >= maxActiveSchedules) {
|
|
39
|
+
const translate = await resolveActiveScheduleLimitTranslator()
|
|
40
|
+
throw new CrudHttpError(422, {
|
|
41
|
+
error: translate(
|
|
42
|
+
'scheduler.error.active_schedule_limit',
|
|
43
|
+
'Active scheduled job limit reached for this tenant ({max}).',
|
|
44
|
+
{ max: maxActiveSchedules },
|
|
45
|
+
),
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
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
|
+
|
|
4
6
|
export function parseInterval(interval: string): number {
|
|
5
7
|
const regex = /^(\d+)(s|m|h|d)$/
|
|
6
8
|
const match = interval.match(regex)
|
|
@@ -22,6 +24,10 @@ export function parseInterval(interval: string): number {
|
|
|
22
24
|
return value * multipliers[unit]
|
|
23
25
|
}
|
|
24
26
|
|
|
27
|
+
export function resolveScheduleIntervalMs(interval: string): number {
|
|
28
|
+
return Math.max(parseInterval(interval), MIN_SCHEDULE_INTERVAL_MS)
|
|
29
|
+
}
|
|
30
|
+
|
|
25
31
|
/**
|
|
26
32
|
* Calculate next run time based on interval
|
|
27
33
|
*/
|
|
@@ -29,7 +35,7 @@ export function calculateNextRunFromInterval(
|
|
|
29
35
|
interval: string,
|
|
30
36
|
fromDate: Date = new Date()
|
|
31
37
|
): Date {
|
|
32
|
-
const ms =
|
|
38
|
+
const ms = resolveScheduleIntervalMs(interval)
|
|
33
39
|
return new Date(fromDate.getTime() + ms)
|
|
34
40
|
}
|
|
35
41
|
|
|
@@ -38,8 +44,7 @@ export function calculateNextRunFromInterval(
|
|
|
38
44
|
*/
|
|
39
45
|
export function validateInterval(interval: string): boolean {
|
|
40
46
|
try {
|
|
41
|
-
parseInterval(interval)
|
|
42
|
-
return true
|
|
47
|
+
return parseInterval(interval) >= MIN_SCHEDULE_INTERVAL_MS
|
|
43
48
|
} catch {
|
|
44
49
|
return false
|
|
45
50
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseCronExpression } from './cronParser.js'
|
|
2
|
-
import { calculateNextRunFromInterval } from './intervalParser.js'
|
|
2
|
+
import { calculateNextRunFromInterval, validateInterval } from './intervalParser.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Calculate the next run time for a schedule
|
|
@@ -28,6 +28,19 @@ 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
|
+
|
|
31
44
|
/**
|
|
32
45
|
* Recalculate next run for a schedule after execution
|
|
33
46
|
*/
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export type SchedulerSafeCommandDefinition = {
|
|
2
|
+
commandId: string
|
|
3
|
+
requiredFeatures: readonly [string, ...string[]]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type SchedulerCommandRbacService = {
|
|
7
|
+
userHasAllFeatures?: (
|
|
8
|
+
userId: string,
|
|
9
|
+
required: readonly string[],
|
|
10
|
+
scope: { tenantId: string | null; organizationId: string | null }
|
|
11
|
+
) => Promise<boolean>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const schedulerSafeCommands = new Map<string, SchedulerSafeCommandDefinition>()
|
|
15
|
+
|
|
16
|
+
function normalizeCommandId(commandId: unknown): string | null {
|
|
17
|
+
if (typeof commandId !== 'string') return null
|
|
18
|
+
const trimmed = commandId.trim()
|
|
19
|
+
return trimmed.length > 0 ? trimmed : null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function normalizeFeatures(features: readonly string[]): [string, ...string[]] | null {
|
|
23
|
+
const normalized = features
|
|
24
|
+
.map((feature) => feature.trim())
|
|
25
|
+
.filter((feature) => feature.length > 0)
|
|
26
|
+
return normalized.length > 0 ? (normalized as [string, ...string[]]) : null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function registerSchedulerSafeCommands(commands: readonly SchedulerSafeCommandDefinition[]): void {
|
|
30
|
+
for (const command of commands) {
|
|
31
|
+
const commandId = normalizeCommandId(command.commandId)
|
|
32
|
+
const requiredFeatures = normalizeFeatures(command.requiredFeatures)
|
|
33
|
+
if (!commandId || !requiredFeatures) {
|
|
34
|
+
throw new Error('[internal] Scheduler-safe commands require a commandId and requiredFeatures')
|
|
35
|
+
}
|
|
36
|
+
schedulerSafeCommands.set(commandId, { commandId, requiredFeatures })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getSchedulerSafeCommand(commandId: unknown): SchedulerSafeCommandDefinition | null {
|
|
41
|
+
const normalized = normalizeCommandId(commandId)
|
|
42
|
+
if (!normalized) return null
|
|
43
|
+
return schedulerSafeCommands.get(normalized) ?? null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isSchedulerSafeCommandId(commandId: unknown): boolean {
|
|
47
|
+
return getSchedulerSafeCommand(commandId) !== null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function listSchedulerSafeCommands(): SchedulerSafeCommandDefinition[] {
|
|
51
|
+
return Array.from(schedulerSafeCommands.values()).sort((a, b) => a.commandId.localeCompare(b.commandId))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function assertSchedulerSafeCommandAuthorized(params: {
|
|
55
|
+
commandId: unknown
|
|
56
|
+
actorUserId: string | null | undefined
|
|
57
|
+
tenantId: string | null | undefined
|
|
58
|
+
organizationId: string | null | undefined
|
|
59
|
+
rbacService: SchedulerCommandRbacService
|
|
60
|
+
}): Promise<SchedulerSafeCommandDefinition> {
|
|
61
|
+
const command = getSchedulerSafeCommand(params.commandId)
|
|
62
|
+
if (!command) {
|
|
63
|
+
throw new Error('Scheduled command is not allowed')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const actorUserId = normalizeCommandId(params.actorUserId)
|
|
67
|
+
if (!actorUserId) {
|
|
68
|
+
throw new Error('Scheduled command requires an authenticated creator')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (typeof params.rbacService.userHasAllFeatures !== 'function') {
|
|
72
|
+
throw new Error('Scheduled command authorization is unavailable')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const authorized = await params.rbacService.userHasAllFeatures(actorUserId, command.requiredFeatures, {
|
|
76
|
+
tenantId: params.tenantId ?? null,
|
|
77
|
+
organizationId: params.organizationId ?? null,
|
|
78
|
+
})
|
|
79
|
+
if (!authorized) {
|
|
80
|
+
throw new Error('Scheduled command creator is not authorized')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return command
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function clearSchedulerSafeCommandsForTests(): void {
|
|
87
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
88
|
+
throw new Error('[internal] clearSchedulerSafeCommandsForTests is test-only')
|
|
89
|
+
}
|
|
90
|
+
schedulerSafeCommands.clear()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
registerSchedulerSafeCommands([
|
|
94
|
+
{
|
|
95
|
+
commandId: 'scheduler.test.echo',
|
|
96
|
+
requiredFeatures: ['scheduler.jobs.manage'],
|
|
97
|
+
},
|
|
98
|
+
])
|