@celljs/ta-cron 0.0.0-stage → 4.8.0
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.en.md +48 -0
- package/README.md +47 -2
- package/extension/index.mjs +856 -0
- package/host/index.mjs +877 -0
- package/package.json +44 -4
- package/web/index.js +1985 -0
- package/web-src/commands.spec.ts +55 -0
- package/web-src/commands.ts +259 -0
- package/web-src/cron-expression.spec.ts +41 -0
- package/web-src/cron-expression.ts +150 -0
- package/web-src/cron-protocol.ts +151 -0
- package/web-src/editor.tsx +249 -0
- package/web-src/extension.ts +187 -0
- package/web-src/form-util.spec.ts +33 -0
- package/web-src/form-util.ts +141 -0
- package/web-src/host.ts +12 -0
- package/web-src/i18n.ts +175 -0
- package/web-src/index.tsx +35 -0
- package/web-src/model-picker.spec.ts +32 -0
- package/web-src/model-picker.tsx +227 -0
- package/web-src/notifier.ts +47 -0
- package/web-src/page.ts +126 -0
- package/web-src/react.ts +11 -0
- package/web-src/reply.tsx +80 -0
- package/web-src/schedule-util.spec.ts +48 -0
- package/web-src/schedule-util.ts +233 -0
- package/web-src/scheduler.spec.ts +254 -0
- package/web-src/scheduler.ts +380 -0
- package/web-src/server-texts.spec.ts +10 -0
- package/web-src/server-texts.ts +18 -0
- package/web-src/style.ts +133 -0
- package/web-src/task-store.ts +67 -0
- package/web-src/task-util.ts +174 -0
- package/web-src/tasks-view.tsx +279 -0
- package/web-src/tool.ts +135 -0
package/host/index.mjs
ADDED
|
@@ -0,0 +1,877 @@
|
|
|
1
|
+
// web-src/cron-protocol.ts
|
|
2
|
+
var NAME = "@celljs/ta-cron";
|
|
3
|
+
var RUN_MARK = "<ta-cron-run>";
|
|
4
|
+
var Keys;
|
|
5
|
+
((Keys) => {
|
|
6
|
+
Keys.TASKS = "tasks";
|
|
7
|
+
Keys.LATEST = "latest";
|
|
8
|
+
function runs(taskId) {
|
|
9
|
+
return `runs/${taskId}`;
|
|
10
|
+
}
|
|
11
|
+
Keys.runs = runs;
|
|
12
|
+
})(Keys ||= {});
|
|
13
|
+
var Limits;
|
|
14
|
+
((Limits) => {
|
|
15
|
+
Limits.RUNS_KEPT = 20;
|
|
16
|
+
Limits.SUMMARY_MAX = 4000;
|
|
17
|
+
Limits.MIN_INTERVAL_MS = 60000;
|
|
18
|
+
Limits.RUN_TIMEOUT_MS = 30 * 60000;
|
|
19
|
+
Limits.GRACE_MS = 5 * 60000;
|
|
20
|
+
Limits.PARALLEL_RUNS = 3;
|
|
21
|
+
})(Limits ||= {});
|
|
22
|
+
|
|
23
|
+
// web-src/cron-expression.ts
|
|
24
|
+
var FIELDS = [
|
|
25
|
+
{ name: "minute", min: 0, max: 59 },
|
|
26
|
+
{ name: "hour", min: 0, max: 23 },
|
|
27
|
+
{ name: "day of month", min: 1, max: 31 },
|
|
28
|
+
{ name: "month", min: 1, max: 12, names: ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"] },
|
|
29
|
+
{ name: "day of week", min: 0, max: 7, names: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] }
|
|
30
|
+
];
|
|
31
|
+
var NICKNAMES = {
|
|
32
|
+
"@yearly": "0 0 1 1 *",
|
|
33
|
+
"@annually": "0 0 1 1 *",
|
|
34
|
+
"@monthly": "0 0 1 * *",
|
|
35
|
+
"@weekly": "0 0 * * 0",
|
|
36
|
+
"@daily": "0 0 * * *",
|
|
37
|
+
"@midnight": "0 0 * * *",
|
|
38
|
+
"@hourly": "0 * * * *"
|
|
39
|
+
};
|
|
40
|
+
var MAX_STEPS = 200000;
|
|
41
|
+
|
|
42
|
+
class CronExpression {
|
|
43
|
+
source;
|
|
44
|
+
minutes;
|
|
45
|
+
hours;
|
|
46
|
+
days;
|
|
47
|
+
months;
|
|
48
|
+
weekdays;
|
|
49
|
+
anyDay;
|
|
50
|
+
anyWeekday;
|
|
51
|
+
constructor(source, minutes, hours, days, months, weekdays, anyDay, anyWeekday) {
|
|
52
|
+
this.source = source;
|
|
53
|
+
this.minutes = minutes;
|
|
54
|
+
this.hours = hours;
|
|
55
|
+
this.days = days;
|
|
56
|
+
this.months = months;
|
|
57
|
+
this.weekdays = weekdays;
|
|
58
|
+
this.anyDay = anyDay;
|
|
59
|
+
this.anyWeekday = anyWeekday;
|
|
60
|
+
}
|
|
61
|
+
static parse(text) {
|
|
62
|
+
const source = text.trim().replace(/\s+/g, " ");
|
|
63
|
+
const expanded = NICKNAMES[source.toLowerCase()] ?? source;
|
|
64
|
+
const parts = expanded.split(" ");
|
|
65
|
+
if (parts.length !== 5) {
|
|
66
|
+
throw new Error(`A cron expression has five fields (minute hour day month weekday), or is one of ${Object.keys(NICKNAMES).join(", ")}: ${JSON.stringify(text)} has ${parts.length}.`);
|
|
67
|
+
}
|
|
68
|
+
const sets = parts.map((part, index) => CronExpression.field(part, FIELDS[index]));
|
|
69
|
+
const weekdays = sets[4];
|
|
70
|
+
if (weekdays.delete(7)) {
|
|
71
|
+
weekdays.add(0);
|
|
72
|
+
}
|
|
73
|
+
return new CronExpression(source, sets[0], sets[1], sets[2], sets[3], weekdays, parts[2] === "*", parts[4] === "*");
|
|
74
|
+
}
|
|
75
|
+
static isValid(text) {
|
|
76
|
+
try {
|
|
77
|
+
CronExpression.parse(text);
|
|
78
|
+
return true;
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
static field(text, field) {
|
|
84
|
+
const values = new Set;
|
|
85
|
+
for (const item of text.toLowerCase().split(",")) {
|
|
86
|
+
const [range, stepText, extra] = item.split("/");
|
|
87
|
+
const step = stepText === undefined ? 1 : Number(stepText);
|
|
88
|
+
if (extra !== undefined || !Number.isInteger(step) || step < 1) {
|
|
89
|
+
throw new Error(`${JSON.stringify(item)} is not a valid ${field.name}: a step is a whole number from 1.`);
|
|
90
|
+
}
|
|
91
|
+
let from;
|
|
92
|
+
let to;
|
|
93
|
+
if (range === "*") {
|
|
94
|
+
from = field.min;
|
|
95
|
+
to = field.max;
|
|
96
|
+
} else {
|
|
97
|
+
const [first, last, more] = range.split("-");
|
|
98
|
+
if (more !== undefined) {
|
|
99
|
+
throw new Error(`${JSON.stringify(item)} is not a valid ${field.name}.`);
|
|
100
|
+
}
|
|
101
|
+
from = CronExpression.value(first, field);
|
|
102
|
+
to = last === undefined ? stepText === undefined ? from : field.max : CronExpression.value(last, field);
|
|
103
|
+
}
|
|
104
|
+
if (from > to) {
|
|
105
|
+
throw new Error(`${JSON.stringify(item)} is not a valid ${field.name}: the range goes backwards.`);
|
|
106
|
+
}
|
|
107
|
+
for (let value = from;value <= to; value += step) {
|
|
108
|
+
values.add(value);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return values;
|
|
112
|
+
}
|
|
113
|
+
static value(text, field) {
|
|
114
|
+
const named = field.names?.indexOf(text) ?? -1;
|
|
115
|
+
const value = named >= 0 ? named + (field.min === 1 ? 1 : 0) : /^\d+$/.test(text) ? Number(text) : Number.NaN;
|
|
116
|
+
if (!(value >= field.min && value <= field.max)) {
|
|
117
|
+
throw new Error(`${JSON.stringify(text)} is not a valid ${field.name}: from ${field.min} to ${field.max}.`);
|
|
118
|
+
}
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
next(after) {
|
|
122
|
+
const time = new Date(after.getTime());
|
|
123
|
+
time.setSeconds(0, 0);
|
|
124
|
+
time.setMinutes(time.getMinutes() + 1);
|
|
125
|
+
for (let step = 0;step < MAX_STEPS; step++) {
|
|
126
|
+
if (!this.months.has(time.getMonth() + 1)) {
|
|
127
|
+
time.setMonth(time.getMonth() + 1, 1);
|
|
128
|
+
time.setHours(0, 0, 0, 0);
|
|
129
|
+
} else if (!this.matchesDay(time)) {
|
|
130
|
+
time.setDate(time.getDate() + 1);
|
|
131
|
+
time.setHours(0, 0, 0, 0);
|
|
132
|
+
} else if (!this.hours.has(time.getHours())) {
|
|
133
|
+
time.setHours(time.getHours() + 1, 0, 0, 0);
|
|
134
|
+
} else if (!this.minutes.has(time.getMinutes())) {
|
|
135
|
+
time.setMinutes(time.getMinutes() + 1, 0, 0);
|
|
136
|
+
} else {
|
|
137
|
+
return time;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
matchesDay(time) {
|
|
143
|
+
const day = this.days.has(time.getDate());
|
|
144
|
+
const weekday = this.weekdays.has(time.getDay());
|
|
145
|
+
if (this.anyDay || this.anyWeekday) {
|
|
146
|
+
return day && weekday;
|
|
147
|
+
}
|
|
148
|
+
return day || weekday;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// web-src/schedule-util.ts
|
|
153
|
+
var THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
154
|
+
var NOTIFY_LEVELS = ["always", "error", "never"];
|
|
155
|
+
var UNITS = {
|
|
156
|
+
s: 1000,
|
|
157
|
+
sec: 1000,
|
|
158
|
+
m: 60000,
|
|
159
|
+
min: 60000,
|
|
160
|
+
h: 3600000,
|
|
161
|
+
hr: 3600000,
|
|
162
|
+
d: 86400000,
|
|
163
|
+
w: 7 * 86400000
|
|
164
|
+
};
|
|
165
|
+
var Duration;
|
|
166
|
+
((Duration) => {
|
|
167
|
+
const PART = /(\d+)\s*(sec|min|hr|s|m|h|d|w)/gy;
|
|
168
|
+
function parse(text) {
|
|
169
|
+
const source = text.trim().toLowerCase();
|
|
170
|
+
if (!source) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
PART.lastIndex = 0;
|
|
174
|
+
let total = 0;
|
|
175
|
+
let match;
|
|
176
|
+
let end = 0;
|
|
177
|
+
while (match = PART.exec(source)) {
|
|
178
|
+
total += Number(match[1]) * UNITS[match[2]];
|
|
179
|
+
end = PART.lastIndex;
|
|
180
|
+
}
|
|
181
|
+
return end === source.length && total > 0 ? total : undefined;
|
|
182
|
+
}
|
|
183
|
+
Duration.parse = parse;
|
|
184
|
+
function format(ms) {
|
|
185
|
+
const parts = [];
|
|
186
|
+
let rest = Math.round(ms / 1000);
|
|
187
|
+
for (const [unit, size] of [
|
|
188
|
+
["d", 86400],
|
|
189
|
+
["h", 3600],
|
|
190
|
+
["m", 60],
|
|
191
|
+
["s", 1]
|
|
192
|
+
]) {
|
|
193
|
+
if (rest >= size) {
|
|
194
|
+
parts.push(`${Math.floor(rest / size)}${unit}`);
|
|
195
|
+
rest %= size;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return parts.join("") || "0s";
|
|
199
|
+
}
|
|
200
|
+
Duration.format = format;
|
|
201
|
+
})(Duration ||= {});
|
|
202
|
+
var EN_DAYS = ["Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"];
|
|
203
|
+
var ZH_DAYS = ["日", "一", "二", "三", "四", "五", "六"];
|
|
204
|
+
var WORDS = {
|
|
205
|
+
en: {
|
|
206
|
+
once: (time) => `Once, ${time}`,
|
|
207
|
+
every: (amount, unit) => amount === 1 ? `Every ${unit}` : `Every ${amount} ${unit}s`,
|
|
208
|
+
everyRaw: (duration) => `Every ${duration}`,
|
|
209
|
+
daily: (time) => `Daily at ${time}`,
|
|
210
|
+
weekdays: (time) => `Weekdays at ${time}`,
|
|
211
|
+
weekly: (day, time) => `${EN_DAYS[day]} at ${time}`,
|
|
212
|
+
monthly: (day, time) => `Monthly on day ${day} at ${time}`,
|
|
213
|
+
hourly: (minute) => minute === 0 ? "Hourly" : `Hourly at :${pad(minute)}`,
|
|
214
|
+
cron: (expression) => `Cron ${expression}`
|
|
215
|
+
},
|
|
216
|
+
zh: {
|
|
217
|
+
once: (time) => `${time} 运行一次`,
|
|
218
|
+
every: (amount, unit) => `每 ${amount === 1 ? "" : `${amount} `}${{ minute: "分钟", hour: "小时", day: "天" }[unit]}`.replace("每 天", "每天"),
|
|
219
|
+
everyRaw: (duration) => `每 ${duration}`,
|
|
220
|
+
daily: (time) => `每天 ${time}`,
|
|
221
|
+
weekdays: (time) => `工作日 ${time}`,
|
|
222
|
+
weekly: (day, time) => `每周${ZH_DAYS[day]} ${time}`,
|
|
223
|
+
monthly: (day, time) => `每月 ${day} 日 ${time}`,
|
|
224
|
+
hourly: (minute) => minute === 0 ? "每小时" : `每小时第 ${minute} 分`,
|
|
225
|
+
cron: (expression) => `cron ${expression}`
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
function pad(value) {
|
|
229
|
+
return String(value).padStart(2, "0");
|
|
230
|
+
}
|
|
231
|
+
var ScheduleUtil;
|
|
232
|
+
((ScheduleUtil) => {
|
|
233
|
+
function check(value) {
|
|
234
|
+
const schedule = value ?? {};
|
|
235
|
+
switch (schedule.kind) {
|
|
236
|
+
case "once": {
|
|
237
|
+
const at = new Date(String(schedule.at ?? ""));
|
|
238
|
+
if (typeof schedule.at !== "string" || Number.isNaN(at.getTime())) {
|
|
239
|
+
throw new Error("A task that runs once needs `at`, a time such as 2026-11-12T09:00 (local time) or an ISO time.");
|
|
240
|
+
}
|
|
241
|
+
return { kind: "once", at: at.toISOString() };
|
|
242
|
+
}
|
|
243
|
+
case "interval": {
|
|
244
|
+
const every = typeof schedule.every === "string" ? schedule.every.trim() : "";
|
|
245
|
+
const ms = Duration.parse(every);
|
|
246
|
+
if (ms === undefined) {
|
|
247
|
+
throw new Error("An interval needs `every`, a duration such as 30m, 2h or 1d.");
|
|
248
|
+
}
|
|
249
|
+
if (ms < Limits.MIN_INTERVAL_MS) {
|
|
250
|
+
throw new Error(`An interval is at least 1m; ${every} is shorter.`);
|
|
251
|
+
}
|
|
252
|
+
return { kind: "interval", every: Duration.format(ms) };
|
|
253
|
+
}
|
|
254
|
+
case "cron": {
|
|
255
|
+
const expression = typeof schedule.expression === "string" ? schedule.expression : "";
|
|
256
|
+
return { kind: "cron", expression: CronExpression.parse(expression).source };
|
|
257
|
+
}
|
|
258
|
+
default:
|
|
259
|
+
throw new Error('A schedule is { kind: "once", at }, { kind: "interval", every } or { kind: "cron", expression }.');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
ScheduleUtil.check = check;
|
|
263
|
+
function next(schedule, now) {
|
|
264
|
+
switch (schedule.kind) {
|
|
265
|
+
case "once": {
|
|
266
|
+
const at = new Date(schedule.at);
|
|
267
|
+
return at.getTime() > now.getTime() ? at : undefined;
|
|
268
|
+
}
|
|
269
|
+
case "interval":
|
|
270
|
+
return new Date(now.getTime() + (Duration.parse(schedule.every) ?? Limits.MIN_INTERVAL_MS));
|
|
271
|
+
case "cron":
|
|
272
|
+
return CronExpression.parse(schedule.expression).next(now);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
ScheduleUtil.next = next;
|
|
276
|
+
function repeats(schedule) {
|
|
277
|
+
return schedule.kind !== "once";
|
|
278
|
+
}
|
|
279
|
+
ScheduleUtil.repeats = repeats;
|
|
280
|
+
function notifyLevel(task) {
|
|
281
|
+
return task.notify ?? (repeats(task.schedule) ? "error" : "always");
|
|
282
|
+
}
|
|
283
|
+
ScheduleUtil.notifyLevel = notifyLevel;
|
|
284
|
+
function shouldNotify(level, failed) {
|
|
285
|
+
return level === "always" || level === "error" && failed;
|
|
286
|
+
}
|
|
287
|
+
ScheduleUtil.shouldNotify = shouldNotify;
|
|
288
|
+
function clock(time, now = new Date) {
|
|
289
|
+
const day = `${time.getMonth() + 1}/${time.getDate()}`;
|
|
290
|
+
const year = time.getFullYear() === now.getFullYear() ? "" : `${time.getFullYear()}/`;
|
|
291
|
+
return `${year}${day} ${pad(time.getHours())}:${pad(time.getMinutes())}`;
|
|
292
|
+
}
|
|
293
|
+
ScheduleUtil.clock = clock;
|
|
294
|
+
function describe(schedule, language, now = new Date) {
|
|
295
|
+
const words = WORDS[language.startsWith("zh") ? "zh" : "en"];
|
|
296
|
+
switch (schedule.kind) {
|
|
297
|
+
case "once":
|
|
298
|
+
return words.once(clock(new Date(schedule.at), now));
|
|
299
|
+
case "interval":
|
|
300
|
+
return describeInterval(schedule.every, words);
|
|
301
|
+
case "cron":
|
|
302
|
+
return describeCron(schedule.expression, words);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
ScheduleUtil.describe = describe;
|
|
306
|
+
function describeInterval(every, words) {
|
|
307
|
+
const ms = Duration.parse(every) ?? 0;
|
|
308
|
+
for (const [unit, size] of [
|
|
309
|
+
["day", 86400000],
|
|
310
|
+
["hour", 3600000],
|
|
311
|
+
["minute", 60000]
|
|
312
|
+
]) {
|
|
313
|
+
if (ms >= size && ms % size === 0) {
|
|
314
|
+
return words.every(ms / size, unit);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return words.everyRaw(every);
|
|
318
|
+
}
|
|
319
|
+
function describeCron(expression, words) {
|
|
320
|
+
const fields = (NICKNAMES[expression.toLowerCase()] ?? expression).split(" ");
|
|
321
|
+
const [minute, hour, day, month, weekday] = fields;
|
|
322
|
+
const number = (text) => /^\d+$/.test(text) ? Number(text) : undefined;
|
|
323
|
+
const m = number(minute);
|
|
324
|
+
const h = number(hour);
|
|
325
|
+
const time = m !== undefined && h !== undefined ? `${pad(h)}:${pad(m)}` : undefined;
|
|
326
|
+
if (month === "*") {
|
|
327
|
+
if (time && day === "*" && weekday === "*") {
|
|
328
|
+
return words.daily(time);
|
|
329
|
+
}
|
|
330
|
+
if (time && day === "*" && weekday === "1-5") {
|
|
331
|
+
return words.weekdays(time);
|
|
332
|
+
}
|
|
333
|
+
const d = number(weekday);
|
|
334
|
+
if (time && day === "*" && d !== undefined && d <= 7) {
|
|
335
|
+
return words.weekly(d % 7, time);
|
|
336
|
+
}
|
|
337
|
+
const date = number(day);
|
|
338
|
+
if (time && date !== undefined && weekday === "*") {
|
|
339
|
+
return words.monthly(date, time);
|
|
340
|
+
}
|
|
341
|
+
if (m !== undefined && hour === "*" && day === "*" && weekday === "*") {
|
|
342
|
+
return words.hourly(m);
|
|
343
|
+
}
|
|
344
|
+
const everyMinutes = /^\*\/(\d+)$/.exec(minute);
|
|
345
|
+
if (everyMinutes && hour === "*" && day === "*" && weekday === "*") {
|
|
346
|
+
return words.every(Number(everyMinutes[1]), "minute");
|
|
347
|
+
}
|
|
348
|
+
const everyHours = /^\*\/(\d+)$/.exec(hour);
|
|
349
|
+
if (m === 0 && everyHours && day === "*" && weekday === "*") {
|
|
350
|
+
return words.every(Number(everyHours[1]), "hour");
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return words.cron(expression);
|
|
354
|
+
}
|
|
355
|
+
})(ScheduleUtil ||= {});
|
|
356
|
+
|
|
357
|
+
// web-src/task-store.ts
|
|
358
|
+
class TaskStore {
|
|
359
|
+
host;
|
|
360
|
+
writing = Promise.resolve();
|
|
361
|
+
constructor(host) {
|
|
362
|
+
this.host = host;
|
|
363
|
+
}
|
|
364
|
+
async tasks() {
|
|
365
|
+
const value = await this.host.getExtensionData(NAME, Keys.TASKS);
|
|
366
|
+
return Array.isArray(value) ? value.filter(TaskStore.isTask) : [];
|
|
367
|
+
}
|
|
368
|
+
saveTasks(tasks) {
|
|
369
|
+
const copy = tasks.map((task) => ({ ...task }));
|
|
370
|
+
return this.queue(() => this.host.setExtensionData(NAME, Keys.TASKS, copy));
|
|
371
|
+
}
|
|
372
|
+
async runs(taskId) {
|
|
373
|
+
await this.writing.catch(() => {
|
|
374
|
+
return;
|
|
375
|
+
});
|
|
376
|
+
const value = await this.host.getExtensionData(NAME, Keys.runs(taskId));
|
|
377
|
+
return Array.isArray(value) ? value : [];
|
|
378
|
+
}
|
|
379
|
+
saveRun(run) {
|
|
380
|
+
const copy = { ...run };
|
|
381
|
+
return this.queue(async () => {
|
|
382
|
+
const value = await this.host.getExtensionData(NAME, Keys.runs(run.taskId));
|
|
383
|
+
const runs = Array.isArray(value) ? value : [];
|
|
384
|
+
const index = runs.findIndex((other) => other.id === run.id);
|
|
385
|
+
const next = index >= 0 ? runs.map((other) => other.id === run.id ? copy : other) : [copy, ...runs];
|
|
386
|
+
await this.host.setExtensionData(NAME, Keys.runs(run.taskId), next.slice(0, Limits.RUNS_KEPT));
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
removeRuns(taskId) {
|
|
390
|
+
return this.queue(() => this.host.setExtensionData(NAME, Keys.runs(taskId), null));
|
|
391
|
+
}
|
|
392
|
+
finished(finished) {
|
|
393
|
+
return this.queue(() => this.host.setExtensionData(NAME, Keys.LATEST, finished));
|
|
394
|
+
}
|
|
395
|
+
async flush() {
|
|
396
|
+
await this.writing.catch(() => {
|
|
397
|
+
return;
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
queue(write) {
|
|
401
|
+
const next = this.writing.catch(() => {
|
|
402
|
+
return;
|
|
403
|
+
}).then(write);
|
|
404
|
+
this.writing = next;
|
|
405
|
+
return next;
|
|
406
|
+
}
|
|
407
|
+
static isTask(value) {
|
|
408
|
+
const task = value;
|
|
409
|
+
return typeof task?.id === "string" && typeof task.prompt === "string" && typeof task.schedule === "object" && typeof task.cwd === "string";
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// web-src/task-util.ts
|
|
414
|
+
var PROMPT_MAX = 20000;
|
|
415
|
+
var NAME_MAX = 80;
|
|
416
|
+
var PREFIX_MIN = 4;
|
|
417
|
+
var TaskUtil;
|
|
418
|
+
((TaskUtil) => {
|
|
419
|
+
function newId(prefix) {
|
|
420
|
+
const bytes = new Uint8Array(4);
|
|
421
|
+
crypto.getRandomValues(bytes);
|
|
422
|
+
return `${prefix}_${Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("")}`;
|
|
423
|
+
}
|
|
424
|
+
TaskUtil.newId = newId;
|
|
425
|
+
function create(input, cwd, now) {
|
|
426
|
+
const prompt = checkPrompt(input?.prompt);
|
|
427
|
+
const schedule = ScheduleUtil.check(input.schedule);
|
|
428
|
+
const at = now.toISOString();
|
|
429
|
+
const task = {
|
|
430
|
+
id: newId("task"),
|
|
431
|
+
name: checkName(input.name) ?? defaultName(prompt),
|
|
432
|
+
prompt,
|
|
433
|
+
schedule,
|
|
434
|
+
cwd: checkCwd(input.cwd) ?? cwd,
|
|
435
|
+
enabled: input.enabled !== false,
|
|
436
|
+
runCount: 0,
|
|
437
|
+
failCount: 0,
|
|
438
|
+
createdAt: at,
|
|
439
|
+
updatedAt: at
|
|
440
|
+
};
|
|
441
|
+
setOptional(task, input);
|
|
442
|
+
return arm(task, now);
|
|
443
|
+
}
|
|
444
|
+
TaskUtil.create = create;
|
|
445
|
+
function patch(task, patch2, now) {
|
|
446
|
+
const next = { ...task, updatedAt: now.toISOString() };
|
|
447
|
+
if (patch2.prompt !== undefined) {
|
|
448
|
+
next.prompt = checkPrompt(patch2.prompt);
|
|
449
|
+
}
|
|
450
|
+
if (patch2.name !== undefined) {
|
|
451
|
+
next.name = checkName(patch2.name) ?? defaultName(next.prompt);
|
|
452
|
+
}
|
|
453
|
+
if (patch2.cwd !== undefined) {
|
|
454
|
+
next.cwd = checkCwd(patch2.cwd) ?? task.cwd;
|
|
455
|
+
}
|
|
456
|
+
setOptional(next, patch2);
|
|
457
|
+
if (patch2.enabled !== undefined) {
|
|
458
|
+
next.enabled = patch2.enabled === true;
|
|
459
|
+
}
|
|
460
|
+
if (patch2.schedule !== undefined) {
|
|
461
|
+
next.schedule = ScheduleUtil.check(patch2.schedule);
|
|
462
|
+
}
|
|
463
|
+
const rearm = patch2.schedule !== undefined || next.enabled && !task.enabled;
|
|
464
|
+
return rearm ? arm(next, now) : next;
|
|
465
|
+
}
|
|
466
|
+
TaskUtil.patch = patch;
|
|
467
|
+
function arm(task, now) {
|
|
468
|
+
if (!task.enabled) {
|
|
469
|
+
return task;
|
|
470
|
+
}
|
|
471
|
+
const next = ScheduleUtil.next(task.schedule, now);
|
|
472
|
+
if (!next) {
|
|
473
|
+
throw new Error(task.schedule.kind === "once" ? `The time of ${task.name} (${task.schedule.at}) has passed: give it a time to come.` : `The rule of ${task.name} never comes round: check the cron expression.`);
|
|
474
|
+
}
|
|
475
|
+
return { ...task, nextRunAt: next.toISOString() };
|
|
476
|
+
}
|
|
477
|
+
TaskUtil.arm = arm;
|
|
478
|
+
function find(tasks, ref) {
|
|
479
|
+
const text = typeof ref === "string" ? ref.trim() : "";
|
|
480
|
+
const found = tasks.find((task) => task.id === text) ?? tasks.find((task) => task.name === text) ?? (text.length >= PREFIX_MIN ? only(tasks.filter((task) => task.id.startsWith(text))) : undefined);
|
|
481
|
+
if (!found) {
|
|
482
|
+
const known = tasks.length ? tasks.map((task) => `${task.id} (${task.name})`).join(", ") : "none yet";
|
|
483
|
+
throw new Error(`No task is ${JSON.stringify(text)}. The tasks: ${known}.`);
|
|
484
|
+
}
|
|
485
|
+
return found;
|
|
486
|
+
}
|
|
487
|
+
TaskUtil.find = find;
|
|
488
|
+
function systemPrompt(task, now) {
|
|
489
|
+
return [
|
|
490
|
+
RUN_MARK,
|
|
491
|
+
`This conversation is a scheduled run of the task "${task.name}" (${ScheduleUtil.describe(task.schedule, "en", now)}), started by the Ta service at ${now.toISOString()}.`,
|
|
492
|
+
"Nobody watches it while it runs: do the work without asking the user anything, and end with a short summary of what you found or did, which the user reads first.",
|
|
493
|
+
"</ta-cron-run>"
|
|
494
|
+
].join(`
|
|
495
|
+
`);
|
|
496
|
+
}
|
|
497
|
+
TaskUtil.systemPrompt = systemPrompt;
|
|
498
|
+
function only(tasks) {
|
|
499
|
+
return tasks.length === 1 ? tasks[0] : undefined;
|
|
500
|
+
}
|
|
501
|
+
function setOptional(task, input) {
|
|
502
|
+
if (input.model !== undefined) {
|
|
503
|
+
task.model = checkModel(input.model);
|
|
504
|
+
}
|
|
505
|
+
if (input.thinking !== undefined) {
|
|
506
|
+
task.thinking = checkThinking(input.thinking);
|
|
507
|
+
}
|
|
508
|
+
if (input.notify !== undefined) {
|
|
509
|
+
task.notify = checkNotify(input.notify);
|
|
510
|
+
}
|
|
511
|
+
for (const key of ["model", "thinking", "notify"]) {
|
|
512
|
+
if (task[key] === undefined) {
|
|
513
|
+
delete task[key];
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function checkPrompt(value) {
|
|
518
|
+
const prompt = typeof value === "string" ? value.trim() : "";
|
|
519
|
+
if (!prompt) {
|
|
520
|
+
throw new Error("A task needs a prompt: what Ta should do each time it runs.");
|
|
521
|
+
}
|
|
522
|
+
if (prompt.length > PROMPT_MAX) {
|
|
523
|
+
throw new Error(`A prompt is at most ${PROMPT_MAX} characters; keep long instructions in a file and name it.`);
|
|
524
|
+
}
|
|
525
|
+
return prompt;
|
|
526
|
+
}
|
|
527
|
+
function checkName(value) {
|
|
528
|
+
const name = typeof value === "string" ? value.trim() : "";
|
|
529
|
+
return name ? name.slice(0, NAME_MAX) : undefined;
|
|
530
|
+
}
|
|
531
|
+
function defaultName(prompt) {
|
|
532
|
+
const line = prompt.split(`
|
|
533
|
+
`)[0].trim();
|
|
534
|
+
return line.length > 40 ? `${line.slice(0, 39)}…` : line;
|
|
535
|
+
}
|
|
536
|
+
function checkCwd(value) {
|
|
537
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
538
|
+
}
|
|
539
|
+
function checkModel(value) {
|
|
540
|
+
const model = typeof value === "string" ? value.trim() : "";
|
|
541
|
+
return model || undefined;
|
|
542
|
+
}
|
|
543
|
+
function checkThinking(value) {
|
|
544
|
+
const level = typeof value === "string" ? value.trim() : "";
|
|
545
|
+
if (level && !THINKING_LEVELS.includes(level)) {
|
|
546
|
+
throw new Error(`A thinking level is one of ${THINKING_LEVELS.join(", ")}; not ${JSON.stringify(level)}.`);
|
|
547
|
+
}
|
|
548
|
+
return level || undefined;
|
|
549
|
+
}
|
|
550
|
+
function checkNotify(value) {
|
|
551
|
+
if (value === "" || value === null) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
if (!NOTIFY_LEVELS.includes(value)) {
|
|
555
|
+
throw new Error(`notify is one of ${NOTIFY_LEVELS.join(", ")}; not ${JSON.stringify(value)}.`);
|
|
556
|
+
}
|
|
557
|
+
return value;
|
|
558
|
+
}
|
|
559
|
+
function missed(task, now) {
|
|
560
|
+
return task.schedule.kind === "once" && !!task.nextRunAt && now.getTime() - new Date(task.nextRunAt).getTime() > Limits.GRACE_MS;
|
|
561
|
+
}
|
|
562
|
+
TaskUtil.missed = missed;
|
|
563
|
+
})(TaskUtil ||= {});
|
|
564
|
+
|
|
565
|
+
// web-src/scheduler.ts
|
|
566
|
+
class CronScheduler {
|
|
567
|
+
host;
|
|
568
|
+
tasks = [];
|
|
569
|
+
store;
|
|
570
|
+
options;
|
|
571
|
+
active = new Map;
|
|
572
|
+
waiting = [];
|
|
573
|
+
working = 0;
|
|
574
|
+
lingering = new Set;
|
|
575
|
+
timer;
|
|
576
|
+
disposed = false;
|
|
577
|
+
constructor(host, options = {}) {
|
|
578
|
+
this.host = host;
|
|
579
|
+
this.store = new TaskStore(host);
|
|
580
|
+
this.options = { now: () => new Date, tickMs: 60000, pollMs: 1000, timeoutMs: Limits.RUN_TIMEOUT_MS, parallel: Limits.PARALLEL_RUNS, ...options };
|
|
581
|
+
}
|
|
582
|
+
async start() {
|
|
583
|
+
this.tasks = await this.store.tasks();
|
|
584
|
+
const now = this.options.now();
|
|
585
|
+
for (const task of this.tasks) {
|
|
586
|
+
if (task.lastStatus === "running") {
|
|
587
|
+
await this.reclaim(task, now);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
for (const task of [...this.tasks]) {
|
|
591
|
+
if (task.enabled && TaskUtil.missed(task, now)) {
|
|
592
|
+
await this.miss(task, now);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
await this.store.saveTasks(this.tasks);
|
|
596
|
+
this.arm();
|
|
597
|
+
}
|
|
598
|
+
async dispose() {
|
|
599
|
+
this.disposed = true;
|
|
600
|
+
clearTimeout(this.timer);
|
|
601
|
+
await this.store.flush();
|
|
602
|
+
}
|
|
603
|
+
async list() {
|
|
604
|
+
return this.tasks.map((task) => ({ ...task }));
|
|
605
|
+
}
|
|
606
|
+
async get(ref) {
|
|
607
|
+
return { ...TaskUtil.find(this.tasks, ref) };
|
|
608
|
+
}
|
|
609
|
+
async runs(ref, limit = Limits.RUNS_KEPT) {
|
|
610
|
+
const task = TaskUtil.find(this.tasks, ref);
|
|
611
|
+
return (await this.store.runs(task.id)).slice(0, Math.max(1, limit));
|
|
612
|
+
}
|
|
613
|
+
async add(input) {
|
|
614
|
+
const cwd = (await this.host.getServerInfo()).cwd;
|
|
615
|
+
const task = TaskUtil.create(input, cwd, this.options.now());
|
|
616
|
+
await this.save([...this.tasks, task]);
|
|
617
|
+
return { ...task };
|
|
618
|
+
}
|
|
619
|
+
async update(ref, patch) {
|
|
620
|
+
const task = TaskUtil.find(this.tasks, ref);
|
|
621
|
+
const next = TaskUtil.patch(task, patch ?? {}, this.options.now());
|
|
622
|
+
await this.replace(next);
|
|
623
|
+
return { ...next };
|
|
624
|
+
}
|
|
625
|
+
setEnabled(ref, enabled) {
|
|
626
|
+
return this.update(ref, { enabled: enabled === true });
|
|
627
|
+
}
|
|
628
|
+
async remove(ref) {
|
|
629
|
+
const task = TaskUtil.find(this.tasks, ref);
|
|
630
|
+
await this.drop([task]);
|
|
631
|
+
return task;
|
|
632
|
+
}
|
|
633
|
+
async clear(cwd) {
|
|
634
|
+
const gone = this.tasks.filter((task) => !cwd || task.cwd === cwd);
|
|
635
|
+
await this.drop(gone);
|
|
636
|
+
return gone;
|
|
637
|
+
}
|
|
638
|
+
async run(ref) {
|
|
639
|
+
const task = TaskUtil.find(this.tasks, ref);
|
|
640
|
+
if (this.active.has(task.id)) {
|
|
641
|
+
throw new Error(`${task.name} is running now; wait for its run to end.`);
|
|
642
|
+
}
|
|
643
|
+
return this.begin(task, "manual", this.options.now());
|
|
644
|
+
}
|
|
645
|
+
isRun(sessionId) {
|
|
646
|
+
return [...this.active.values()].some((active) => active.sessionId === sessionId);
|
|
647
|
+
}
|
|
648
|
+
arm() {
|
|
649
|
+
clearTimeout(this.timer);
|
|
650
|
+
if (this.disposed) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
const now = this.options.now().getTime();
|
|
654
|
+
let delay = this.options.tickMs;
|
|
655
|
+
for (const task of this.tasks) {
|
|
656
|
+
if (task.enabled && task.nextRunAt) {
|
|
657
|
+
delay = Math.min(delay, Math.max(0, new Date(task.nextRunAt).getTime() - now));
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
this.timer = setTimeout(() => void this.tick().catch((error) => console.error(`${NAME}: ${message(error)}`)), delay);
|
|
661
|
+
this.timer.unref?.();
|
|
662
|
+
}
|
|
663
|
+
async tick() {
|
|
664
|
+
if (this.disposed) {
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
const now = this.options.now();
|
|
668
|
+
for (const task of this.tasks) {
|
|
669
|
+
if (!task.enabled || !task.nextRunAt || new Date(task.nextRunAt).getTime() > now.getTime()) {
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
if (this.active.has(task.id)) {
|
|
673
|
+
await this.replace(this.advance(task, now));
|
|
674
|
+
} else {
|
|
675
|
+
await this.begin(task, "schedule", now);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
await this.sweep();
|
|
679
|
+
this.arm();
|
|
680
|
+
}
|
|
681
|
+
advance(task, now) {
|
|
682
|
+
if (!ScheduleUtil.repeats(task.schedule)) {
|
|
683
|
+
const { nextRunAt: _done, ...rest } = task;
|
|
684
|
+
return { ...rest, enabled: false };
|
|
685
|
+
}
|
|
686
|
+
const next = ScheduleUtil.next(task.schedule, now);
|
|
687
|
+
return { ...task, nextRunAt: next?.toISOString() };
|
|
688
|
+
}
|
|
689
|
+
async begin(task, trigger, now) {
|
|
690
|
+
const run = { id: TaskUtil.newId("run"), taskId: task.id, trigger, status: "running", startedAt: now.toISOString(), cwd: task.cwd };
|
|
691
|
+
const started = trigger === "schedule" ? this.advance(task, now) : task;
|
|
692
|
+
await this.replace({ ...started, lastRunAt: run.startedAt, lastStatus: "running" });
|
|
693
|
+
await this.store.saveRun(run);
|
|
694
|
+
const active = { run, task: { ...task } };
|
|
695
|
+
this.active.set(task.id, active);
|
|
696
|
+
this.waiting.push(active);
|
|
697
|
+
this.pump();
|
|
698
|
+
return { ...run };
|
|
699
|
+
}
|
|
700
|
+
pump() {
|
|
701
|
+
while (this.working < this.options.parallel && this.waiting.length > 0 && !this.disposed) {
|
|
702
|
+
const active = this.waiting.shift();
|
|
703
|
+
this.working++;
|
|
704
|
+
this.execute(active).catch((error) => console.error(`${NAME}: ${message(error)}`)).finally(() => {
|
|
705
|
+
this.working--;
|
|
706
|
+
this.pump();
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
async execute(active) {
|
|
711
|
+
const { task, run } = active;
|
|
712
|
+
if (active.dropped) {
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
try {
|
|
716
|
+
const now = this.options.now();
|
|
717
|
+
const { sessionId } = await this.host.openSession({
|
|
718
|
+
cwd: task.cwd,
|
|
719
|
+
group: NAME,
|
|
720
|
+
appendSystemPrompt: TaskUtil.systemPrompt(task, now),
|
|
721
|
+
...task.model ? { model: task.model } : {},
|
|
722
|
+
...task.thinking ? { thinkingLevel: task.thinking } : {}
|
|
723
|
+
});
|
|
724
|
+
active.sessionId = sessionId;
|
|
725
|
+
await this.host.setSessionName(sessionId, `${task.name} · ${ScheduleUtil.clock(now)}`).catch(() => {
|
|
726
|
+
return;
|
|
727
|
+
});
|
|
728
|
+
const state = await this.host.getState(sessionId);
|
|
729
|
+
run.sessionFile = state.sessionFile;
|
|
730
|
+
await this.store.saveRun(run);
|
|
731
|
+
await this.host.prompt(sessionId, { clientMessageId: run.id, message: task.prompt });
|
|
732
|
+
await this.settled(active, state.messageCount);
|
|
733
|
+
const { text } = await this.host.getLastAssistantText(sessionId);
|
|
734
|
+
if (!text?.trim()) {
|
|
735
|
+
throw new Error("The run ended without an answer; the model may have failed. Open its conversation to see.");
|
|
736
|
+
}
|
|
737
|
+
await this.finish(active, { status: "success", summary: clip(text.trim()) });
|
|
738
|
+
} catch (error) {
|
|
739
|
+
await this.finish(active, { status: "error", error: message(error), ...error instanceof TimeoutError ? { reason: "timeout" } : {} });
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
async settled(active, before) {
|
|
743
|
+
const sessionId = active.sessionId;
|
|
744
|
+
const start = Date.now();
|
|
745
|
+
for (;; ) {
|
|
746
|
+
if (active.dropped || this.disposed) {
|
|
747
|
+
throw new Error("The task was removed while it ran.");
|
|
748
|
+
}
|
|
749
|
+
const state = await this.host.getState(sessionId);
|
|
750
|
+
if (!state.isStreaming && state.pendingMessageCount === 0 && state.messageCount > before) {
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
if (Date.now() - start > this.options.timeoutMs) {
|
|
754
|
+
await this.host.abort(sessionId).catch(() => {
|
|
755
|
+
return;
|
|
756
|
+
});
|
|
757
|
+
throw new TimeoutError(`The run took longer than ${Math.round(this.options.timeoutMs / 60000)} minutes and was stopped.`);
|
|
758
|
+
}
|
|
759
|
+
await new Promise((resolve) => setTimeout(resolve, this.options.pollMs));
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
async finish(active, end) {
|
|
763
|
+
this.active.delete(active.task.id);
|
|
764
|
+
const sessionId = active.sessionId;
|
|
765
|
+
if (sessionId) {
|
|
766
|
+
this.lingering.add(sessionId);
|
|
767
|
+
}
|
|
768
|
+
if (active.dropped || this.disposed) {
|
|
769
|
+
await this.sweep();
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
const run = { ...active.run, ...end, endedAt: this.options.now().toISOString() };
|
|
773
|
+
await this.store.saveRun(run);
|
|
774
|
+
const current = this.tasks.find((task) => task.id === active.task.id);
|
|
775
|
+
if (current) {
|
|
776
|
+
const failed = run.status === "error";
|
|
777
|
+
await this.replace({
|
|
778
|
+
...current,
|
|
779
|
+
lastStatus: run.status,
|
|
780
|
+
runCount: current.runCount + 1,
|
|
781
|
+
failCount: current.failCount + (failed ? 1 : 0)
|
|
782
|
+
});
|
|
783
|
+
await this.store.finished({ run, taskName: current.name, notify: ScheduleUtil.shouldNotify(ScheduleUtil.notifyLevel(current), failed) });
|
|
784
|
+
}
|
|
785
|
+
await this.sweep();
|
|
786
|
+
}
|
|
787
|
+
async sweep() {
|
|
788
|
+
if (this.lingering.size === 0) {
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
const open = new Map((await this.host.listSessions()).map((info) => [info.sessionId, info]));
|
|
792
|
+
for (const sessionId of [...this.lingering]) {
|
|
793
|
+
const info = open.get(sessionId);
|
|
794
|
+
if (!info) {
|
|
795
|
+
this.lingering.delete(sessionId);
|
|
796
|
+
} else if (info.attached === 0 && !info.isStreaming) {
|
|
797
|
+
this.lingering.delete(sessionId);
|
|
798
|
+
await this.host.closeSession(sessionId).catch(() => {
|
|
799
|
+
return;
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
async reclaim(task, now) {
|
|
805
|
+
for (const run of await this.store.runs(task.id)) {
|
|
806
|
+
if (run.status === "running") {
|
|
807
|
+
await this.store.saveRun({
|
|
808
|
+
...run,
|
|
809
|
+
status: "error",
|
|
810
|
+
reason: "interrupted",
|
|
811
|
+
error: "The Ta service stopped before the run ended.",
|
|
812
|
+
endedAt: now.toISOString()
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
this.tasks = this.tasks.map((other) => other.id === task.id ? { ...other, lastStatus: "error", failCount: other.failCount + 1 } : other);
|
|
817
|
+
}
|
|
818
|
+
async miss(task, now) {
|
|
819
|
+
const run = {
|
|
820
|
+
id: TaskUtil.newId("run"),
|
|
821
|
+
taskId: task.id,
|
|
822
|
+
trigger: "schedule",
|
|
823
|
+
status: "missed",
|
|
824
|
+
startedAt: task.nextRunAt ?? now.toISOString(),
|
|
825
|
+
endedAt: now.toISOString(),
|
|
826
|
+
error: "The Ta service was not running at its time; run it by hand if it is still wanted.",
|
|
827
|
+
cwd: task.cwd
|
|
828
|
+
};
|
|
829
|
+
await this.store.saveRun(run);
|
|
830
|
+
const { nextRunAt: _gone, ...rest } = task;
|
|
831
|
+
this.tasks = this.tasks.map((other) => other.id === task.id ? { ...rest, enabled: false, lastStatus: "missed", lastRunAt: run.startedAt } : other);
|
|
832
|
+
await this.store.finished({ run, taskName: task.name, notify: ScheduleUtil.notifyLevel(task) !== "never" });
|
|
833
|
+
}
|
|
834
|
+
async drop(tasks) {
|
|
835
|
+
const ids = new Set(tasks.map((task) => task.id));
|
|
836
|
+
for (const id of ids) {
|
|
837
|
+
const active = this.active.get(id);
|
|
838
|
+
if (active) {
|
|
839
|
+
active.dropped = true;
|
|
840
|
+
if (active.sessionId) {
|
|
841
|
+
await this.host.abort(active.sessionId).catch(() => {
|
|
842
|
+
return;
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
await this.store.removeRuns(id);
|
|
847
|
+
}
|
|
848
|
+
await this.save(this.tasks.filter((task) => !ids.has(task.id)));
|
|
849
|
+
}
|
|
850
|
+
replace(task) {
|
|
851
|
+
return this.save(this.tasks.map((other) => other.id === task.id ? task : other));
|
|
852
|
+
}
|
|
853
|
+
async save(tasks) {
|
|
854
|
+
this.tasks = tasks;
|
|
855
|
+
await this.store.saveTasks(tasks);
|
|
856
|
+
this.arm();
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
class TimeoutError extends Error {
|
|
861
|
+
}
|
|
862
|
+
function clip(text) {
|
|
863
|
+
return text.length > Limits.SUMMARY_MAX ? `${text.slice(0, Limits.SUMMARY_MAX - 1)}…` : text;
|
|
864
|
+
}
|
|
865
|
+
function message(error) {
|
|
866
|
+
return error instanceof Error ? error.message : String(error);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
// web-src/host.ts
|
|
870
|
+
async function activate(host) {
|
|
871
|
+
const scheduler = new CronScheduler(host);
|
|
872
|
+
await scheduler.start();
|
|
873
|
+
return scheduler;
|
|
874
|
+
}
|
|
875
|
+
export {
|
|
876
|
+
activate as default
|
|
877
|
+
};
|