@goodandready/dsh-cron 0.3.42 → 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 +24 -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 +127 -810
- 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
|
@@ -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 || {});
|
|
@@ -448,6 +201,13 @@ export class TaskScheduler {
|
|
|
448
201
|
this.isStopped = false;
|
|
449
202
|
this.generation = (this.generation || 0) + 1;
|
|
450
203
|
|
|
204
|
+
if (this.store && (this.store.isReadOnly?.() || this.store.hasLoadFailed?.())) {
|
|
205
|
+
const accessCode = this.store.getAccessErrorCode?.();
|
|
206
|
+
const codeInfo = accessCode ? ` (code: ${accessCode})` : '';
|
|
207
|
+
logger.warn(`[dsh-cron] scheduler started in read-only recovery mode${codeInfo}: tasks store could not be loaded. Task scheduling and automatic executions are suspended.`);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
451
211
|
// Recovery & cleanup on restart (#317, #321)
|
|
452
212
|
if (this.store && this.store.durableJournal) {
|
|
453
213
|
try {
|
|
@@ -511,71 +271,12 @@ export class TaskScheduler {
|
|
|
511
271
|
}
|
|
512
272
|
|
|
513
273
|
stopAll() {
|
|
514
|
-
this
|
|
515
|
-
this.generation = (this.generation || 0) + 1;
|
|
516
|
-
this.stopHeartbeatWatcher();
|
|
517
|
-
if (this.watcherManager) {
|
|
518
|
-
this.watcherManager.stopAll();
|
|
519
|
-
}
|
|
520
|
-
if (this.wakeTriggerManager) {
|
|
521
|
-
this.wakeTriggerManager.stopAll();
|
|
522
|
-
}
|
|
523
|
-
if (this.cronAsCodeManager) {
|
|
524
|
-
this.cronAsCodeManager.stopAll();
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
// Clear concurrency queue (#217)
|
|
528
|
-
this.queue = [];
|
|
529
|
-
|
|
530
|
-
for (const [, job] of this.jobs.entries()) {
|
|
531
|
-
bestEffort('stop-job', () => job.stop(), this.logger);
|
|
532
|
-
}
|
|
533
|
-
this.jobs.clear();
|
|
534
|
-
|
|
535
|
-
for (const [, timer] of this.timers.entries()) {
|
|
536
|
-
bestEffort('clear-timer', () => clearTimeout(timer), this.logger);
|
|
537
|
-
}
|
|
538
|
-
this.timers.clear();
|
|
539
|
-
|
|
540
|
-
for (const [, timer] of this.retryTimers.entries()) {
|
|
541
|
-
bestEffort('clear-retry-timer', () => clearTimeout(timer), this.logger);
|
|
542
|
-
}
|
|
543
|
-
this.retryTimers.clear();
|
|
544
|
-
|
|
545
|
-
for (const [, timer] of this.adaptiveTimers.entries()) {
|
|
546
|
-
bestEffort('clear-adaptive-timer', () => clearTimeout(timer), this.logger);
|
|
547
|
-
}
|
|
548
|
-
this.adaptiveTimers.clear();
|
|
549
|
-
|
|
550
|
-
for (const [, run] of this.running.entries()) {
|
|
551
|
-
bestEffort('abort-running', () => run.controller.abort(new Error('Scheduler stopped')), this.logger);
|
|
552
|
-
}
|
|
553
|
-
this.running.clear();
|
|
554
|
-
|
|
555
|
-
if (this.store && typeof this.store.flushSync === 'function') {
|
|
556
|
-
bestEffort('store-flush-on-stop', () => this.store.flushSync(), this.logger);
|
|
557
|
-
}
|
|
274
|
+
return handleStopAll(this);
|
|
558
275
|
}
|
|
559
276
|
|
|
560
277
|
clearScheduled(taskId) {
|
|
561
|
-
this
|
|
562
|
-
this.clearDeferredTimer(taskId);
|
|
563
|
-
if (this.wakeTriggerManager) {
|
|
564
|
-
this.wakeTriggerManager.unregisterTask(taskId);
|
|
565
|
-
}
|
|
566
|
-
if (this.watcherManager) {
|
|
567
|
-
this.watcherManager.unwatchTask(taskId);
|
|
568
|
-
}
|
|
569
|
-
if (this.jobs.has(taskId)) {
|
|
570
|
-
this.jobs.get(taskId).stop();
|
|
571
|
-
this.jobs.delete(taskId);
|
|
572
|
-
}
|
|
573
|
-
if (this.timers.has(taskId)) {
|
|
574
|
-
clearTimeout(this.timers.get(taskId));
|
|
575
|
-
this.timers.delete(taskId);
|
|
576
|
-
}
|
|
278
|
+
return handleClearScheduled(this, taskId);
|
|
577
279
|
}
|
|
578
|
-
|
|
579
280
|
scheduleOneShot(task, parsed) {
|
|
580
281
|
task.oneShot = true;
|
|
581
282
|
task.targetTimestamp = parsed.targetTimestamp;
|
|
@@ -769,6 +470,10 @@ export class TaskScheduler {
|
|
|
769
470
|
|
|
770
471
|
async runTask(taskId, options = {}) {
|
|
771
472
|
if (this.isStopped) return null;
|
|
473
|
+
if (this.store && (this.store.isReadOnly?.() || this.store.hasLoadFailed?.())) {
|
|
474
|
+
logger.warn(`[dsh-cron] skipping task run ${taskId}: store is in read-only recovery mode`);
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
772
477
|
return executeTask(this, taskId, options);
|
|
773
478
|
}
|
|
774
479
|
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { gzipSync, gunzipSync } from 'node:zlib';
|
|
4
|
+
import { logger } from '../logger.js';
|
|
5
|
+
import { bestEffort } from '../best-effort.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Append rotated overflow history entries into tasks-history-archive.json (#134, #379, GH #17).
|
|
9
|
+
*/
|
|
10
|
+
export function archiveHistoryRuns(store, id, overflowRuns) {
|
|
11
|
+
if (!Array.isArray(overflowRuns) || overflowRuns.length === 0) return;
|
|
12
|
+
try {
|
|
13
|
+
const dir = path.dirname(store.filePath);
|
|
14
|
+
const archivePath = path.join(dir, 'tasks-history-archive.json');
|
|
15
|
+
let archive = {};
|
|
16
|
+
if (fs.existsSync(archivePath)) {
|
|
17
|
+
archive = bestEffort('parse-history-archive', () => JSON.parse(fs.readFileSync(archivePath, 'utf-8'))) || {};
|
|
18
|
+
}
|
|
19
|
+
if (!Array.isArray(archive[id])) archive[id] = [];
|
|
20
|
+
|
|
21
|
+
// Strip full raw diff payload from archived overflow runs to prevent runaway JSON growth (#379, GH #17)
|
|
22
|
+
const sanitizedOverflow = overflowRuns.map((r) => {
|
|
23
|
+
if (!r || !r.gitDiff) return r;
|
|
24
|
+
const { diff, ...gitDiffMeta } = r.gitDiff;
|
|
25
|
+
return {
|
|
26
|
+
...r,
|
|
27
|
+
gitDiff: {
|
|
28
|
+
...gitDiffMeta,
|
|
29
|
+
diffExcerpt: diff ? diff.slice(0, 500) : '',
|
|
30
|
+
hasChanges: Boolean(gitDiffMeta.hasChanges),
|
|
31
|
+
archived: true,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
archive[id].push(...sanitizedOverflow);
|
|
37
|
+
if (archive[id].length > 1000) {
|
|
38
|
+
archive[id] = archive[id].slice(-1000);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Enforce total archive size budget (max 15MB) across tasks (#379, GH #17)
|
|
42
|
+
const MAX_ARCHIVE_BYTES = 15 * 1024 * 1024;
|
|
43
|
+
let serialized = JSON.stringify(archive);
|
|
44
|
+
if (serialized.length > MAX_ARCHIVE_BYTES) {
|
|
45
|
+
for (const taskId of Object.keys(archive)) {
|
|
46
|
+
if (Array.isArray(archive[taskId]) && archive[taskId].length > 100) {
|
|
47
|
+
archive[taskId] = archive[taskId].slice(-100);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
serialized = JSON.stringify(archive);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Write atomically with tmp file using compact JSON (avoiding mega-lines and null, 2 serialization bloat)
|
|
54
|
+
const rand = Math.random().toString(36).slice(2, 8);
|
|
55
|
+
const tmp = `${archivePath}.tmp.${process.pid}_${Date.now()}_${rand}`;
|
|
56
|
+
fs.writeFileSync(tmp, serialized, { encoding: 'utf-8', mode: 0o600 });
|
|
57
|
+
bestEffort('chmod-tmp', () => fs.chmodSync(tmp, 0o600));
|
|
58
|
+
fs.renameSync(tmp, archivePath);
|
|
59
|
+
bestEffort('chmod-archive', () => fs.chmodSync(archivePath, 0o600));
|
|
60
|
+
|
|
61
|
+
// Partition runs into monthly buckets for compressed archives (#391)
|
|
62
|
+
const monthlyBuckets = new Map();
|
|
63
|
+
for (const run of sanitizedOverflow) {
|
|
64
|
+
const at = run.at || run.timestamp || run.startedAt || Date.now();
|
|
65
|
+
const date = new Date(at);
|
|
66
|
+
const monthKey = !isNaN(date.getTime())
|
|
67
|
+
? `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, '0')}`
|
|
68
|
+
: new Date().toISOString().slice(0, 7);
|
|
69
|
+
if (!monthlyBuckets.has(monthKey)) {
|
|
70
|
+
monthlyBuckets.set(monthKey, []);
|
|
71
|
+
}
|
|
72
|
+
monthlyBuckets.get(monthKey).push(run);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (const [monthKey, runsForMonth] of monthlyBuckets.entries()) {
|
|
76
|
+
const gzPath = path.join(dir, `tasks-history-${monthKey}.json.gz`);
|
|
77
|
+
let monthArchive = {};
|
|
78
|
+
if (fs.existsSync(gzPath)) {
|
|
79
|
+
try {
|
|
80
|
+
const rawGz = fs.readFileSync(gzPath);
|
|
81
|
+
const decompressed = gunzipSync(rawGz).toString('utf-8');
|
|
82
|
+
monthArchive = JSON.parse(decompressed) || {};
|
|
83
|
+
} catch (gzReadErr) {
|
|
84
|
+
logger.warn(`[dsh-cron] failed to read existing gzip archive for ${monthKey}:`, gzReadErr.message);
|
|
85
|
+
monthArchive = {};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!Array.isArray(monthArchive[id])) {
|
|
89
|
+
monthArchive[id] = [];
|
|
90
|
+
}
|
|
91
|
+
monthArchive[id].push(...runsForMonth);
|
|
92
|
+
const compressed = gzipSync(Buffer.from(JSON.stringify(monthArchive), 'utf-8'), { level: 9 });
|
|
93
|
+
const randGz = Math.random().toString(36).slice(2, 8);
|
|
94
|
+
const tmpGz = `${gzPath}.tmp.${process.pid}_${Date.now()}_${randGz}`;
|
|
95
|
+
fs.writeFileSync(tmpGz, compressed, { mode: 0o600 });
|
|
96
|
+
bestEffort('chmod-tmp-gz', () => fs.chmodSync(tmpGz, 0o600));
|
|
97
|
+
fs.renameSync(tmpGz, gzPath);
|
|
98
|
+
bestEffort('chmod-archive-gz', () => fs.chmodSync(gzPath, 0o600));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
pruneOldMonthlyArchives(dir, 6);
|
|
102
|
+
} catch (archiveErr) {
|
|
103
|
+
logger.error('[dsh-cron] history archive error:', archiveErr.message);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function listMonthlyArchives(store) {
|
|
108
|
+
try {
|
|
109
|
+
const dir = path.dirname(store.filePath);
|
|
110
|
+
if (!fs.existsSync(dir)) return [];
|
|
111
|
+
const files = fs.readdirSync(dir);
|
|
112
|
+
const archives = [];
|
|
113
|
+
for (const file of files) {
|
|
114
|
+
const match = file.match(/^tasks-history-(\d{4}-\d{2})\.json\.gz$/);
|
|
115
|
+
if (match) {
|
|
116
|
+
const month = match[1];
|
|
117
|
+
const fullPath = path.join(dir, file);
|
|
118
|
+
try {
|
|
119
|
+
const stat = fs.statSync(fullPath);
|
|
120
|
+
archives.push({
|
|
121
|
+
month,
|
|
122
|
+
filename: file,
|
|
123
|
+
sizeBytes: stat.size,
|
|
124
|
+
mtime: stat.mtime.toISOString(),
|
|
125
|
+
path: fullPath,
|
|
126
|
+
});
|
|
127
|
+
} catch (_err) {
|
|
128
|
+
// ignore unreadable stat
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return archives.sort((a, b) => b.month.localeCompare(a.month));
|
|
133
|
+
} catch (err) {
|
|
134
|
+
logger.error('[dsh-cron] listMonthlyArchives error:', err.message);
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function getMonthlyArchive(store, month) {
|
|
140
|
+
if (!month || !/^\d{4}-\d{2}$/.test(month)) return null;
|
|
141
|
+
try {
|
|
142
|
+
const dir = path.dirname(store.filePath);
|
|
143
|
+
const gzPath = path.join(dir, `tasks-history-${month}.json.gz`);
|
|
144
|
+
if (!fs.existsSync(gzPath)) return null;
|
|
145
|
+
const rawGz = fs.readFileSync(gzPath);
|
|
146
|
+
const decompressed = gunzipSync(rawGz).toString('utf-8');
|
|
147
|
+
return JSON.parse(decompressed);
|
|
148
|
+
} catch (err) {
|
|
149
|
+
logger.error(`[dsh-cron] getMonthlyArchive error for ${month}:`, err.message);
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function pruneOldMonthlyArchives(dir, maxMonths = 6) {
|
|
155
|
+
try {
|
|
156
|
+
if (!fs.existsSync(dir) || maxMonths < 0) return;
|
|
157
|
+
const files = fs.readdirSync(dir);
|
|
158
|
+
const monthlyFiles = [];
|
|
159
|
+
for (const file of files) {
|
|
160
|
+
const match = file.match(/^tasks-history-(\d{4}-\d{2})\.json\.gz$/);
|
|
161
|
+
if (match) {
|
|
162
|
+
monthlyFiles.push({ month: match[1], file, fullPath: path.join(dir, file) });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
monthlyFiles.sort((a, b) => b.month.localeCompare(a.month));
|
|
166
|
+
if (monthlyFiles.length > maxMonths) {
|
|
167
|
+
const toDelete = monthlyFiles.slice(maxMonths);
|
|
168
|
+
for (const item of toDelete) {
|
|
169
|
+
bestEffort('delete-old-archive', () => fs.unlinkSync(item.fullPath));
|
|
170
|
+
logger.info(`[dsh-cron] pruned old monthly history archive: ${item.file}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} catch (err) {
|
|
174
|
+
bestEffort('log-prune-err', () => logger.debug?.(`[dsh-cron] prune old monthly archives error: ${err.message}`));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function getArchivedRuns(store, id, { limit = 50, offset = 0, search = '' } = {}) {
|
|
179
|
+
try {
|
|
180
|
+
const dir = path.dirname(store.filePath);
|
|
181
|
+
const archivePath = path.join(dir, 'tasks-history-archive.json');
|
|
182
|
+
if (!fs.existsSync(archivePath)) return { runs: [], total: 0 };
|
|
183
|
+
const archive = JSON.parse(fs.readFileSync(archivePath, 'utf-8')) || {};
|
|
184
|
+
let runs = Array.isArray(archive[id]) ? [...archive[id]].sort((a, b) => ((b.at || b.timestamp || b.startedAt || 0) - (a.at || a.timestamp || a.startedAt || 0))) : [];
|
|
185
|
+
if (search && search.trim()) {
|
|
186
|
+
const q = search.toLowerCase();
|
|
187
|
+
runs = runs.filter(r => (r.output && r.output.toLowerCase().includes(q)) || (r.error && r.error.toLowerCase().includes(q)));
|
|
188
|
+
}
|
|
189
|
+
const total = runs.length;
|
|
190
|
+
const paged = runs.slice(offset, offset + limit);
|
|
191
|
+
return { runs: paged, total };
|
|
192
|
+
} catch (err) {
|
|
193
|
+
return { runs: [], total: 0, error: err.message };
|
|
194
|
+
}
|
|
195
|
+
}
|