@goodandready/dsh-cron 0.3.31 → 0.3.33
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/README.md +10 -0
- package/README.ru.md +10 -0
- package/README.zh.md +10 -0
- package/lib/client-src/10-locales.js +26 -0
- package/lib/client-src/50-cron-screen-state.js +7 -0
- package/lib/client-src/53-cron-screen-modal-actions.js +20 -0
- package/lib/client-src/62-modal-task-form-tail.js +69 -0
- package/lib/client.js +122 -0
- package/lib/cron-tool.js +17 -0
- package/lib/input-hash.js +106 -0
- package/lib/locales.json +16 -2
- package/lib/prompt-interpolation.js +15 -0
- package/lib/scheduler/scheduler-completion.js +10 -34
- package/lib/scheduler/scheduler-execution.js +50 -0
- package/lib/scheduler/scheduler.js +63 -0
- package/lib/scope-locks.js +189 -0
- package/lib/store/task-schema.js +5 -0
- package/lib/store/task-transfer.js +5 -0
- package/lib/task-create.js +8 -0
- package/lib/task-patch.js +18 -0
- package/lib/wake-triggers.js +362 -0
- package/package.json +1 -1
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { logger } from './logger.js';
|
|
4
|
+
import { bestEffort } from './best-effort.js';
|
|
5
|
+
|
|
6
|
+
const DISALLOWED_ROOTS = [
|
|
7
|
+
'/',
|
|
8
|
+
'/proc',
|
|
9
|
+
'/sys',
|
|
10
|
+
'/dev',
|
|
11
|
+
'/etc',
|
|
12
|
+
'/boot',
|
|
13
|
+
'/run',
|
|
14
|
+
'/var/run',
|
|
15
|
+
'c:\\',
|
|
16
|
+
'c:\\windows',
|
|
17
|
+
'c:\\windows\\system32',
|
|
18
|
+
'c:\\program files',
|
|
19
|
+
'c:\\program files (x86)',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Validates that a path does not target sensitive system locations or escape root.
|
|
24
|
+
*/
|
|
25
|
+
export function isSafeWakePath(targetPath) {
|
|
26
|
+
if (!targetPath || typeof targetPath !== 'string') {
|
|
27
|
+
return { safe: false, error: 'Path must be a non-empty string' };
|
|
28
|
+
}
|
|
29
|
+
const trimmed = targetPath.trim();
|
|
30
|
+
if (trimmed.includes('\0')) {
|
|
31
|
+
return { safe: false, error: 'Path must not contain null bytes' };
|
|
32
|
+
}
|
|
33
|
+
const resolved = path.resolve(trimmed);
|
|
34
|
+
const lower = resolved.toLowerCase();
|
|
35
|
+
|
|
36
|
+
for (const root of DISALLOWED_ROOTS) {
|
|
37
|
+
if (lower === root || lower === root.replace(/\\/g, '/')) {
|
|
38
|
+
return { safe: false, error: `Path "${resolved}" targets a disallowed system root directory` };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (resolved === path.parse(resolved).root) {
|
|
43
|
+
return { safe: false, error: `Path "${resolved}" is a filesystem root` };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { safe: true, resolved };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Normalizes wake trigger configuration. Returns null if invalid or disabled.
|
|
51
|
+
*/
|
|
52
|
+
export function normalizeWakeTrigger(cfg) {
|
|
53
|
+
if (!cfg || typeof cfg !== 'object') return null;
|
|
54
|
+
const enabled = cfg.enabled !== false;
|
|
55
|
+
const type = String(cfg.type || '').trim().toLowerCase();
|
|
56
|
+
|
|
57
|
+
if (!['file_count', 'marker_file', 'process_exit'].includes(type)) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const normalized = {
|
|
62
|
+
enabled,
|
|
63
|
+
type,
|
|
64
|
+
pollIntervalMs: Math.max(20, Number(cfg.pollIntervalMs) || 3000),
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
if (type === 'file_count') {
|
|
68
|
+
normalized.path = String(cfg.path || '').trim();
|
|
69
|
+
normalized.minCount = Math.max(1, parseInt(cfg.minCount, 10) || 1);
|
|
70
|
+
normalized.pattern = String(cfg.pattern || '*').trim() || '*';
|
|
71
|
+
} else if (type === 'marker_file') {
|
|
72
|
+
normalized.markerPath = String(cfg.markerPath || cfg.path || '').trim();
|
|
73
|
+
normalized.consumeMarker = cfg.consumeMarker !== false;
|
|
74
|
+
} else if (type === 'process_exit') {
|
|
75
|
+
if (cfg.pid !== undefined && cfg.pid !== null && cfg.pid !== '') {
|
|
76
|
+
const pidNum = parseInt(cfg.pid, 10);
|
|
77
|
+
if (Number.isInteger(pidNum) && pidNum > 1) {
|
|
78
|
+
normalized.pid = pidNum;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
normalized.pidFile = String(cfg.pidFile || '').trim();
|
|
82
|
+
normalized.consumeMarker = Boolean(cfg.consumeMarker);
|
|
83
|
+
if (!normalized.pid && !normalized.pidFile) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return normalized;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Checks if an OS process is currently running.
|
|
93
|
+
*/
|
|
94
|
+
export function isProcessAlive(pid) {
|
|
95
|
+
if (!Number.isInteger(pid) || pid <= 1) return false;
|
|
96
|
+
try {
|
|
97
|
+
process.kill(pid, 0);
|
|
98
|
+
return true;
|
|
99
|
+
} catch (err) {
|
|
100
|
+
return err.code === 'EPERM';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Matches a filename against a glob pattern (wildcards * and ?, comma-separated).
|
|
106
|
+
*/
|
|
107
|
+
export function matchPattern(filename, pattern) {
|
|
108
|
+
if (!pattern || pattern.trim() === '' || pattern === '*') return true;
|
|
109
|
+
const parts = pattern.split(',').map((p) => p.trim()).filter(Boolean);
|
|
110
|
+
if (!parts.length) return true;
|
|
111
|
+
|
|
112
|
+
return parts.some((p) => {
|
|
113
|
+
const regexStr = '^' + p.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.') + '$';
|
|
114
|
+
try {
|
|
115
|
+
return new RegExp(regexStr, 'i').test(filename);
|
|
116
|
+
} catch (_err) {
|
|
117
|
+
void _err;
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Counts non-directory files in a directory matching pattern.
|
|
125
|
+
*/
|
|
126
|
+
export function countMatchingFiles(dir, pattern = '*') {
|
|
127
|
+
try {
|
|
128
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
129
|
+
const matched = [];
|
|
130
|
+
for (const entry of entries) {
|
|
131
|
+
if (entry.isFile() || entry.isSymbolicLink()) {
|
|
132
|
+
if (matchPattern(entry.name, pattern)) {
|
|
133
|
+
matched.push(entry.name);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return { count: matched.length, files: matched };
|
|
138
|
+
} catch (_err) {
|
|
139
|
+
void _err;
|
|
140
|
+
return { count: 0, files: [] };
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* WakeTriggerManager manages background observation and event-driven waking of tasks (#410).
|
|
146
|
+
*/
|
|
147
|
+
export class WakeTriggerManager {
|
|
148
|
+
constructor(scheduler) {
|
|
149
|
+
this.scheduler = scheduler;
|
|
150
|
+
this.watchers = new Map(); // taskId -> { timer, config, state }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
syncTasks(tasks) {
|
|
154
|
+
if (!Array.isArray(tasks)) return;
|
|
155
|
+
const activeTaskIds = new Set();
|
|
156
|
+
for (const task of tasks) {
|
|
157
|
+
if (task && task.id && task.status === 'active' && task.wakeTrigger) {
|
|
158
|
+
const norm = normalizeWakeTrigger(task.wakeTrigger);
|
|
159
|
+
if (norm && norm.enabled) {
|
|
160
|
+
activeTaskIds.add(task.id);
|
|
161
|
+
this.registerTask(task);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Stop watchers for tasks that are no longer active or have no wake trigger
|
|
167
|
+
for (const taskId of this.watchers.keys()) {
|
|
168
|
+
if (!activeTaskIds.has(taskId)) {
|
|
169
|
+
this.unregisterTask(taskId);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
registerTask(task) {
|
|
175
|
+
if (!task || !task.id) return;
|
|
176
|
+
const norm = normalizeWakeTrigger(task.wakeTrigger);
|
|
177
|
+
|
|
178
|
+
if (!norm || !norm.enabled || task.status !== 'active') {
|
|
179
|
+
this.unregisterTask(task.id);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const current = this.watchers.get(task.id);
|
|
184
|
+
const configSig = JSON.stringify(norm);
|
|
185
|
+
|
|
186
|
+
if (current && current.sig === configSig) {
|
|
187
|
+
return; // Already registered with same config
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this.unregisterTask(task.id);
|
|
191
|
+
|
|
192
|
+
const state = {
|
|
193
|
+
triggered: false,
|
|
194
|
+
lastCount: 0,
|
|
195
|
+
processSeenAlive: false,
|
|
196
|
+
monitoredPid: norm.pid || null,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const runCheck = async () => {
|
|
200
|
+
if (!this.scheduler || this.scheduler.isStopped) return;
|
|
201
|
+
try {
|
|
202
|
+
if (norm.type === 'file_count') {
|
|
203
|
+
await this._checkFileCount(task, norm, state);
|
|
204
|
+
} else if (norm.type === 'marker_file') {
|
|
205
|
+
await this._checkMarkerFile(task, norm, state);
|
|
206
|
+
} else if (norm.type === 'process_exit') {
|
|
207
|
+
await this._checkProcessExit(task, norm, state);
|
|
208
|
+
}
|
|
209
|
+
} catch (err) {
|
|
210
|
+
logger.error(`[dsh-cron] Wake trigger check failed for task "${task.title}" (${task.id}): ${err.message}`);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const timer = setInterval(runCheck, norm.pollIntervalMs);
|
|
215
|
+
if (timer.unref) timer.unref();
|
|
216
|
+
|
|
217
|
+
this.watchers.set(task.id, {
|
|
218
|
+
timer,
|
|
219
|
+
sig: configSig,
|
|
220
|
+
config: norm,
|
|
221
|
+
state,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Run first check promptly
|
|
225
|
+
setImmediate(runCheck);
|
|
226
|
+
logger.info(`[dsh-cron] Wake trigger (${norm.type}) registered for task "${task.title}" (${task.id})`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
unregisterTask(taskId) {
|
|
230
|
+
const entry = this.watchers.get(taskId);
|
|
231
|
+
if (entry) {
|
|
232
|
+
if (entry.timer) clearInterval(entry.timer);
|
|
233
|
+
this.watchers.delete(taskId);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
stopAll() {
|
|
238
|
+
for (const entry of this.watchers.values()) {
|
|
239
|
+
if (entry.timer) clearInterval(entry.timer);
|
|
240
|
+
}
|
|
241
|
+
this.watchers.clear();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async _checkFileCount(task, cfg, state) {
|
|
245
|
+
const check = isSafeWakePath(cfg.path);
|
|
246
|
+
if (!check.safe) return;
|
|
247
|
+
if (!fs.existsSync(check.resolved)) return;
|
|
248
|
+
|
|
249
|
+
const { count, files } = countMatchingFiles(check.resolved, cfg.pattern);
|
|
250
|
+
|
|
251
|
+
if (count >= cfg.minCount) {
|
|
252
|
+
// Trigger if not triggered yet, or if new files arrived since last trigger
|
|
253
|
+
if (!state.triggered || count > state.lastCount) {
|
|
254
|
+
state.triggered = true;
|
|
255
|
+
state.lastCount = count;
|
|
256
|
+
logger.info(`[dsh-cron] Wake trigger file_count (${count} >= ${cfg.minCount}) fired for task "${task.title}" (${task.id})`);
|
|
257
|
+
await this._triggerTask(task, {
|
|
258
|
+
type: 'file_count',
|
|
259
|
+
path: check.resolved,
|
|
260
|
+
fileCount: count,
|
|
261
|
+
minCount: cfg.minCount,
|
|
262
|
+
pattern: cfg.pattern,
|
|
263
|
+
matchedFiles: files,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
} else {
|
|
267
|
+
// Reset trigger state when file count drops below threshold
|
|
268
|
+
state.triggered = false;
|
|
269
|
+
state.lastCount = count;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async _checkMarkerFile(task, cfg, state) {
|
|
274
|
+
const check = isSafeWakePath(cfg.markerPath);
|
|
275
|
+
if (!check.safe) return;
|
|
276
|
+
if (!fs.existsSync(check.resolved)) {
|
|
277
|
+
state.triggered = false;
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (state.triggered) return; // Wait for marker to disappear / re-appear
|
|
282
|
+
|
|
283
|
+
state.triggered = true;
|
|
284
|
+
|
|
285
|
+
if (cfg.consumeMarker) {
|
|
286
|
+
try {
|
|
287
|
+
fs.unlinkSync(check.resolved);
|
|
288
|
+
} catch (_err) {
|
|
289
|
+
void _err;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
logger.info(`[dsh-cron] Wake trigger marker_file (${check.resolved}) fired for task "${task.title}" (${task.id})`);
|
|
294
|
+
await this._triggerTask(task, {
|
|
295
|
+
type: 'marker_file',
|
|
296
|
+
markerPath: check.resolved,
|
|
297
|
+
consumed: Boolean(cfg.consumeMarker),
|
|
298
|
+
triggeredAt: Date.now(),
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async _checkProcessExit(task, cfg, state) {
|
|
303
|
+
// If tracking via pidFile, attempt to read PID
|
|
304
|
+
if (cfg.pidFile) {
|
|
305
|
+
const check = isSafeWakePath(cfg.pidFile);
|
|
306
|
+
if (check.safe && fs.existsSync(check.resolved)) {
|
|
307
|
+
try {
|
|
308
|
+
const content = fs.readFileSync(check.resolved, 'utf8').trim();
|
|
309
|
+
const parsed = parseInt(content, 10);
|
|
310
|
+
if (Number.isInteger(parsed) && parsed > 1) {
|
|
311
|
+
state.monitoredPid = parsed;
|
|
312
|
+
}
|
|
313
|
+
} catch (_err) {
|
|
314
|
+
void _err;
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
// pidFile does not exist yet or was removed
|
|
318
|
+
if (!state.monitoredPid) return;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (!state.monitoredPid) return;
|
|
323
|
+
|
|
324
|
+
const alive = isProcessAlive(state.monitoredPid);
|
|
325
|
+
|
|
326
|
+
if (alive) {
|
|
327
|
+
state.processSeenAlive = true;
|
|
328
|
+
} else if (state.processSeenAlive && !alive) {
|
|
329
|
+
// Process was alive and has now exited!
|
|
330
|
+
const exitedPid = state.monitoredPid;
|
|
331
|
+
state.processSeenAlive = false;
|
|
332
|
+
state.monitoredPid = null;
|
|
333
|
+
|
|
334
|
+
if (cfg.consumeMarker && cfg.pidFile) {
|
|
335
|
+
const check = isSafeWakePath(cfg.pidFile);
|
|
336
|
+
if (check.safe && fs.existsSync(check.resolved)) {
|
|
337
|
+
bestEffort('unlink-pid-file', () => fs.unlinkSync(check.resolved));
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
logger.info(`[dsh-cron] Wake trigger process_exit (PID ${exitedPid}) fired for task "${task.title}" (${task.id})`);
|
|
342
|
+
await this._triggerTask(task, {
|
|
343
|
+
type: 'process_exit',
|
|
344
|
+
pid: exitedPid,
|
|
345
|
+
pidFile: cfg.pidFile || '',
|
|
346
|
+
exitTime: Date.now(),
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async _triggerTask(task, wakeContext) {
|
|
352
|
+
if (!this.scheduler || this.scheduler.isStopped) return;
|
|
353
|
+
try {
|
|
354
|
+
await this.scheduler.runTask(task.id, {
|
|
355
|
+
wakeTrigger: wakeContext,
|
|
356
|
+
trigger: `wakeTrigger:${wakeContext.type}`,
|
|
357
|
+
});
|
|
358
|
+
} catch (err) {
|
|
359
|
+
logger.error(`[dsh-cron] Wake trigger execution failed for task ${task.id}: ${err.message}`);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-cron",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.33",
|
|
4
4
|
"description": "Background automation runner for DSH: isolated agent runs, script/HTTP/SSH/Docker runtimes, cost guard, notifications, heartbeats.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|