@goodandready/dsh-cron 0.3.43 → 0.3.44
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/lib/client.js +998 -3987
- package/lib/llm-ask.js +2 -1
- package/lib/routes/api-actions.js +36 -6
- package/lib/routes/api-item.js +1 -1
- package/lib/routes/api-webhook.js +2 -4
- package/lib/runtimes/runner-external.js +2 -3
- package/lib/runtimes/runner-session.js +385 -0
- package/lib/runtimes/runner-turn.js +255 -0
- package/lib/runtimes/runner.js +43 -616
- package/lib/scheduler/scheduler-execution.js +4 -1
- package/lib/scheduler/scheduler-lifecycle.js +65 -0
- package/lib/scheduler/scheduler-parser.js +261 -0
- package/lib/scheduler/scheduler.js +13 -319
- package/lib/store/store-archive.js +195 -0
- package/lib/store/store-io.js +331 -0
- package/lib/store/store-loader.js +165 -0
- package/lib/store/store-migration.js +38 -0
- package/lib/store/store-settings.js +126 -0
- package/lib/store/store-stats.js +55 -0
- package/lib/store/store.js +75 -888
- package/package.json +8 -6
- package/lib/client-src/00-open.js +0 -63
- package/lib/client-src/10-locales.js +0 -540
- package/lib/client-src/20-styles.js +0 -64
- package/lib/client-src/30-utils.js +0 -372
- package/lib/client-src/40-delivery-settings.js +0 -96
- package/lib/client-src/45-dag-graph-view.js +0 -287
- package/lib/client-src/46-live-logs-modal.js +0 -108
- package/lib/client-src/47-analytics-digest-modal.js +0 -166
- package/lib/client-src/48-timeline-heatmap-view.js +0 -176
- package/lib/client-src/50-cron-screen-state.js +0 -457
- package/lib/client-src/52-cron-screen-actions.js +0 -330
- package/lib/client-src/53-cron-screen-modal-actions.js +0 -677
- package/lib/client-src/54-cron-screen-analytics-actions.js +0 -68
- package/lib/client-src/55-cron-screen-view.js +0 -378
- package/lib/client-src/60-modal-task-form-head.js +0 -581
- package/lib/client-src/62-modal-task-form-tail.js +0 -563
- package/lib/client-src/65-modal-dialogs.js +0 -350
- package/lib/client-src/70-settings-card.js +0 -453
- package/lib/client-src/75-quick-action-bar.js +0 -146
- package/lib/client-src/80-sidebar-jobs.js +0 -200
- package/lib/client-src/90-close.js +0 -219
|
@@ -260,7 +260,10 @@ export async function executeTask(scheduler, taskId, options = {}) {
|
|
|
260
260
|
logger.warn(`[dsh-cron] Burn Guard pre-flight blocked task "${task.title}" (${taskId}): ${guard.reason}`);
|
|
261
261
|
scheduler.pauseTask(taskId, guard.reason);
|
|
262
262
|
scheduler.recordSkipped(taskId, `Burn Guard pre-flight pause: ${guard.reason}`);
|
|
263
|
-
checkAndApplyBurnGuard(scheduler, taskId, { status: 'error', costUsd: 0, durationMs: 0 }).catch(() => {
|
|
263
|
+
Promise.resolve(checkAndApplyBurnGuard(scheduler, taskId, { status: 'error', costUsd: 0, durationMs: 0 })).catch((err) => {
|
|
264
|
+
const warnLog = (scheduler?.logger && typeof scheduler.logger.warn === 'function') ? scheduler.logger : logger;
|
|
265
|
+
warnLog.warn(`[dsh-cron] Burn Guard pre-flight alert/bookkeeping failed for task "${task.title}" (${taskId}): ${err?.message || err}`);
|
|
266
|
+
});
|
|
264
267
|
return { ok: false, status: 'error', error: guard.reason, preflightBlocked: true };
|
|
265
268
|
}
|
|
266
269
|
|
|
@@ -155,3 +155,68 @@ export function toggleTask(scheduler, taskId) {
|
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
+
export function handleStopAll(scheduler) {
|
|
159
|
+
scheduler.isStopped = true;
|
|
160
|
+
scheduler.generation = (scheduler.generation || 0) + 1;
|
|
161
|
+
scheduler.stopHeartbeatWatcher();
|
|
162
|
+
if (scheduler.watcherManager) {
|
|
163
|
+
scheduler.watcherManager.stopAll();
|
|
164
|
+
}
|
|
165
|
+
if (scheduler.wakeTriggerManager) {
|
|
166
|
+
scheduler.wakeTriggerManager.stopAll();
|
|
167
|
+
}
|
|
168
|
+
if (scheduler.cronAsCodeManager) {
|
|
169
|
+
scheduler.cronAsCodeManager.stopAll();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Clear concurrency queue (#217)
|
|
173
|
+
scheduler.queue = [];
|
|
174
|
+
|
|
175
|
+
for (const [, job] of scheduler.jobs.entries()) {
|
|
176
|
+
bestEffort('stop-job', () => job.stop(), scheduler.logger);
|
|
177
|
+
}
|
|
178
|
+
scheduler.jobs.clear();
|
|
179
|
+
|
|
180
|
+
for (const [, timer] of scheduler.timers.entries()) {
|
|
181
|
+
bestEffort('clear-timer', () => clearTimeout(timer), scheduler.logger);
|
|
182
|
+
}
|
|
183
|
+
scheduler.timers.clear();
|
|
184
|
+
|
|
185
|
+
for (const [, timer] of scheduler.retryTimers.entries()) {
|
|
186
|
+
bestEffort('clear-retry-timer', () => clearTimeout(timer), scheduler.logger);
|
|
187
|
+
}
|
|
188
|
+
scheduler.retryTimers.clear();
|
|
189
|
+
|
|
190
|
+
for (const [, timer] of scheduler.adaptiveTimers.entries()) {
|
|
191
|
+
bestEffort('clear-adaptive-timer', () => clearTimeout(timer), scheduler.logger);
|
|
192
|
+
}
|
|
193
|
+
scheduler.adaptiveTimers.clear();
|
|
194
|
+
|
|
195
|
+
for (const [, run] of scheduler.running.entries()) {
|
|
196
|
+
bestEffort('abort-running', () => run.controller.abort(new Error('Scheduler stopped')), scheduler.logger);
|
|
197
|
+
}
|
|
198
|
+
scheduler.running.clear();
|
|
199
|
+
|
|
200
|
+
if (scheduler.store && typeof scheduler.store.flushSync === 'function') {
|
|
201
|
+
bestEffort('store-flush-on-stop', () => scheduler.store.flushSync(), scheduler.logger);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function handleClearScheduled(scheduler, taskId) {
|
|
206
|
+
scheduler.clearAdaptiveTimer(taskId);
|
|
207
|
+
scheduler.clearDeferredTimer(taskId);
|
|
208
|
+
if (scheduler.wakeTriggerManager) {
|
|
209
|
+
scheduler.wakeTriggerManager.unregisterTask(taskId);
|
|
210
|
+
}
|
|
211
|
+
if (scheduler.watcherManager) {
|
|
212
|
+
scheduler.watcherManager.unwatchTask(taskId);
|
|
213
|
+
}
|
|
214
|
+
if (scheduler.jobs.has(taskId)) {
|
|
215
|
+
scheduler.jobs.get(taskId).stop();
|
|
216
|
+
scheduler.jobs.delete(taskId);
|
|
217
|
+
}
|
|
218
|
+
if (scheduler.timers.has(taskId)) {
|
|
219
|
+
clearTimeout(scheduler.timers.get(taskId));
|
|
220
|
+
scheduler.timers.delete(taskId);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { Cron } from 'croner';
|
|
2
|
+
import { parseNaturalSchedule } from '../nl-schedule.js';
|
|
3
|
+
|
|
4
|
+
function pad(v) {
|
|
5
|
+
return String(v).padStart(2, '0');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function describeCron(cronPattern) {
|
|
9
|
+
const parts = cronPattern.trim().split(/\s+/);
|
|
10
|
+
if (parts.length === 5) {
|
|
11
|
+
const [m, h, dom, mon, dow] = parts;
|
|
12
|
+
if (m.startsWith('*/')) return `Every ${m.slice(2)} minutes`;
|
|
13
|
+
if (dow === '1-5' && dom === '*' && mon === '*') {
|
|
14
|
+
return `Weekdays at ${pad(h)}:${pad(m)}`;
|
|
15
|
+
}
|
|
16
|
+
if (dow === '*' && dom === '*' && mon === '*') {
|
|
17
|
+
return `Every day at ${pad(h)}:${pad(m)}`;
|
|
18
|
+
}
|
|
19
|
+
if (dow === '5' && dom === '*' && mon === '*') {
|
|
20
|
+
return `Fridays at ${pad(h)}:${pad(m)}`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return cronPattern;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One-shot from an ISO timestamp or an explicit "at:" prefix, else null. */
|
|
27
|
+
function parseAtExpression(str) {
|
|
28
|
+
const atPrefixMatch = str.match(/^(?:at:\s*|at\s+)(.+)$/i);
|
|
29
|
+
const candidateIso = atPrefixMatch ? atPrefixMatch[1].trim() : str;
|
|
30
|
+
const parsedDate = new Date(candidateIso);
|
|
31
|
+
if (isNaN(parsedDate.getTime()) || !(candidateIso.includes('-') || candidateIso.includes('T') || atPrefixMatch)) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const targetMs = parsedDate.getTime();
|
|
35
|
+
return {
|
|
36
|
+
cronPattern: null,
|
|
37
|
+
isOneShot: true,
|
|
38
|
+
targetTimestamp: targetMs,
|
|
39
|
+
humanText: `One-shot at ${parsedDate.toISOString()}`,
|
|
40
|
+
nextRun: targetMs
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Relative one-shot: "in 20m", "in 2h", "in 30s", "in 1d" and the accepted
|
|
46
|
+
* Russian input aliases ("через 15 минут").
|
|
47
|
+
*/
|
|
48
|
+
function parseRelativeOneShot(str) {
|
|
49
|
+
const match = str.match(/^(?:in\s+|через\s+)(\d+)\s*(s|sec|seconds?|m|min|minutes?|h|hr|hours?|d|days?|мин|минут|часа?|часов)?$/i);
|
|
50
|
+
if (!match) return null;
|
|
51
|
+
const num = parseInt(match[1], 10);
|
|
52
|
+
const unit = (match[2] || 'm').toLowerCase();
|
|
53
|
+
let delayMs = num * 60 * 1000;
|
|
54
|
+
let unitText = `${num} min`;
|
|
55
|
+
if (unit.startsWith('s')) {
|
|
56
|
+
delayMs = num * 1000;
|
|
57
|
+
unitText = `${num} sec`;
|
|
58
|
+
} else if (unit.startsWith('h') || unit.startsWith('час')) {
|
|
59
|
+
delayMs = num * 3600 * 1000;
|
|
60
|
+
unitText = `${num} h`;
|
|
61
|
+
} else if (unit.startsWith('d')) {
|
|
62
|
+
delayMs = num * 86400 * 1000;
|
|
63
|
+
unitText = `${num} d`;
|
|
64
|
+
}
|
|
65
|
+
const targetMs = Date.now() + delayMs;
|
|
66
|
+
return {
|
|
67
|
+
cronPattern: null,
|
|
68
|
+
isOneShot: true,
|
|
69
|
+
targetTimestamp: targetMs,
|
|
70
|
+
humanText: `One-shot in ${unitText} (at ${new Date(targetMs).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })})`,
|
|
71
|
+
nextRun: targetMs
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Repeating interval: "every 5m", "every 2h", "every 1d". */
|
|
76
|
+
function parseIntervalExpression(str) {
|
|
77
|
+
const match = str.match(/^every\s+(\d+)\s*(m|min|minute|minutes|h|hr|hour|hours|d|day|days)?$/i);
|
|
78
|
+
if (!match) return null;
|
|
79
|
+
const num = parseInt(match[1], 10);
|
|
80
|
+
const unit = (match[2] || 'm').toLowerCase();
|
|
81
|
+
let pattern = null;
|
|
82
|
+
let humanText = null;
|
|
83
|
+
|
|
84
|
+
if (unit.startsWith('m')) {
|
|
85
|
+
if (num < 1) {
|
|
86
|
+
throw new Error('Invalid interval: minute interval must be at least 1 minute');
|
|
87
|
+
}
|
|
88
|
+
if (num >= 60) {
|
|
89
|
+
if (num % 60 === 0) {
|
|
90
|
+
const hours = num / 60;
|
|
91
|
+
if (hours >= 24) {
|
|
92
|
+
if (hours % 24 === 0) {
|
|
93
|
+
const days = hours / 24;
|
|
94
|
+
if (days > 31) {
|
|
95
|
+
throw new Error(`Invalid interval: minute interval "${num}m" (${days} days) exceeds 31 days`);
|
|
96
|
+
}
|
|
97
|
+
pattern = `0 0 */${days} * *`;
|
|
98
|
+
humanText = `Every ${days} days`;
|
|
99
|
+
} else {
|
|
100
|
+
throw new Error(`Invalid interval: minute interval "${num}m" (${hours} hours) cannot be expressed as a cron step`);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
pattern = `0 */${hours} * * *`;
|
|
104
|
+
humanText = `Every ${hours} hours`;
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
throw new Error(`Invalid interval: minute interval "${num}m" exceeds 59 minutes and is not a multiple of 60. Use hours (e.g. every 2h) or a multiple of 60m`);
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
pattern = `*/${num} * * * *`;
|
|
111
|
+
humanText = `Every ${num} minutes`;
|
|
112
|
+
}
|
|
113
|
+
} else if (unit.startsWith('h')) {
|
|
114
|
+
if (num < 1) {
|
|
115
|
+
throw new Error('Invalid interval: hour interval must be at least 1 hour');
|
|
116
|
+
}
|
|
117
|
+
if (num >= 24) {
|
|
118
|
+
if (num % 24 === 0) {
|
|
119
|
+
const days = num / 24;
|
|
120
|
+
if (days > 31) {
|
|
121
|
+
throw new Error(`Invalid interval: hour interval "${num}h" (${days} days) exceeds 31 days`);
|
|
122
|
+
}
|
|
123
|
+
pattern = `0 0 */${days} * *`;
|
|
124
|
+
humanText = `Every ${days} days`;
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error(`Invalid interval: hour interval "${num}h" exceeds 23 hours and is not a multiple of 24. Use days (e.g. every 2d) or a multiple of 24h`);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
pattern = `0 */${num} * * *`;
|
|
130
|
+
humanText = `Every ${num} hours`;
|
|
131
|
+
}
|
|
132
|
+
} else if (unit.startsWith('d')) {
|
|
133
|
+
if (num < 1 || num > 31) {
|
|
134
|
+
throw new Error(`Invalid interval: day interval "${num}d" must be between 1 and 31 days`);
|
|
135
|
+
}
|
|
136
|
+
pattern = `0 0 */${num} * *`;
|
|
137
|
+
humanText = `Every ${num} days`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (pattern) {
|
|
141
|
+
try {
|
|
142
|
+
const testJob = new Cron(pattern, { sloppyRanges: true });
|
|
143
|
+
const next = testJob.nextRun();
|
|
144
|
+
return {
|
|
145
|
+
cronPattern: pattern,
|
|
146
|
+
humanText,
|
|
147
|
+
isInterval: true,
|
|
148
|
+
nextRun: next ? next.getTime() : null,
|
|
149
|
+
};
|
|
150
|
+
} catch (err) {
|
|
151
|
+
throw new Error(`Invalid interval pattern "${pattern}": ${err.message}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @-shorthands (#15) and the friendly aliases, including the Russian input
|
|
159
|
+
* aliases. Russian keywords are accepted input, not display strings.
|
|
160
|
+
*/
|
|
161
|
+
function makeAliasResult(cronPattern, humanText) {
|
|
162
|
+
try {
|
|
163
|
+
const job = new Cron(cronPattern, { sloppyRanges: true });
|
|
164
|
+
const next = job.nextRun();
|
|
165
|
+
return { cronPattern, humanText, nextRun: next ? next.getTime() : null };
|
|
166
|
+
} catch {
|
|
167
|
+
return { cronPattern, humanText };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function parseAliasExpression(str) {
|
|
172
|
+
const lower = str.toLowerCase();
|
|
173
|
+
if (lower === '@hourly') return makeAliasResult('0 * * * *', 'Every hour');
|
|
174
|
+
if (lower === '@daily' || lower === '@midnight') return makeAliasResult('0 0 * * *', 'Every day at 00:00');
|
|
175
|
+
if (lower === '@weekly') return makeAliasResult('0 0 * * 0', 'Every week on Sunday at 00:00');
|
|
176
|
+
if (lower === '@monthly') return makeAliasResult('0 0 1 * *', 'Every month on the 1st at 00:00');
|
|
177
|
+
if (lower === '@yearly' || lower === '@annually') return makeAliasResult('0 0 1 1 *', 'Every year on Jan 1 at 00:00');
|
|
178
|
+
const everyAlias = str.match(/^@every\s+(\d+)\s*(sec|seconds?|m|min|minutes?|h|hr|hours?|d|days?)$/i);
|
|
179
|
+
if (everyAlias) {
|
|
180
|
+
return parseIntervalExpression(`every ${everyAlias[1]} ${everyAlias[2]}`);
|
|
181
|
+
}
|
|
182
|
+
if (lower === 'daily' || lower === 'каждый день') {
|
|
183
|
+
return makeAliasResult('0 9 * * *', 'Every day at 09:00');
|
|
184
|
+
}
|
|
185
|
+
if (lower === 'weekdays' || lower === 'в будние дни' || lower === 'по будням') {
|
|
186
|
+
return makeAliasResult('0 9 * * 1-5', 'Weekdays at 09:00');
|
|
187
|
+
}
|
|
188
|
+
if (lower === 'hourly' || lower === 'каждый час') {
|
|
189
|
+
return makeAliasResult('0 * * * *', 'Every hour');
|
|
190
|
+
}
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Validate a 5-field cron expression; the only branch that throws. */
|
|
195
|
+
function parseCronExpression(str) {
|
|
196
|
+
try {
|
|
197
|
+
const testJob = new Cron(str, { sloppyRanges: true });
|
|
198
|
+
const next = testJob.nextRun();
|
|
199
|
+
return {
|
|
200
|
+
cronPattern: str,
|
|
201
|
+
humanText: describeCron(str),
|
|
202
|
+
nextRun: next ? next.getTime() : null,
|
|
203
|
+
};
|
|
204
|
+
} catch (err) {
|
|
205
|
+
throw new Error('Invalid cron schedule: ' + err.message);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Turn a typed schedule into a cron pattern or a one-shot target.
|
|
211
|
+
*/
|
|
212
|
+
export function parseScheduleExpression(input) {
|
|
213
|
+
const str = String(input || '').trim();
|
|
214
|
+
if (!str) throw new Error('Schedule expression must not be empty');
|
|
215
|
+
|
|
216
|
+
return parseAtExpression(str)
|
|
217
|
+
|| parseRelativeOneShot(str)
|
|
218
|
+
|| parseIntervalExpression(str)
|
|
219
|
+
|| parseAliasExpression(str)
|
|
220
|
+
|| parseNaturalSchedule(str)
|
|
221
|
+
|| parseCronExpression(str);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Preview future execution timestamps for a cron or interval schedule.
|
|
226
|
+
*/
|
|
227
|
+
export function previewSchedule(scheduleStr, options = {}) {
|
|
228
|
+
const str = String(scheduleStr || '').trim();
|
|
229
|
+
if (!str) throw new Error('Schedule expression must not be empty');
|
|
230
|
+
const parsed = parseScheduleExpression(str);
|
|
231
|
+
const count = Math.min(Math.max(Number(options.count) || 5, 1), 10);
|
|
232
|
+
const timezone = options.timezone || undefined;
|
|
233
|
+
|
|
234
|
+
if (parsed.isOneShot) {
|
|
235
|
+
return {
|
|
236
|
+
isOneShot: true,
|
|
237
|
+
humanText: parsed.humanText,
|
|
238
|
+
runs: [new Date(parsed.nextRun).toISOString()],
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const job = new Cron(parsed.cronPattern, {
|
|
243
|
+
timezone,
|
|
244
|
+
sloppyRanges: true,
|
|
245
|
+
});
|
|
246
|
+
const runs = [];
|
|
247
|
+
let current = new Date();
|
|
248
|
+
for (let i = 0; i < count; i++) {
|
|
249
|
+
const next = job.nextRun(current);
|
|
250
|
+
if (!next) break;
|
|
251
|
+
runs.push(next.toISOString());
|
|
252
|
+
current = new Date(next.getTime() + 1000);
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
isOneShot: false,
|
|
256
|
+
cronPattern: parsed.cronPattern,
|
|
257
|
+
humanText: describeCron(parsed.cronPattern),
|
|
258
|
+
runs,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
@@ -32,9 +32,20 @@ import {
|
|
|
32
32
|
removeTask,
|
|
33
33
|
resumeTask,
|
|
34
34
|
toggleTask,
|
|
35
|
+
handleStopAll,
|
|
36
|
+
handleClearScheduled,
|
|
35
37
|
} from './scheduler-lifecycle.js';
|
|
36
38
|
|
|
39
|
+
import {
|
|
40
|
+
describeCron,
|
|
41
|
+
parseScheduleExpression,
|
|
42
|
+
previewSchedule,
|
|
43
|
+
} from './scheduler-parser.js';
|
|
44
|
+
|
|
37
45
|
export {
|
|
46
|
+
describeCron,
|
|
47
|
+
parseScheduleExpression,
|
|
48
|
+
previewSchedule,
|
|
38
49
|
parseNaturalSchedule,
|
|
39
50
|
executePreflight,
|
|
40
51
|
AGENT_TASK_TYPES,
|
|
@@ -60,264 +71,6 @@ export {
|
|
|
60
71
|
FileSystemWatcherManager,
|
|
61
72
|
};
|
|
62
73
|
|
|
63
|
-
function pad(v) {
|
|
64
|
-
return String(v).padStart(2, '0');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function describeCron(cronPattern) {
|
|
68
|
-
const parts = cronPattern.trim().split(/\s+/);
|
|
69
|
-
if (parts.length === 5) {
|
|
70
|
-
const [m, h, dom, mon, dow] = parts;
|
|
71
|
-
if (m.startsWith('*/')) return `Every ${m.slice(2)} minutes`;
|
|
72
|
-
if (dow === '1-5' && dom === '*' && mon === '*') {
|
|
73
|
-
return `Weekdays at ${pad(h)}:${pad(m)}`;
|
|
74
|
-
}
|
|
75
|
-
if (dow === '*' && dom === '*' && mon === '*') {
|
|
76
|
-
return `Every day at ${pad(h)}:${pad(m)}`;
|
|
77
|
-
}
|
|
78
|
-
if (dow === '5' && dom === '*' && mon === '*') {
|
|
79
|
-
return `Fridays at ${pad(h)}:${pad(m)}`;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return cronPattern;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** One-shot from an ISO timestamp or an explicit "at:" prefix, else null. */
|
|
86
|
-
function parseAtExpression(str) {
|
|
87
|
-
const atPrefixMatch = str.match(/^(?:at:\s*|at\s+)(.+)$/i);
|
|
88
|
-
const candidateIso = atPrefixMatch ? atPrefixMatch[1].trim() : str;
|
|
89
|
-
const parsedDate = new Date(candidateIso);
|
|
90
|
-
if (isNaN(parsedDate.getTime()) || !(candidateIso.includes('-') || candidateIso.includes('T') || atPrefixMatch)) {
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
const targetMs = parsedDate.getTime();
|
|
94
|
-
return {
|
|
95
|
-
cronPattern: null,
|
|
96
|
-
isOneShot: true,
|
|
97
|
-
targetTimestamp: targetMs,
|
|
98
|
-
humanText: `One-shot at ${parsedDate.toISOString()}`,
|
|
99
|
-
nextRun: targetMs
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Relative one-shot: "in 20m", "in 2h", "in 30s", "in 1d" and the accepted
|
|
105
|
-
* Russian input aliases ("через 15 минут").
|
|
106
|
-
*/
|
|
107
|
-
function parseRelativeOneShot(str) {
|
|
108
|
-
const match = str.match(/^(?:in\s+|через\s+)(\d+)\s*(s|sec|seconds?|m|min|minutes?|h|hr|hours?|d|days?|мин|минут|часа?|часов)?$/i);
|
|
109
|
-
if (!match) return null;
|
|
110
|
-
const num = parseInt(match[1], 10);
|
|
111
|
-
const unit = (match[2] || 'm').toLowerCase();
|
|
112
|
-
let delayMs = num * 60 * 1000;
|
|
113
|
-
let unitText = `${num} min`;
|
|
114
|
-
if (unit.startsWith('s')) {
|
|
115
|
-
delayMs = num * 1000;
|
|
116
|
-
unitText = `${num} sec`;
|
|
117
|
-
} else if (unit.startsWith('h') || unit.startsWith('час')) {
|
|
118
|
-
delayMs = num * 3600 * 1000;
|
|
119
|
-
unitText = `${num} h`;
|
|
120
|
-
} else if (unit.startsWith('d')) {
|
|
121
|
-
delayMs = num * 86400 * 1000;
|
|
122
|
-
unitText = `${num} d`;
|
|
123
|
-
}
|
|
124
|
-
const targetMs = Date.now() + delayMs;
|
|
125
|
-
return {
|
|
126
|
-
cronPattern: null,
|
|
127
|
-
isOneShot: true,
|
|
128
|
-
targetTimestamp: targetMs,
|
|
129
|
-
humanText: `One-shot in ${unitText} (at ${new Date(targetMs).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })})`,
|
|
130
|
-
nextRun: targetMs
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Repeating interval: "every 5m", "every 2h", "every 1d". */
|
|
135
|
-
function parseIntervalExpression(str) {
|
|
136
|
-
const match = str.match(/^every\s+(\d+)\s*(m|min|minute|minutes|h|hr|hour|hours|d|day|days)?$/i);
|
|
137
|
-
if (!match) return null;
|
|
138
|
-
const num = parseInt(match[1], 10);
|
|
139
|
-
const unit = (match[2] || 'm').toLowerCase();
|
|
140
|
-
let pattern = null;
|
|
141
|
-
let humanText = null;
|
|
142
|
-
|
|
143
|
-
if (unit.startsWith('m')) {
|
|
144
|
-
if (num < 1) {
|
|
145
|
-
throw new Error('Invalid interval: minute interval must be at least 1 minute');
|
|
146
|
-
}
|
|
147
|
-
if (num >= 60) {
|
|
148
|
-
if (num % 60 === 0) {
|
|
149
|
-
const hours = num / 60;
|
|
150
|
-
if (hours >= 24) {
|
|
151
|
-
if (hours % 24 === 0) {
|
|
152
|
-
const days = hours / 24;
|
|
153
|
-
if (days > 31) {
|
|
154
|
-
throw new Error(`Invalid interval: minute interval "${num}m" (${days} days) exceeds 31 days`);
|
|
155
|
-
}
|
|
156
|
-
pattern = `0 0 */${days} * *`;
|
|
157
|
-
humanText = `Every ${days} days`;
|
|
158
|
-
} else {
|
|
159
|
-
throw new Error(`Invalid interval: minute interval "${num}m" (${hours} hours) cannot be expressed as a cron step`);
|
|
160
|
-
}
|
|
161
|
-
} else {
|
|
162
|
-
pattern = `0 */${hours} * * *`;
|
|
163
|
-
humanText = `Every ${hours} hours`;
|
|
164
|
-
}
|
|
165
|
-
} else {
|
|
166
|
-
throw new Error(`Invalid interval: minute interval "${num}m" exceeds 59 minutes and is not a multiple of 60. Use hours (e.g. every 2h) or a multiple of 60m`);
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
pattern = `*/${num} * * * *`;
|
|
170
|
-
humanText = `Every ${num} minutes`;
|
|
171
|
-
}
|
|
172
|
-
} else if (unit.startsWith('h')) {
|
|
173
|
-
if (num < 1) {
|
|
174
|
-
throw new Error('Invalid interval: hour interval must be at least 1 hour');
|
|
175
|
-
}
|
|
176
|
-
if (num >= 24) {
|
|
177
|
-
if (num % 24 === 0) {
|
|
178
|
-
const days = num / 24;
|
|
179
|
-
if (days > 31) {
|
|
180
|
-
throw new Error(`Invalid interval: hour interval "${num}h" (${days} days) exceeds 31 days`);
|
|
181
|
-
}
|
|
182
|
-
pattern = `0 0 */${days} * *`;
|
|
183
|
-
humanText = `Every ${days} days`;
|
|
184
|
-
} else {
|
|
185
|
-
throw new Error(`Invalid interval: hour interval "${num}h" exceeds 23 hours and is not a multiple of 24. Use days (e.g. every 2d) or a multiple of 24h`);
|
|
186
|
-
}
|
|
187
|
-
} else {
|
|
188
|
-
pattern = `0 */${num} * * *`;
|
|
189
|
-
humanText = `Every ${num} hours`;
|
|
190
|
-
}
|
|
191
|
-
} else if (unit.startsWith('d')) {
|
|
192
|
-
if (num < 1 || num > 31) {
|
|
193
|
-
throw new Error(`Invalid interval: day interval "${num}d" must be between 1 and 31 days`);
|
|
194
|
-
}
|
|
195
|
-
pattern = `0 0 */${num} * *`;
|
|
196
|
-
humanText = `Every ${num} days`;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (pattern) {
|
|
200
|
-
try {
|
|
201
|
-
const testJob = new Cron(pattern, { sloppyRanges: true });
|
|
202
|
-
const next = testJob.nextRun();
|
|
203
|
-
return {
|
|
204
|
-
cronPattern: pattern,
|
|
205
|
-
humanText,
|
|
206
|
-
isInterval: true,
|
|
207
|
-
nextRun: next ? next.getTime() : null,
|
|
208
|
-
};
|
|
209
|
-
} catch (err) {
|
|
210
|
-
throw new Error(`Invalid interval pattern "${pattern}": ${err.message}`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* @-shorthands (#15) and the friendly aliases, including the Russian input
|
|
218
|
-
* aliases. Russian keywords are accepted input, not display strings.
|
|
219
|
-
*/
|
|
220
|
-
function makeAliasResult(cronPattern, humanText) {
|
|
221
|
-
try {
|
|
222
|
-
const job = new Cron(cronPattern, { sloppyRanges: true });
|
|
223
|
-
const next = job.nextRun();
|
|
224
|
-
return { cronPattern, humanText, nextRun: next ? next.getTime() : null };
|
|
225
|
-
} catch {
|
|
226
|
-
return { cronPattern, humanText };
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function parseAliasExpression(str) {
|
|
231
|
-
const lower = str.toLowerCase();
|
|
232
|
-
if (lower === '@hourly') return makeAliasResult('0 * * * *', 'Every hour');
|
|
233
|
-
if (lower === '@daily' || lower === '@midnight') return makeAliasResult('0 0 * * *', 'Every day at 00:00');
|
|
234
|
-
if (lower === '@weekly') return makeAliasResult('0 0 * * 0', 'Every week on Sunday at 00:00');
|
|
235
|
-
if (lower === '@monthly') return makeAliasResult('0 0 1 * *', 'Every month on the 1st at 00:00');
|
|
236
|
-
if (lower === '@yearly' || lower === '@annually') return makeAliasResult('0 0 1 1 *', 'Every year on Jan 1 at 00:00');
|
|
237
|
-
const everyAlias = str.match(/^@every\s+(\d+)\s*(sec|seconds?|m|min|minutes?|h|hr|hours?|d|days?)$/i);
|
|
238
|
-
if (everyAlias) {
|
|
239
|
-
return parseIntervalExpression(`every ${everyAlias[1]} ${everyAlias[2]}`);
|
|
240
|
-
}
|
|
241
|
-
if (lower === 'daily' || lower === 'каждый день') {
|
|
242
|
-
return makeAliasResult('0 9 * * *', 'Every day at 09:00');
|
|
243
|
-
}
|
|
244
|
-
if (lower === 'weekdays' || lower === 'в будние дни' || lower === 'по будням') {
|
|
245
|
-
return makeAliasResult('0 9 * * 1-5', 'Weekdays at 09:00');
|
|
246
|
-
}
|
|
247
|
-
if (lower === 'hourly' || lower === 'каждый час') {
|
|
248
|
-
return makeAliasResult('0 * * * *', 'Every hour');
|
|
249
|
-
}
|
|
250
|
-
return null;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/** Validate a 5-field cron expression; the only branch that throws. */
|
|
254
|
-
function parseCronExpression(str) {
|
|
255
|
-
try {
|
|
256
|
-
const testJob = new Cron(str, { sloppyRanges: true });
|
|
257
|
-
const next = testJob.nextRun();
|
|
258
|
-
return {
|
|
259
|
-
cronPattern: str,
|
|
260
|
-
humanText: describeCron(str),
|
|
261
|
-
nextRun: next ? next.getTime() : null,
|
|
262
|
-
};
|
|
263
|
-
} catch (err) {
|
|
264
|
-
throw new Error('Invalid cron schedule: ' + err.message);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Turn a typed schedule into a cron pattern or a one-shot target.
|
|
270
|
-
*/
|
|
271
|
-
export function parseScheduleExpression(input) {
|
|
272
|
-
const str = String(input || '').trim();
|
|
273
|
-
if (!str) throw new Error('Schedule expression must not be empty');
|
|
274
|
-
|
|
275
|
-
return parseAtExpression(str)
|
|
276
|
-
|| parseRelativeOneShot(str)
|
|
277
|
-
|| parseIntervalExpression(str)
|
|
278
|
-
|| parseAliasExpression(str)
|
|
279
|
-
|| parseNaturalSchedule(str)
|
|
280
|
-
|| parseCronExpression(str);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Preview future execution timestamps for a cron or interval schedule.
|
|
285
|
-
*/
|
|
286
|
-
export function previewSchedule(scheduleStr, options = {}) {
|
|
287
|
-
const str = String(scheduleStr || '').trim();
|
|
288
|
-
if (!str) throw new Error('Schedule expression must not be empty');
|
|
289
|
-
const parsed = parseScheduleExpression(str);
|
|
290
|
-
const count = Math.min(Math.max(Number(options.count) || 5, 1), 10);
|
|
291
|
-
const timezone = options.timezone || undefined;
|
|
292
|
-
|
|
293
|
-
if (parsed.isOneShot) {
|
|
294
|
-
return {
|
|
295
|
-
isOneShot: true,
|
|
296
|
-
humanText: parsed.humanText,
|
|
297
|
-
runs: [new Date(parsed.nextRun).toISOString()],
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
const job = new Cron(parsed.cronPattern, {
|
|
302
|
-
timezone,
|
|
303
|
-
sloppyRanges: true,
|
|
304
|
-
});
|
|
305
|
-
const runs = [];
|
|
306
|
-
let current = new Date();
|
|
307
|
-
for (let i = 0; i < count; i++) {
|
|
308
|
-
const next = job.nextRun(current);
|
|
309
|
-
if (!next) break;
|
|
310
|
-
runs.push(next.toISOString());
|
|
311
|
-
current = new Date(next.getTime() + 1000);
|
|
312
|
-
}
|
|
313
|
-
return {
|
|
314
|
-
isOneShot: false,
|
|
315
|
-
cronPattern: parsed.cronPattern,
|
|
316
|
-
humanText: describeCron(parsed.cronPattern),
|
|
317
|
-
runs,
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
|
|
321
74
|
export class TaskScheduler {
|
|
322
75
|
constructor(store, executeFn, options = {}) {
|
|
323
76
|
const opts = (typeof executeFn === 'object' && executeFn !== null && typeof executeFn !== 'function') ? executeFn : (options || {});
|
|
@@ -518,71 +271,12 @@ export class TaskScheduler {
|
|
|
518
271
|
}
|
|
519
272
|
|
|
520
273
|
stopAll() {
|
|
521
|
-
this
|
|
522
|
-
this.generation = (this.generation || 0) + 1;
|
|
523
|
-
this.stopHeartbeatWatcher();
|
|
524
|
-
if (this.watcherManager) {
|
|
525
|
-
this.watcherManager.stopAll();
|
|
526
|
-
}
|
|
527
|
-
if (this.wakeTriggerManager) {
|
|
528
|
-
this.wakeTriggerManager.stopAll();
|
|
529
|
-
}
|
|
530
|
-
if (this.cronAsCodeManager) {
|
|
531
|
-
this.cronAsCodeManager.stopAll();
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
// Clear concurrency queue (#217)
|
|
535
|
-
this.queue = [];
|
|
536
|
-
|
|
537
|
-
for (const [, job] of this.jobs.entries()) {
|
|
538
|
-
bestEffort('stop-job', () => job.stop(), this.logger);
|
|
539
|
-
}
|
|
540
|
-
this.jobs.clear();
|
|
541
|
-
|
|
542
|
-
for (const [, timer] of this.timers.entries()) {
|
|
543
|
-
bestEffort('clear-timer', () => clearTimeout(timer), this.logger);
|
|
544
|
-
}
|
|
545
|
-
this.timers.clear();
|
|
546
|
-
|
|
547
|
-
for (const [, timer] of this.retryTimers.entries()) {
|
|
548
|
-
bestEffort('clear-retry-timer', () => clearTimeout(timer), this.logger);
|
|
549
|
-
}
|
|
550
|
-
this.retryTimers.clear();
|
|
551
|
-
|
|
552
|
-
for (const [, timer] of this.adaptiveTimers.entries()) {
|
|
553
|
-
bestEffort('clear-adaptive-timer', () => clearTimeout(timer), this.logger);
|
|
554
|
-
}
|
|
555
|
-
this.adaptiveTimers.clear();
|
|
556
|
-
|
|
557
|
-
for (const [, run] of this.running.entries()) {
|
|
558
|
-
bestEffort('abort-running', () => run.controller.abort(new Error('Scheduler stopped')), this.logger);
|
|
559
|
-
}
|
|
560
|
-
this.running.clear();
|
|
561
|
-
|
|
562
|
-
if (this.store && typeof this.store.flushSync === 'function') {
|
|
563
|
-
bestEffort('store-flush-on-stop', () => this.store.flushSync(), this.logger);
|
|
564
|
-
}
|
|
274
|
+
return handleStopAll(this);
|
|
565
275
|
}
|
|
566
276
|
|
|
567
277
|
clearScheduled(taskId) {
|
|
568
|
-
this
|
|
569
|
-
this.clearDeferredTimer(taskId);
|
|
570
|
-
if (this.wakeTriggerManager) {
|
|
571
|
-
this.wakeTriggerManager.unregisterTask(taskId);
|
|
572
|
-
}
|
|
573
|
-
if (this.watcherManager) {
|
|
574
|
-
this.watcherManager.unwatchTask(taskId);
|
|
575
|
-
}
|
|
576
|
-
if (this.jobs.has(taskId)) {
|
|
577
|
-
this.jobs.get(taskId).stop();
|
|
578
|
-
this.jobs.delete(taskId);
|
|
579
|
-
}
|
|
580
|
-
if (this.timers.has(taskId)) {
|
|
581
|
-
clearTimeout(this.timers.get(taskId));
|
|
582
|
-
this.timers.delete(taskId);
|
|
583
|
-
}
|
|
278
|
+
return handleClearScheduled(this, taskId);
|
|
584
279
|
}
|
|
585
|
-
|
|
586
280
|
scheduleOneShot(task, parsed) {
|
|
587
281
|
task.oneShot = true;
|
|
588
282
|
task.targetTimestamp = parsed.targetTimestamp;
|