hammerkit 2.1.0-next.12 → 2.1.0-next.14
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/dist/harness/connect.js +10 -7
- package/dist/harness/reconnect.js +137 -0
- package/dist/harness/sandbox-client.js +110 -25
- package/dist/remote/remote-work-commands.js +19 -8
- package/dist/server/executor.js +22 -5
- package/dist/server/http.js +4 -1
- package/dist/server/kubernetes-backend.js +155 -52
- package/dist/server/run-logs.js +25 -1
- package/dist/server/runs.js +339 -96
- package/dist/server/server.js +9 -4
- package/package.json +1 -1
package/dist/server/runs.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unavailableExecutor = exports.RunRegistry = exports.MAX_COMMANDS = exports.MAX_TASK_ERROR = exports.MAX_CAUSES = exports.SANDBOX_ID_PATTERN = exports.RUN_ID_PATTERN = exports.QuotaError = exports.RunError = exports.RUN_PHASES = exports.isFinal = void 0;
|
|
3
|
+
exports.unavailableExecutor = exports.RunRegistry = exports.MAX_COMMANDS = exports.MAX_TASK_ERROR = exports.MAX_CAUSES = exports.MAX_RESUMES = exports.CLOSE_TIMEOUT = exports.SANDBOX_ID_PATTERN = exports.RUN_ID_PATTERN = exports.QuotaError = exports.ShuttingDownError = exports.RunError = exports.isDetached = exports.RUN_PHASES = exports.isFinal = void 0;
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
5
|
const oauth_1 = require("./oauth");
|
|
6
6
|
const run_logs_1 = require("./run-logs");
|
|
@@ -30,6 +30,11 @@ function ranFor(result) {
|
|
|
30
30
|
}
|
|
31
31
|
return result.durationMs;
|
|
32
32
|
}
|
|
33
|
+
// Whether the server stops and leaves the run to the next one (RunContext.detached)
|
|
34
|
+
function isDetached(context) {
|
|
35
|
+
return context.signal.aborted && !!context.detached?.();
|
|
36
|
+
}
|
|
37
|
+
exports.isDetached = isDetached;
|
|
33
38
|
// The server can not take the run now; `status` is what the caller is told
|
|
34
39
|
class RunError extends Error {
|
|
35
40
|
constructor() {
|
|
@@ -38,6 +43,14 @@ class RunError extends Error {
|
|
|
38
43
|
}
|
|
39
44
|
}
|
|
40
45
|
exports.RunError = RunError;
|
|
46
|
+
// The server shuts down and took nothing of the request: a client may send it again, to the
|
|
47
|
+
// server that comes up after it
|
|
48
|
+
class ShuttingDownError extends RunError {
|
|
49
|
+
constructor() {
|
|
50
|
+
super('the server is shutting down');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ShuttingDownError = ShuttingDownError;
|
|
41
54
|
// A caller is over one of their limits, others may still submit
|
|
42
55
|
class QuotaError extends RunError {
|
|
43
56
|
constructor() {
|
|
@@ -71,6 +84,18 @@ function servicePlaces(runs, asleep = false) {
|
|
|
71
84
|
function onSandbox(run, sandbox) {
|
|
72
85
|
return run.source.type === 'sandbox' && run.source.id === sandbox;
|
|
73
86
|
}
|
|
87
|
+
// How long a server that stops waits for the executions it detaches: a pod gets 30s by
|
|
88
|
+
// default, and the records are written after. The process of a detached run gets less than
|
|
89
|
+
// this to stop its tasks on a host (DETACH_GRACE_MS in executor.ts), so what its execution
|
|
90
|
+
// does after (its directory, a release of its volumes) fits too. A server without a store
|
|
91
|
+
// cancels its runs and waits for them as long as they take (STOP_GRACE_MS, then a kill).
|
|
92
|
+
exports.CLOSE_TIMEOUT = 15000;
|
|
93
|
+
// how often the record of a run that executes is written while it says something
|
|
94
|
+
const PERSIST_EVERY = 2000;
|
|
95
|
+
// how often a run is resumed in a row without getting anywhere, then it fails
|
|
96
|
+
exports.MAX_RESUMES = 3;
|
|
97
|
+
// what a move that waited or executed when the server stopped says
|
|
98
|
+
const MOVE_LOST = 'not applied, the server restarted';
|
|
74
99
|
// the tasks of a run whose check keys are kept, a build file has fewer
|
|
75
100
|
const MAX_KEYED_TASKS = 1000;
|
|
76
101
|
// how much of the end of a task's output its result carries
|
|
@@ -114,6 +139,50 @@ function taskEventSize(event) {
|
|
|
114
139
|
const causes = (event.causes ?? []).reduce((sum, cause) => sum + Buffer.byteLength(cause) + 4, 0);
|
|
115
140
|
return 64 + causes + Buffer.byteLength(event.error ?? '') + (event.commands?.length ?? 0) * 64;
|
|
116
141
|
}
|
|
142
|
+
// what an event weighs against the log limits of a run; a state change weighs nothing, it is
|
|
143
|
+
// always kept
|
|
144
|
+
function eventSize(event) {
|
|
145
|
+
if (event.type === 'state') {
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
148
|
+
return event.type === 'log' ? Buffer.byteLength(event.text) + 64 : event.type === 'task' ? taskEventSize(event) : 64;
|
|
149
|
+
}
|
|
150
|
+
// The check keys of a record, as the registry keeps them: by task, a key or null
|
|
151
|
+
function checkKeys(keys) {
|
|
152
|
+
if (!keys || typeof keys !== 'object' || Array.isArray(keys)) {
|
|
153
|
+
return {};
|
|
154
|
+
}
|
|
155
|
+
return Object.fromEntries(Object.entries(keys)
|
|
156
|
+
.filter(([, key]) => key === null || typeof key === 'string')
|
|
157
|
+
.slice(0, MAX_KEYED_TASKS));
|
|
158
|
+
}
|
|
159
|
+
// What keeps the run of a record from executing again: what it runs, from where, is not in it
|
|
160
|
+
function unresumable(run) {
|
|
161
|
+
if (typeof run.id !== 'string' || !exports.RUN_ID_PATTERN.test(run.id)) {
|
|
162
|
+
return 'its id is not one of a run';
|
|
163
|
+
}
|
|
164
|
+
if (!run.owner || typeof run.owner.iss !== 'string' || typeof run.owner.sub !== 'string') {
|
|
165
|
+
return 'it names no owner';
|
|
166
|
+
}
|
|
167
|
+
if (typeof run.definition !== 'string') {
|
|
168
|
+
return 'it names no build file';
|
|
169
|
+
}
|
|
170
|
+
if (!Array.isArray(run.tasks) || !Array.isArray(run.accounts) || !run.options || typeof run.options !== 'object') {
|
|
171
|
+
return 'it names no tasks';
|
|
172
|
+
}
|
|
173
|
+
if (!run.source || !['ref', 'upload', 'sandbox'].includes(run.source.type)) {
|
|
174
|
+
return 'it names no source';
|
|
175
|
+
}
|
|
176
|
+
if (run.up &&
|
|
177
|
+
(typeof run.up.name !== 'string' ||
|
|
178
|
+
!Array.isArray(run.up.services) ||
|
|
179
|
+
!run.up.services.every((service) => typeof service === 'string') ||
|
|
180
|
+
!Array.isArray(run.up.endpoints) ||
|
|
181
|
+
!run.up.endpoints.every((endpoint) => endpoint && typeof endpoint.host === 'string'))) {
|
|
182
|
+
return 'it names no services';
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
117
186
|
// Where runs are kept while the server is up. Everything is scoped to the
|
|
118
187
|
// caller that submitted the run: the same id asked for by someone else is
|
|
119
188
|
// "not found", so ids can't be used to find out which runs exist.
|
|
@@ -126,10 +195,15 @@ class RunRegistry {
|
|
|
126
195
|
this.closed = false;
|
|
127
196
|
// callers that wait for a run to finish, let go when the server stops
|
|
128
197
|
this.waiters = new Set();
|
|
129
|
-
// the moves of service runs that execute
|
|
130
|
-
|
|
198
|
+
// the executions of runs, until they ended, and the moves of service runs that execute
|
|
199
|
+
// beside their services
|
|
200
|
+
this.executions = new Map();
|
|
201
|
+
this.moving = new Map();
|
|
131
202
|
// the records of evicted runs still being removed from the store, waited for by close
|
|
132
203
|
this.removing = new Set();
|
|
204
|
+
// close wrote every run down with all it said: a later write (a stop, an extend, a run that
|
|
205
|
+
// ends after the server stopped waiting for it) has it all too
|
|
206
|
+
this.written = false;
|
|
133
207
|
this.maxConcurrent = options.maxConcurrent ?? 4;
|
|
134
208
|
this.keep = options.keep ?? 200;
|
|
135
209
|
this.maxEvents = options.maxEvents ?? 5000;
|
|
@@ -140,8 +214,8 @@ class RunRegistry {
|
|
|
140
214
|
}
|
|
141
215
|
/**
|
|
142
216
|
* Takes back the runs of a previous start. Those that waited for an approval wait
|
|
143
|
-
* again, for what is left of their time
|
|
144
|
-
*
|
|
217
|
+
* again, for what is left of their time, and those that were asleep sleep on. Those that
|
|
218
|
+
* were queued, running or up go back in the queue under their id (resume).
|
|
145
219
|
*/
|
|
146
220
|
async restore() {
|
|
147
221
|
const store = this.options.store;
|
|
@@ -151,8 +225,10 @@ class RunRegistry {
|
|
|
151
225
|
return;
|
|
152
226
|
}
|
|
153
227
|
const { records, skipped } = await store.load();
|
|
228
|
+
// the queue as it was: the runs that executed first, then the others as they were queued
|
|
229
|
+
const resuming = [];
|
|
154
230
|
skipped.forEach((name) => this.options.log?.(`skip run record ${name}: it can not be read`));
|
|
155
|
-
for (const { run, events } of records) {
|
|
231
|
+
for (const { run, events, keys, clean } of records) {
|
|
156
232
|
// a move that executed went with the server
|
|
157
233
|
delete run.moving;
|
|
158
234
|
const entry = {
|
|
@@ -164,7 +240,7 @@ class RunRegistry {
|
|
|
164
240
|
bytes: 0,
|
|
165
241
|
tails: new Map(Object.entries(run.results ?? {}).map(([task, result]) => [task, result.lastLog])),
|
|
166
242
|
saving: Promise.resolve(),
|
|
167
|
-
keys:
|
|
243
|
+
keys: checkKeys(keys),
|
|
168
244
|
};
|
|
169
245
|
this.entries.set(run.id, entry);
|
|
170
246
|
if (run.state === 'awaiting-approval') {
|
|
@@ -177,47 +253,115 @@ class RunRegistry {
|
|
|
177
253
|
this.finish(entry, 'rejected', 'nobody approved it in time');
|
|
178
254
|
}
|
|
179
255
|
}
|
|
180
|
-
else if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
run.volumesKept = true;
|
|
256
|
+
else if (run.state === 'asleep') {
|
|
257
|
+
if (this.maySleep(entry)) {
|
|
258
|
+
this.expireLater(entry);
|
|
259
|
+
if (!this.keepsVolumes()) {
|
|
260
|
+
// the backend no longer keeps them, the sweep below removes them
|
|
261
|
+
delete run.volumesKept;
|
|
187
262
|
}
|
|
188
|
-
this.sleep(entry);
|
|
189
263
|
}
|
|
190
264
|
else {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// the backend no longer keeps them, the sweep below removes them
|
|
195
|
-
delete run.volumesKept;
|
|
265
|
+
// its time ran out while the server was down: it ends now, and gives its place back
|
|
266
|
+
run.stoppedBy ?? (run.stoppedBy = 'ttl');
|
|
267
|
+
this.finish(entry, 'succeeded');
|
|
196
268
|
}
|
|
197
269
|
}
|
|
198
|
-
else if (run.state === '
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
270
|
+
else if (run.state === 'queued' || run.state === 'running' || run.state === 'up') {
|
|
271
|
+
resuming.push({
|
|
272
|
+
entry,
|
|
273
|
+
executed: run.state !== 'queued',
|
|
274
|
+
queuedAt: String(run.queuedAt ?? run.createdAt ?? ''),
|
|
275
|
+
clean: clean === true,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
resuming
|
|
280
|
+
.sort((a, b) => Number(b.executed) - Number(a.executed) || a.queuedAt.localeCompare(b.queuedAt))
|
|
281
|
+
.forEach(({ entry, clean }) => this.resume(entry, clean));
|
|
282
|
+
this.evict();
|
|
283
|
+
try {
|
|
284
|
+
this.options.executor.restored?.([...this.entries.values()]
|
|
285
|
+
.filter((entry) => !isFinal(entry.run.state) && (entry.run.volumesKept || entry.resumed))
|
|
286
|
+
.map((entry) => this.copy(entry.run)));
|
|
287
|
+
}
|
|
288
|
+
catch (e) {
|
|
289
|
+
// the server starts all the same, what the backend kept is looked at again on the next start
|
|
290
|
+
this.options.log?.(`the backend could not take back what it kept: ${e instanceof Error ? e.message : e}`);
|
|
291
|
+
}
|
|
292
|
+
this.dispatch();
|
|
293
|
+
}
|
|
294
|
+
// A run that was queued or executing when the server stopped goes back in the queue, under
|
|
295
|
+
// its id and with what it was pinned to; its tasks that finished are taken from the cache.
|
|
296
|
+
// It ends instead when it was to end: a cancel or a stop came while the server stopped, its
|
|
297
|
+
// time ran out, or it took the server down too often. One that can not be read fails.
|
|
298
|
+
// `clean`: the server that stopped wrote it down with all it said.
|
|
299
|
+
resume(entry, clean) {
|
|
300
|
+
const run = entry.run;
|
|
301
|
+
const was = run.state;
|
|
302
|
+
// what an execution before the restart left goes when the run ends before it executes again
|
|
303
|
+
entry.resumed = !!run.startedAt;
|
|
304
|
+
const unreadable = unresumable(run);
|
|
305
|
+
if (unreadable) {
|
|
306
|
+
// enough of a run to show how it ended, to nobody when its owner is not known
|
|
307
|
+
if (!run.owner || typeof run.owner.iss !== 'string' || typeof run.owner.sub !== 'string') {
|
|
308
|
+
run.owner = { iss: '', sub: '' };
|
|
202
309
|
}
|
|
203
|
-
|
|
204
|
-
|
|
310
|
+
run.tasks = Array.isArray(run.tasks) ? run.tasks : [];
|
|
311
|
+
run.accounts = Array.isArray(run.accounts) ? run.accounts : [];
|
|
312
|
+
run.options = run.options && typeof run.options === 'object' ? run.options : {};
|
|
313
|
+
if (!run.source || typeof run.source !== 'object') {
|
|
314
|
+
run.source = { type: 'upload', snapshot: '' };
|
|
315
|
+
}
|
|
316
|
+
this.finish(entry, 'failed', `the server stopped while the run was ${was} and its record can not resume it: ${unreadable}, submit it again`);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (run.cancelling) {
|
|
320
|
+
this.finish(entry, 'cancelled');
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (run.up && run.stoppedBy) {
|
|
324
|
+
// stopped while it was up, or replaced
|
|
325
|
+
this.finish(entry, 'succeeded');
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (run.up && run.upAt && !(Date.parse(run.expiresAt ?? '') > this.now())) {
|
|
329
|
+
run.stoppedBy = 'ttl';
|
|
330
|
+
this.finish(entry, 'succeeded');
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
// the time a run gets counts from its first start; a service run that was up gets it again
|
|
334
|
+
const limit = this.options.maxDuration;
|
|
335
|
+
if (limit && run.startedAt && !run.upAt && this.now() - Date.parse(run.startedAt) >= limit) {
|
|
336
|
+
this.finish(entry, 'failed', this.tooLong());
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (was !== 'queued') {
|
|
340
|
+
run.resumes = (run.resumes ?? 0) + 1;
|
|
341
|
+
if (run.resumes > exports.MAX_RESUMES) {
|
|
342
|
+
this.finish(entry, 'failed', `the server stopped ${run.resumes} times in a row while the run was ${was}, it is not resumed again, submit it again`);
|
|
343
|
+
return;
|
|
205
344
|
}
|
|
206
|
-
|
|
207
|
-
//
|
|
208
|
-
|
|
345
|
+
if (!clean) {
|
|
346
|
+
// a server that was killed: what the run said since it was last written down is gone
|
|
347
|
+
run.lostAfter = entry.events.length;
|
|
209
348
|
}
|
|
210
349
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
350
|
+
// what it said counts against its limits as before
|
|
351
|
+
entry.bytes = entry.events.reduce((sum, event) => sum + eventSize(event), 0);
|
|
352
|
+
delete run.sleepsAt;
|
|
353
|
+
run.state = 'queued';
|
|
354
|
+
run.queuedAt = this.timestamp();
|
|
355
|
+
const lost = was !== 'queued' && !clean ? ', the last of its log before is lost' : '';
|
|
356
|
+
this.record(entry, { type: 'log', task: 'hammerkit', stream: 'stdout', text: `resumed after a server restart${lost}` }, true);
|
|
357
|
+
this.record(entry, { type: 'state', state: 'queued' });
|
|
358
|
+
this.queue.push(entry);
|
|
215
359
|
}
|
|
216
360
|
// A run that needs approvals (for the accounts named) waits for them, the others
|
|
217
361
|
// are queued at once.
|
|
218
362
|
submit(owner, spec, approvalAccounts = []) {
|
|
219
363
|
if (this.closed) {
|
|
220
|
-
throw new
|
|
364
|
+
throw new ShuttingDownError();
|
|
221
365
|
}
|
|
222
366
|
const waiting = approvalAccounts.length > 0;
|
|
223
367
|
if (waiting && this.waiting().length >= this.maxWaiting) {
|
|
@@ -484,12 +628,16 @@ class RunRegistry {
|
|
|
484
628
|
this.finish(entry, 'cancelled', reason);
|
|
485
629
|
}
|
|
486
630
|
else if (entry.run.state === 'running') {
|
|
631
|
+
// written down at once: a server that stops before the run did ends it on its next start
|
|
632
|
+
entry.run.cancelling = true;
|
|
633
|
+
this.persist(entry);
|
|
487
634
|
entry.abort.abort();
|
|
488
635
|
}
|
|
489
636
|
else if (entry.run.state === 'up') {
|
|
490
637
|
(_a = entry.run).stoppedBy ?? (_a.stoppedBy = by ?? 'replaced');
|
|
491
638
|
entry.sleeping = false;
|
|
492
639
|
entry.restartTo = undefined;
|
|
640
|
+
this.persist(entry);
|
|
493
641
|
entry.mover?.abort.abort();
|
|
494
642
|
entry.abort.abort();
|
|
495
643
|
}
|
|
@@ -566,7 +714,11 @@ class RunRegistry {
|
|
|
566
714
|
if (!entry) {
|
|
567
715
|
return undefined;
|
|
568
716
|
}
|
|
569
|
-
|
|
717
|
+
// a cursor past the end comes from a server that was killed after it sent what it did not
|
|
718
|
+
// write down: those events are gone, the ones said again since have their numbers
|
|
719
|
+
const last = entry.events.length;
|
|
720
|
+
const from = after > last ? Math.min(entry.run.lostAfter ?? last, last) : after;
|
|
721
|
+
const events = entry.events.filter((event) => event.seq > from);
|
|
570
722
|
const finished = FINAL_STATES.includes(entry.run.state);
|
|
571
723
|
if (!finished) {
|
|
572
724
|
entry.listeners.add(listener);
|
|
@@ -614,46 +766,47 @@ class RunRegistry {
|
|
|
614
766
|
if (!entry) {
|
|
615
767
|
return undefined;
|
|
616
768
|
}
|
|
617
|
-
|
|
618
|
-
entry.index ?? (entry.index = new run_logs_1.LogIndex());
|
|
619
|
-
entry.index.update(entry.events);
|
|
620
|
-
return { events: entry.events, index: entry.index, truncated: entry.truncated };
|
|
769
|
+
return this.logOf(entry);
|
|
621
770
|
}
|
|
622
771
|
// The log of any run, for a caller whose right to read it was checked already (an
|
|
623
772
|
// entitlement with `observe`).
|
|
624
773
|
logsOf(id) {
|
|
625
774
|
const entry = this.entries.get(id);
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
775
|
+
return entry ? this.logOf(entry) : undefined;
|
|
776
|
+
}
|
|
777
|
+
logOf(entry) {
|
|
778
|
+
// only the events since the last read are looked at
|
|
629
779
|
entry.index ?? (entry.index = new run_logs_1.LogIndex());
|
|
630
780
|
entry.index.update(entry.events);
|
|
631
|
-
return {
|
|
781
|
+
return {
|
|
782
|
+
events: entry.events,
|
|
783
|
+
index: entry.index,
|
|
784
|
+
truncated: entry.truncated,
|
|
785
|
+
...(entry.run.lostAfter !== undefined ? { lostAfter: entry.run.lostAfter } : {}),
|
|
786
|
+
};
|
|
632
787
|
}
|
|
633
|
-
|
|
788
|
+
/**
|
|
789
|
+
* Stops accepting runs. Without a store the runs in flight are cancelled. With one they are
|
|
790
|
+
* detached: a queued run stays in the queue of its record, and a running one or a service
|
|
791
|
+
* run that is up stops its execution without cancelling what it started (RunContext.detached).
|
|
792
|
+
* The next start takes them all back (restore). The executions get `closeTimeout` to stop,
|
|
793
|
+
* then every run is written down as it is.
|
|
794
|
+
*/
|
|
634
795
|
async close() {
|
|
635
796
|
this.closed = true;
|
|
797
|
+
const detach = !!this.options.store;
|
|
636
798
|
for (const entry of this.queue.splice(0)) {
|
|
637
|
-
if (
|
|
638
|
-
// a woken service run sleeps again, the next start keeps it
|
|
639
|
-
this.sleep(entry);
|
|
640
|
-
}
|
|
641
|
-
else {
|
|
799
|
+
if (!detach) {
|
|
642
800
|
this.finish(entry, 'cancelled');
|
|
643
801
|
}
|
|
644
802
|
}
|
|
645
803
|
for (const entry of this.entries.values()) {
|
|
646
|
-
if (entry.run.state === '
|
|
647
|
-
this.
|
|
804
|
+
if (entry.run.state === 'awaiting-approval' && !detach) {
|
|
805
|
+
this.finish(entry, 'cancelled');
|
|
648
806
|
}
|
|
649
|
-
else if (entry.run.state
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
this.clearTimer(entry);
|
|
653
|
-
}
|
|
654
|
-
else {
|
|
655
|
-
this.finish(entry, 'cancelled');
|
|
656
|
-
}
|
|
807
|
+
else if (!isFinal(entry.run.state)) {
|
|
808
|
+
// the record keeps it waiting, asleep or up; the next start takes it back, with its time
|
|
809
|
+
this.clearTimer(entry);
|
|
657
810
|
}
|
|
658
811
|
}
|
|
659
812
|
// who waits for a run that waits for an approval is told how it stands now
|
|
@@ -661,19 +814,44 @@ class RunRegistry {
|
|
|
661
814
|
done();
|
|
662
815
|
}
|
|
663
816
|
for (const entry of this.entries.values()) {
|
|
817
|
+
if (detach && !isFinal(entry.run.state)) {
|
|
818
|
+
// the next server starts the run at the commit it is at
|
|
819
|
+
this.dropMove(entry);
|
|
820
|
+
}
|
|
664
821
|
if (entry.run.state === 'running' || entry.run.state === 'up') {
|
|
665
|
-
//
|
|
666
|
-
|
|
667
|
-
|
|
822
|
+
// one that stops already (a cancel, its ttl, a sleep, a move) ends as it would
|
|
823
|
+
if (detach && !entry.abort.signal.aborted) {
|
|
824
|
+
entry.detached = true;
|
|
825
|
+
clearTimeout(entry.durationTimer);
|
|
826
|
+
}
|
|
668
827
|
entry.mover?.abort.abort();
|
|
669
828
|
entry.abort.abort();
|
|
670
829
|
}
|
|
671
830
|
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
831
|
+
// a server without a store waits for its cancels to clean up, however long they take
|
|
832
|
+
const deadline = detach ? Date.now() + (this.options.closeTimeout ?? exports.CLOSE_TIMEOUT) : Infinity;
|
|
833
|
+
const stopping = () => new Set([
|
|
834
|
+
...this.executions.values(),
|
|
835
|
+
...this.moving.values(),
|
|
836
|
+
...[...this.entries.values()].filter((entry) => (entry.run.state === 'running' || entry.run.state === 'up') && !entry.detached),
|
|
837
|
+
]);
|
|
838
|
+
while (stopping().size > 0 && Date.now() < deadline) {
|
|
675
839
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
676
840
|
}
|
|
841
|
+
const left = stopping().size;
|
|
842
|
+
if (left > 0) {
|
|
843
|
+
this.options.log?.(`the server stops while ${left} ${left === 1 ? 'run still stops, it is' : 'runs still stop, they are'} written down as ${left === 1 ? 'it is' : 'they are'}`);
|
|
844
|
+
}
|
|
845
|
+
if (detach) {
|
|
846
|
+
// with all they said, for the start that resumes them
|
|
847
|
+
this.written = true;
|
|
848
|
+
for (const entry of this.entries.values()) {
|
|
849
|
+
clearTimeout(entry.persistTimer);
|
|
850
|
+
if (!isFinal(entry.run.state)) {
|
|
851
|
+
this.persist(entry, true);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
}
|
|
677
855
|
// the runs that were stopped are written down before the server is gone, and the records
|
|
678
856
|
// of evicted runs are gone from the store
|
|
679
857
|
await Promise.all([...this.entries.values()].map((entry) => entry.saving));
|
|
@@ -852,7 +1030,8 @@ class RunRegistry {
|
|
|
852
1030
|
// does not keep the others from the machine. A run on a sandbox another run works on
|
|
853
1031
|
// waits for it, as does one that needs what an executing run holds.
|
|
854
1032
|
dispatch() {
|
|
855
|
-
|
|
1033
|
+
// the runs left in the queue start with the next server
|
|
1034
|
+
while (this.running < this.maxConcurrent && !this.closed) {
|
|
856
1035
|
const perCaller = this.options.limits?.running ?? Infinity;
|
|
857
1036
|
const index = this.queue.findIndex((entry) => this.runningOf(entry.run.owner) < perCaller && !this.sandboxBusy(entry.run) && !this.held(entry.run));
|
|
858
1037
|
if (index < 0) {
|
|
@@ -866,26 +1045,35 @@ class RunRegistry {
|
|
|
866
1045
|
}
|
|
867
1046
|
const [entry] = this.queue.splice(index, 1);
|
|
868
1047
|
this.running++;
|
|
869
|
-
this.execute(entry).finally(() => {
|
|
1048
|
+
const execution = this.execute(entry).finally(() => {
|
|
1049
|
+
this.executions.delete(execution);
|
|
870
1050
|
// a service run that is up gave its place back already
|
|
871
1051
|
if (!entry.released) {
|
|
872
1052
|
this.running--;
|
|
873
1053
|
}
|
|
874
1054
|
this.dispatch();
|
|
875
1055
|
});
|
|
1056
|
+
this.executions.set(execution, entry);
|
|
876
1057
|
}
|
|
877
1058
|
}
|
|
878
1059
|
async execute(entry) {
|
|
1060
|
+
// a run that resumes executes again: what it left is this execution's to remove, and its
|
|
1061
|
+
// time counts from its first start (a service run that was up gets it again)
|
|
1062
|
+
const continues = entry.resumed && !!entry.run.startedAt && !entry.run.upAt;
|
|
1063
|
+
entry.resumed = false;
|
|
879
1064
|
entry.run.state = 'running';
|
|
880
|
-
|
|
1065
|
+
if (!continues) {
|
|
1066
|
+
entry.run.startedAt = this.timestamp();
|
|
1067
|
+
}
|
|
881
1068
|
this.record(entry, { type: 'state', state: 'running' });
|
|
882
1069
|
// a service run gets this long to be up, then its ttl counts
|
|
883
1070
|
const limit = this.options.maxDuration;
|
|
1071
|
+
const ran = continues ? Math.max(0, this.now() - Date.parse(entry.run.startedAt)) : 0;
|
|
884
1072
|
entry.durationTimer = limit
|
|
885
1073
|
? setTimeout(() => {
|
|
886
1074
|
entry.timedOut = true;
|
|
887
1075
|
entry.abort.abort();
|
|
888
|
-
}, limit)
|
|
1076
|
+
}, Math.max(0, limit - ran))
|
|
889
1077
|
: undefined;
|
|
890
1078
|
entry.durationTimer?.unref();
|
|
891
1079
|
const token = {};
|
|
@@ -900,6 +1088,7 @@ class RunRegistry {
|
|
|
900
1088
|
await this.options.executor.execute(run, {
|
|
901
1089
|
emit: (event) => this.record(entry, event),
|
|
902
1090
|
signal: entry.abort.signal,
|
|
1091
|
+
detached: () => !!entry.detached,
|
|
903
1092
|
...(Object.keys(durations).length > 0 ? { durations } : {}),
|
|
904
1093
|
...(this.options.artifacts && !entry.run.up
|
|
905
1094
|
? {
|
|
@@ -916,8 +1105,8 @@ class RunRegistry {
|
|
|
916
1105
|
}
|
|
917
1106
|
: {}),
|
|
918
1107
|
});
|
|
919
|
-
if (entry.holder !== token) {
|
|
920
|
-
// a move took the services over
|
|
1108
|
+
if (entry.holder !== token || entry.detached) {
|
|
1109
|
+
// a move took the services over, or the run resumes with the next server
|
|
921
1110
|
return;
|
|
922
1111
|
}
|
|
923
1112
|
if (this.fallBack(entry, entry.timedOut ? this.tooLong() : 'its services stopped before they were up')) {
|
|
@@ -936,7 +1125,7 @@ class RunRegistry {
|
|
|
936
1125
|
}
|
|
937
1126
|
catch (e) {
|
|
938
1127
|
const message = e instanceof Error ? e.message : String(e);
|
|
939
|
-
if (entry.holder !== token) {
|
|
1128
|
+
if (entry.holder !== token || entry.detached) {
|
|
940
1129
|
return;
|
|
941
1130
|
}
|
|
942
1131
|
if (this.fallBack(entry, entry.timedOut ? this.tooLong() : message)) {
|
|
@@ -975,6 +1164,7 @@ class RunRegistry {
|
|
|
975
1164
|
this.moveTo(entry, landing.change);
|
|
976
1165
|
}
|
|
977
1166
|
entry.run.state = 'up';
|
|
1167
|
+
delete entry.run.resumes;
|
|
978
1168
|
// one reading of the clock, so the ttl is exactly expiresAt - upAt
|
|
979
1169
|
const now = this.now();
|
|
980
1170
|
// the ttl counts from the first time, not from a wake
|
|
@@ -1181,7 +1371,7 @@ class RunRegistry {
|
|
|
1181
1371
|
...(change.plan ? { plan: change.plan } : {}),
|
|
1182
1372
|
};
|
|
1183
1373
|
const execution = this.executeMove(entry, token, abort, run);
|
|
1184
|
-
this.moving.
|
|
1374
|
+
this.moving.set(execution, entry);
|
|
1185
1375
|
void execution.finally(() => this.moving.delete(execution));
|
|
1186
1376
|
}
|
|
1187
1377
|
// A move beside the services of a run that is up: once its services are ready it holds them
|
|
@@ -1192,6 +1382,7 @@ class RunRegistry {
|
|
|
1192
1382
|
await this.options.executor.execute(run, {
|
|
1193
1383
|
emit: (event) => this.record(entry, event),
|
|
1194
1384
|
signal: abort.signal,
|
|
1385
|
+
detached: () => !!entry.detached,
|
|
1195
1386
|
up: (targets) => this.moved(entry, token, targets),
|
|
1196
1387
|
keep: () => (entry.holder === token ? this.keepOf(entry) : 'everything'),
|
|
1197
1388
|
});
|
|
@@ -1205,6 +1396,10 @@ class RunRegistry {
|
|
|
1205
1396
|
failure = `the move took longer than ${(0, units_1.formatDuration)(this.options.maxDuration)} and was stopped`;
|
|
1206
1397
|
}
|
|
1207
1398
|
this.releaseMover(mover);
|
|
1399
|
+
if (entry.detached) {
|
|
1400
|
+
// the run resumes with the next server, at the commit it is at
|
|
1401
|
+
return;
|
|
1402
|
+
}
|
|
1208
1403
|
if (entry.holder === token) {
|
|
1209
1404
|
// the run's services were this execution's
|
|
1210
1405
|
if (entry.restartTo || entry.sleeping) {
|
|
@@ -1272,12 +1467,7 @@ class RunRegistry {
|
|
|
1272
1467
|
if (this.closed) {
|
|
1273
1468
|
// the record keeps it asleep, at the commit it was up at
|
|
1274
1469
|
this.moveTo(entry, restart, 'the server stopped before the services were up at the commit');
|
|
1275
|
-
|
|
1276
|
-
this.sleep(entry);
|
|
1277
|
-
}
|
|
1278
|
-
else {
|
|
1279
|
-
this.finish(entry, 'failed', 'the server stopped while the services were up, start them again');
|
|
1280
|
-
}
|
|
1470
|
+
this.leaveForRestart(entry);
|
|
1281
1471
|
return;
|
|
1282
1472
|
}
|
|
1283
1473
|
// up at its commit, the run is there (markUp); else it goes back (fallBack)
|
|
@@ -1314,17 +1504,49 @@ class RunRegistry {
|
|
|
1314
1504
|
text: `did not get up at ${landing.change.source.commit}, starting again at ${from.commit}: ${error}`,
|
|
1315
1505
|
});
|
|
1316
1506
|
if (this.closed) {
|
|
1317
|
-
|
|
1318
|
-
this.sleep(entry);
|
|
1319
|
-
}
|
|
1320
|
-
else {
|
|
1321
|
-
this.finish(entry, 'failed', 'the server stopped while the services were up, start them again');
|
|
1322
|
-
}
|
|
1507
|
+
this.leaveForRestart(entry);
|
|
1323
1508
|
return true;
|
|
1324
1509
|
}
|
|
1325
1510
|
this.requeue(entry);
|
|
1326
1511
|
return true;
|
|
1327
1512
|
}
|
|
1513
|
+
// The server stops: a move that waits, executes beside the services or starts them at its
|
|
1514
|
+
// commit does not get there, the run stays at the commit it is at and says so
|
|
1515
|
+
dropMove(entry) {
|
|
1516
|
+
const landing = entry.landing && !entry.landing.recorded ? entry.landing : undefined;
|
|
1517
|
+
const change = entry.mover?.change ?? entry.pendingMove ?? landing?.change;
|
|
1518
|
+
if (!change) {
|
|
1519
|
+
return;
|
|
1520
|
+
}
|
|
1521
|
+
entry.pendingMove = undefined;
|
|
1522
|
+
if (landing) {
|
|
1523
|
+
entry.landing = undefined;
|
|
1524
|
+
entry.run.source = { ...landing.from.source };
|
|
1525
|
+
if (landing.from.plan) {
|
|
1526
|
+
entry.run.plan = landing.from.plan;
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
// the execution of a move ends with the server, its record says where it got
|
|
1530
|
+
const mover = entry.mover;
|
|
1531
|
+
entry.mover = undefined;
|
|
1532
|
+
this.moveTo(entry, change, MOVE_LOST);
|
|
1533
|
+
entry.mover = mover;
|
|
1534
|
+
}
|
|
1535
|
+
// The server stops while the services of a run stop to start again at another commit: it
|
|
1536
|
+
// sleeps in its record if it may, else the next server resumes it at the commit it was up at
|
|
1537
|
+
leaveForRestart(entry) {
|
|
1538
|
+
if (this.maySleep(entry) && this.options.store) {
|
|
1539
|
+
this.sleep(entry);
|
|
1540
|
+
}
|
|
1541
|
+
else if (this.options.store) {
|
|
1542
|
+
entry.targets = undefined;
|
|
1543
|
+
entry.run.state = 'queued';
|
|
1544
|
+
this.record(entry, { type: 'state', state: 'queued' });
|
|
1545
|
+
}
|
|
1546
|
+
else {
|
|
1547
|
+
this.finish(entry, 'failed', 'the server stopped while the services were up, start them again');
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1328
1550
|
// where a run that moves goes back to: the commit it is at (service runs start from a ref)
|
|
1329
1551
|
landingFrom(entry) {
|
|
1330
1552
|
return {
|
|
@@ -1393,8 +1615,8 @@ class RunRegistry {
|
|
|
1393
1615
|
// the record, so the volumes are removed (RunExecutor.forget) when the run ends asleep, or
|
|
1394
1616
|
// when a stop came after the sleep began.
|
|
1395
1617
|
keepOf(entry) {
|
|
1396
|
-
// a run that starts again at another commit keeps them on any server
|
|
1397
|
-
if (entry.restartTo || entry.landing || (entry.sleeping && this.keepsVolumes())) {
|
|
1618
|
+
// a run that starts again at another commit, or with the next server, keeps them on any server
|
|
1619
|
+
if (entry.restartTo || entry.landing || entry.detached || (entry.sleeping && this.keepsVolumes())) {
|
|
1398
1620
|
entry.run.volumesKept = true;
|
|
1399
1621
|
return 'volumes';
|
|
1400
1622
|
}
|
|
@@ -1431,7 +1653,7 @@ class RunRegistry {
|
|
|
1431
1653
|
}
|
|
1432
1654
|
const sleepTtl = entry.run.up.sleepTtl;
|
|
1433
1655
|
if (entry.run.state === 'up' && sleepTtl) {
|
|
1434
|
-
// only kept in memory: a
|
|
1656
|
+
// only kept in memory: a run that resumes after a restart is up for its sleepTtl from then
|
|
1435
1657
|
entry.run.sleepsAt = new Date(this.now() + sleepTtl).toISOString();
|
|
1436
1658
|
return undefined;
|
|
1437
1659
|
}
|
|
@@ -1467,7 +1689,7 @@ class RunRegistry {
|
|
|
1467
1689
|
return undefined;
|
|
1468
1690
|
}
|
|
1469
1691
|
// The end of a run that was up: stopped by somebody, its ttl or a newer run it succeeded,
|
|
1470
|
-
// stopped by
|
|
1692
|
+
// stopped by a server without a store going away it failed (one with a store detaches it).
|
|
1471
1693
|
endServices(entry, failure) {
|
|
1472
1694
|
if (entry.timedOut) {
|
|
1473
1695
|
this.finish(entry, 'failed', this.tooLong());
|
|
@@ -1495,14 +1717,17 @@ class RunRegistry {
|
|
|
1495
1717
|
entry.mover?.abort.abort();
|
|
1496
1718
|
entry.mover = undefined;
|
|
1497
1719
|
delete entry.run.moving;
|
|
1498
|
-
if (entry.run.volumesKept) {
|
|
1499
|
-
// it ends without waking: what was kept for it
|
|
1720
|
+
if (entry.run.volumesKept || entry.resumed) {
|
|
1721
|
+
// it ends without waking, or after a restart: what was kept for it, or what an execution
|
|
1722
|
+
// before the restart left, goes
|
|
1500
1723
|
delete entry.run.volumesKept;
|
|
1724
|
+
entry.resumed = false;
|
|
1501
1725
|
const run = this.copy(entry.run);
|
|
1502
1726
|
Promise.resolve()
|
|
1503
1727
|
.then(() => this.options.executor.forget?.(run))
|
|
1504
1728
|
.catch((e) => this.options.log?.(`the volumes run ${run.id} kept could not be removed: ${e instanceof Error ? e.message : e}`));
|
|
1505
1729
|
}
|
|
1730
|
+
delete entry.run.cancelling;
|
|
1506
1731
|
entry.run.state = state;
|
|
1507
1732
|
entry.run.finishedAt = this.timestamp();
|
|
1508
1733
|
if (error) {
|
|
@@ -1544,11 +1769,15 @@ class RunRegistry {
|
|
|
1544
1769
|
}
|
|
1545
1770
|
else if (event.type === 'task') {
|
|
1546
1771
|
this.keepResult(entry, event);
|
|
1772
|
+
if (event.state === 'succeeded' || event.state === 'failed') {
|
|
1773
|
+
// it got somewhere since the last restart
|
|
1774
|
+
delete entry.run.resumes;
|
|
1775
|
+
}
|
|
1547
1776
|
}
|
|
1548
1777
|
// an executor that keeps talking can't fill the memory of the server or its disk; the
|
|
1549
1778
|
// state changes, which are few, are always kept, and the results above are kept apart
|
|
1550
1779
|
if (!force && event.type !== 'state') {
|
|
1551
|
-
const size =
|
|
1780
|
+
const size = eventSize(event);
|
|
1552
1781
|
if (entry.events.length >= this.maxEvents || entry.bytes + size > this.maxLogBytes) {
|
|
1553
1782
|
if (!entry.truncated) {
|
|
1554
1783
|
entry.truncated = true;
|
|
@@ -1631,18 +1860,32 @@ class RunRegistry {
|
|
|
1631
1860
|
for (const listener of entry.listeners) {
|
|
1632
1861
|
listener(stored);
|
|
1633
1862
|
}
|
|
1634
|
-
// what a restart must not lose: the runs that wait, and how a run ended
|
|
1635
|
-
// run
|
|
1863
|
+
// what a restart must not lose: the runs that wait, and how a run ended, at once; what a
|
|
1864
|
+
// run says while it executes every so often, as the next server resumes it
|
|
1636
1865
|
if (event.type === 'state' || event.type === 'approval' || event.type === 'extended' || event.type === 'moved') {
|
|
1637
1866
|
this.persist(entry);
|
|
1638
1867
|
}
|
|
1868
|
+
else if (this.options.store && !entry.persistTimer && !this.closed) {
|
|
1869
|
+
entry.persistTimer = setTimeout(() => this.persist(entry), this.options.persistEvery ?? PERSIST_EVERY);
|
|
1870
|
+
entry.persistTimer.unref();
|
|
1871
|
+
}
|
|
1639
1872
|
}
|
|
1640
|
-
|
|
1873
|
+
// `clean`: the server stops, and the record has all the run said
|
|
1874
|
+
persist(entry, clean = false) {
|
|
1641
1875
|
const store = this.options.store;
|
|
1876
|
+
clearTimeout(entry.persistTimer);
|
|
1877
|
+
entry.persistTimer = undefined;
|
|
1642
1878
|
if (!store) {
|
|
1643
1879
|
return;
|
|
1644
1880
|
}
|
|
1645
|
-
|
|
1881
|
+
// the check keys of a run that resumes are the ones from before anything of it ran
|
|
1882
|
+
const keys = isFinal(entry.run.state) || Object.keys(entry.keys).length === 0 ? {} : { keys: { ...entry.keys } };
|
|
1883
|
+
const record = {
|
|
1884
|
+
run: this.copy(entry.run),
|
|
1885
|
+
events: [...entry.events],
|
|
1886
|
+
...keys,
|
|
1887
|
+
...(clean || this.written ? { clean: true } : {}),
|
|
1888
|
+
};
|
|
1646
1889
|
entry.saving = entry.saving
|
|
1647
1890
|
.then(() => store.save(record))
|
|
1648
1891
|
.catch((e) => this.options.log?.(`could not keep run ${record.run.id}: ${e instanceof Error ? e.message : e}`));
|