hammerkit 2.1.0 → 2.2.0-next.1
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/build.schema.json +126 -28
- package/dist/cli.js +5 -0
- package/dist/dashboard/assets/graph-canvas-D6GMgkoe.js +8 -0
- package/dist/dashboard/assets/index-BNq9j5Jo.js +23 -0
- package/dist/dashboard/assets/index-DP3_hKOe.css +2 -0
- package/dist/dashboard/index.html +2 -2
- package/dist/docker/item-network.js +6 -5
- package/dist/docker/remove-orphaned-containers.js +5 -4
- package/dist/environment/replace-env-variables.js +8 -5
- package/dist/environment/run-source-envs.js +2 -1
- package/dist/executer/archive-task-entry.js +1 -1
- package/dist/executer/check-readiness.js +8 -4
- package/dist/executer/docker-healthcheck.js +73 -0
- package/dist/executer/docker-service.js +21 -15
- package/dist/executer/event-cache.js +5 -1
- package/dist/executer/runner-commands.js +36 -0
- package/dist/file/get-file-context.js +4 -7
- package/dist/harness/connect.js +12 -8
- package/dist/harness/reconnect.js +141 -0
- package/dist/harness/sandbox-client.js +241 -33
- package/dist/harness/service-tools.js +11 -2
- package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
- package/dist/kubernetes/ensure-persistent-data.js +1 -1
- package/dist/kubernetes/follow-pod-logs.js +8 -5
- package/dist/kubernetes/kept-claims.js +38 -14
- package/dist/kubernetes/sandbox-runner.js +526 -156
- package/dist/kubernetes/store-kubernetes-data.js +18 -3
- package/dist/kubernetes/sync-changes.js +463 -0
- package/dist/kubernetes/volumes.js +23 -1
- package/dist/network/healthcheck-request.js +38 -0
- package/dist/optimizer/get-work-cache-stats.js +7 -0
- package/dist/optimizer/work-task-cache-description.js +5 -3
- package/dist/planner/image-build.js +5 -0
- package/dist/planner/service-definition.js +2 -1
- package/dist/planner/utils/append-work-service.js +12 -4
- package/dist/planner/utils/dependency-outputs.js +7 -6
- package/dist/planner/validate.js +41 -0
- package/dist/planner/work-healthcheck.js +23 -2
- package/dist/planner/work-item-status.js +12 -2
- package/dist/planner/work-runtime-aggregate.js +1 -0
- package/dist/planner/work-runtime-docker.js +33 -6
- package/dist/planner/work-runtime-kubernetes.js +313 -70
- package/dist/planner/work-runtime-local.js +8 -4
- package/dist/planner/work-secret.js +33 -11
- package/dist/planner/work-service-id.js +30 -6
- package/dist/planner/work-service.js +2 -2
- package/dist/program.js +19 -1
- package/dist/remote/annotations.js +8 -2
- package/dist/remote/bundle.js +6 -8
- package/dist/remote/device-login.js +4 -1
- package/dist/remote/remote-commands.js +16 -7
- package/dist/remote/remote-status.js +1 -1
- package/dist/remote/remote-work-commands.js +220 -68
- package/dist/remote/server-client.js +38 -40
- package/dist/schema/build-file-base-task-schema.js +16 -1
- package/dist/schema/build-file-image-build-schema.js +5 -8
- package/dist/schema/build-file-service-container-healthcheck.js +25 -7
- package/dist/schema/reference-parser.js +108 -43
- package/dist/schema/work-scope-parser.js +8 -0
- package/dist/secrets/prefetch-secrets.js +20 -1
- package/dist/server/artifacts-api.js +56 -0
- package/dist/server/borrowed-quota.js +216 -0
- package/dist/server/capacity.js +18 -6
- package/dist/server/definitions-api.js +62 -8
- package/dist/server/executor.js +117 -30
- package/dist/server/http.js +4 -1
- package/dist/server/keep-artifacts.js +230 -0
- package/dist/server/kubernetes-backend.js +292 -79
- package/dist/server/kubernetes-sandbox-storage.js +86 -24
- package/dist/server/overview.js +73 -9
- package/dist/server/paging.js +167 -0
- package/dist/server/plan.js +16 -1
- package/dist/server/run-access.js +24 -0
- package/dist/server/run-artifacts.js +171 -0
- package/dist/server/run-logs.js +25 -1
- package/dist/server/run-request.js +11 -0
- package/dist/server/run-source-environment.js +1 -1
- package/dist/server/run-worker.js +267 -33
- package/dist/server/runs.js +531 -105
- package/dist/server/sandbox-files-api.js +27 -12
- package/dist/server/sandbox-files.js +12 -10
- package/dist/server/sandbox-follow.js +54 -0
- package/dist/server/sandbox-pod.js +1 -1
- package/dist/server/sandbox-storage.js +38 -1
- package/dist/server/sandbox-sync.js +392 -0
- package/dist/server/sandboxes-api.js +116 -19
- package/dist/server/sandboxes.js +38 -3
- package/dist/server/server-config.js +73 -2
- package/dist/server/server.js +169 -38
- package/dist/server/task-order.js +56 -0
- package/dist/utils/project-path.js +20 -0
- package/dist/utils/tar-files.js +52 -0
- package/dist/utils/tar.js +37 -1
- package/package.json +1 -1
- package/dist/dashboard/assets/graph-canvas-DtJSpova.js +0 -8
- package/dist/dashboard/assets/index-BndChMep.css +0 -2
- package/dist/dashboard/assets/index-DRSwIRXd.js +0 -17
package/dist/server/runs.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
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.isLogCursor = exports.logCursors = exports.MAX_CURSORS_PER_ITEM = 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
|
+
const runner_commands_1 = require("../executer/runner-commands");
|
|
6
7
|
const run_logs_1 = require("./run-logs");
|
|
7
8
|
const units_1 = require("../utils/units");
|
|
8
9
|
const FINAL_STATES = ['succeeded', 'failed', 'cancelled', 'rejected'];
|
|
@@ -30,6 +31,11 @@ function ranFor(result) {
|
|
|
30
31
|
}
|
|
31
32
|
return result.durationMs;
|
|
32
33
|
}
|
|
34
|
+
// Whether the server stops and leaves the run to the next one (RunContext.detached)
|
|
35
|
+
function isDetached(context) {
|
|
36
|
+
return context.signal.aborted && !!context.detached?.();
|
|
37
|
+
}
|
|
38
|
+
exports.isDetached = isDetached;
|
|
33
39
|
// The server can not take the run now; `status` is what the caller is told
|
|
34
40
|
class RunError extends Error {
|
|
35
41
|
constructor() {
|
|
@@ -38,6 +44,14 @@ class RunError extends Error {
|
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
exports.RunError = RunError;
|
|
47
|
+
// The server shuts down and took nothing of the request: a client may send it again, to the
|
|
48
|
+
// server that comes up after it
|
|
49
|
+
class ShuttingDownError extends RunError {
|
|
50
|
+
constructor() {
|
|
51
|
+
super('the server is shutting down');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.ShuttingDownError = ShuttingDownError;
|
|
41
55
|
// A caller is over one of their limits, others may still submit
|
|
42
56
|
class QuotaError extends RunError {
|
|
43
57
|
constructor() {
|
|
@@ -71,6 +85,18 @@ function servicePlaces(runs, asleep = false) {
|
|
|
71
85
|
function onSandbox(run, sandbox) {
|
|
72
86
|
return run.source.type === 'sandbox' && run.source.id === sandbox;
|
|
73
87
|
}
|
|
88
|
+
// How long a server that stops waits for the executions it detaches: a pod gets 30s by
|
|
89
|
+
// default, and the records are written after. The process of a detached run gets less than
|
|
90
|
+
// this to stop its tasks on a host (DETACH_GRACE_MS in executor.ts), so what its execution
|
|
91
|
+
// does after (its directory, a release of its volumes) fits too. A server without a store
|
|
92
|
+
// cancels its runs and waits for them as long as they take (STOP_GRACE_MS, then a kill).
|
|
93
|
+
exports.CLOSE_TIMEOUT = 15000;
|
|
94
|
+
// how often the record of a run that executes is written while it says something
|
|
95
|
+
const PERSIST_EVERY = 2000;
|
|
96
|
+
// how often a run is resumed in a row without getting anywhere, then it fails
|
|
97
|
+
exports.MAX_RESUMES = 3;
|
|
98
|
+
// what a move that waited or executed when the server stopped says
|
|
99
|
+
const MOVE_LOST = 'not applied, the server restarted';
|
|
74
100
|
// the tasks of a run whose check keys are kept, a build file has fewer
|
|
75
101
|
const MAX_KEYED_TASKS = 1000;
|
|
76
102
|
// how much of the end of a task's output its result carries
|
|
@@ -114,6 +140,147 @@ function taskEventSize(event) {
|
|
|
114
140
|
const causes = (event.causes ?? []).reduce((sum, cause) => sum + Buffer.byteLength(cause) + 4, 0);
|
|
115
141
|
return 64 + causes + Buffer.byteLength(event.error ?? '') + (event.commands?.length ?? 0) * 64;
|
|
116
142
|
}
|
|
143
|
+
// what an event weighs against the log limits of a run; a state change weighs nothing, it is
|
|
144
|
+
// always kept
|
|
145
|
+
function eventSize(event) {
|
|
146
|
+
if (event.type === 'state') {
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
return event.type === 'log' ? Buffer.byteLength(event.text) + 64 : event.type === 'task' ? taskEventSize(event) : 64;
|
|
150
|
+
}
|
|
151
|
+
// the instances of the container of a task or service whose cursors a run keeps: the kubelet
|
|
152
|
+
// keeps the log of the one that runs and of the one before it
|
|
153
|
+
exports.MAX_CURSORS_PER_ITEM = 10;
|
|
154
|
+
// lines with the same time of a log, which has nanoseconds
|
|
155
|
+
const MAX_CURSOR_COUNT = 1000000;
|
|
156
|
+
// The cursors of a record, as the registry keeps them: by task, those of a pod's log
|
|
157
|
+
function logCursors(cursors) {
|
|
158
|
+
const kept = new Map();
|
|
159
|
+
if (!cursors || typeof cursors !== 'object' || Array.isArray(cursors)) {
|
|
160
|
+
return kept;
|
|
161
|
+
}
|
|
162
|
+
for (const [item, list] of Object.entries(cursors)) {
|
|
163
|
+
if (kept.size >= MAX_KEYED_TASKS) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
const valid = Array.isArray(list) ? list.filter(isLogCursor).slice(-exports.MAX_CURSORS_PER_ITEM) : [];
|
|
167
|
+
if (valid.length > 0) {
|
|
168
|
+
kept.set(item, valid.map(copyCursor));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return kept;
|
|
172
|
+
}
|
|
173
|
+
exports.logCursors = logCursors;
|
|
174
|
+
function isLogCursor(cursor) {
|
|
175
|
+
const value = cursor;
|
|
176
|
+
return (!!value &&
|
|
177
|
+
typeof value === 'object' &&
|
|
178
|
+
typeof value.container === 'string' &&
|
|
179
|
+
value.container.length > 0 &&
|
|
180
|
+
value.container.length <= 256 &&
|
|
181
|
+
typeof value.at === 'string' &&
|
|
182
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}$/.test(value.at) &&
|
|
183
|
+
Number.isSafeInteger(value.count) &&
|
|
184
|
+
value.count >= 0 &&
|
|
185
|
+
value.count <= MAX_CURSOR_COUNT);
|
|
186
|
+
}
|
|
187
|
+
exports.isLogCursor = isLogCursor;
|
|
188
|
+
function copyCursor(cursor) {
|
|
189
|
+
return { container: cursor.container, at: cursor.at, count: cursor.count };
|
|
190
|
+
}
|
|
191
|
+
// Where the log of an instance got to: in place of the cursor it had, the newest last
|
|
192
|
+
function keepCursor(cursors, item, cursor) {
|
|
193
|
+
const list = cursors.get(item);
|
|
194
|
+
if (!list) {
|
|
195
|
+
if (cursors.size < MAX_KEYED_TASKS) {
|
|
196
|
+
cursors.set(item, [copyCursor(cursor)]);
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const index = list.findIndex((kept) => kept.container === cursor.container);
|
|
201
|
+
if (index >= 0) {
|
|
202
|
+
list.splice(index, 1);
|
|
203
|
+
}
|
|
204
|
+
list.push(copyCursor(cursor));
|
|
205
|
+
list.splice(0, Math.max(0, list.length - exports.MAX_CURSORS_PER_ITEM));
|
|
206
|
+
}
|
|
207
|
+
// without a prototype: an item may be named as a property of any object
|
|
208
|
+
function cursorsOf(cursors) {
|
|
209
|
+
const record = Object.create(null);
|
|
210
|
+
for (const [item, list] of cursors) {
|
|
211
|
+
record[item] = list.map(copyCursor);
|
|
212
|
+
}
|
|
213
|
+
return record;
|
|
214
|
+
}
|
|
215
|
+
// The check keys of a record, as the registry keeps them: by task, a key or null
|
|
216
|
+
function checkKeys(keys) {
|
|
217
|
+
if (!keys || typeof keys !== 'object' || Array.isArray(keys)) {
|
|
218
|
+
return {};
|
|
219
|
+
}
|
|
220
|
+
return Object.fromEntries(Object.entries(keys)
|
|
221
|
+
.filter(([, key]) => key === null || typeof key === 'string')
|
|
222
|
+
.slice(0, MAX_KEYED_TASKS));
|
|
223
|
+
}
|
|
224
|
+
// The commands of a record, as the registry keeps them (`output` events)
|
|
225
|
+
function commandRecords(outputs) {
|
|
226
|
+
if (!outputs || typeof outputs !== 'object' || Array.isArray(outputs)) {
|
|
227
|
+
return {};
|
|
228
|
+
}
|
|
229
|
+
return Object.fromEntries(Object.entries(outputs)
|
|
230
|
+
.flatMap(([command, record]) => {
|
|
231
|
+
const kept = toCommandRecord({ ...record, command });
|
|
232
|
+
return kept ? [[command, kept]] : [];
|
|
233
|
+
})
|
|
234
|
+
.slice(0, runner_commands_1.MAX_OUTPUT_COMMANDS));
|
|
235
|
+
}
|
|
236
|
+
// What an `output` event says of its command, when it is one
|
|
237
|
+
function toCommandRecord(event) {
|
|
238
|
+
const count = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
239
|
+
if (typeof event?.command !== 'string' ||
|
|
240
|
+
!runner_commands_1.COMMAND_ID_PATTERN.test(event.command) ||
|
|
241
|
+
typeof event.task !== 'string' ||
|
|
242
|
+
event.task.length > 1024 ||
|
|
243
|
+
![event.step, event.execution, event.stdout, event.stderr].every(count)) {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
const { task, step, execution, stdout, stderr } = event;
|
|
247
|
+
return {
|
|
248
|
+
task,
|
|
249
|
+
step,
|
|
250
|
+
execution,
|
|
251
|
+
stdout,
|
|
252
|
+
stderr,
|
|
253
|
+
...(event.done === true ? { done: true } : {}),
|
|
254
|
+
...(event.cut === true ? { cut: true } : {}),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
// What keeps the run of a record from executing again: what it runs, from where, is not in it
|
|
258
|
+
function unresumable(run) {
|
|
259
|
+
if (typeof run.id !== 'string' || !exports.RUN_ID_PATTERN.test(run.id)) {
|
|
260
|
+
return 'its id is not one of a run';
|
|
261
|
+
}
|
|
262
|
+
if (!run.owner || typeof run.owner.iss !== 'string' || typeof run.owner.sub !== 'string') {
|
|
263
|
+
return 'it names no owner';
|
|
264
|
+
}
|
|
265
|
+
if (typeof run.definition !== 'string') {
|
|
266
|
+
return 'it names no build file';
|
|
267
|
+
}
|
|
268
|
+
if (!Array.isArray(run.tasks) || !Array.isArray(run.accounts) || !run.options || typeof run.options !== 'object') {
|
|
269
|
+
return 'it names no tasks';
|
|
270
|
+
}
|
|
271
|
+
if (!run.source || !['ref', 'upload', 'sandbox'].includes(run.source.type)) {
|
|
272
|
+
return 'it names no source';
|
|
273
|
+
}
|
|
274
|
+
if (run.up &&
|
|
275
|
+
(typeof run.up.name !== 'string' ||
|
|
276
|
+
!Array.isArray(run.up.services) ||
|
|
277
|
+
!run.up.services.every((service) => typeof service === 'string') ||
|
|
278
|
+
!Array.isArray(run.up.endpoints) ||
|
|
279
|
+
!run.up.endpoints.every((endpoint) => endpoint && typeof endpoint.host === 'string'))) {
|
|
280
|
+
return 'it names no services';
|
|
281
|
+
}
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
117
284
|
// Where runs are kept while the server is up. Everything is scoped to the
|
|
118
285
|
// caller that submitted the run: the same id asked for by someone else is
|
|
119
286
|
// "not found", so ids can't be used to find out which runs exist.
|
|
@@ -126,10 +293,15 @@ class RunRegistry {
|
|
|
126
293
|
this.closed = false;
|
|
127
294
|
// callers that wait for a run to finish, let go when the server stops
|
|
128
295
|
this.waiters = new Set();
|
|
129
|
-
// the moves of service runs that execute
|
|
130
|
-
|
|
296
|
+
// the executions of runs, until they ended, and the moves of service runs that execute
|
|
297
|
+
// beside their services
|
|
298
|
+
this.executions = new Map();
|
|
299
|
+
this.moving = new Map();
|
|
131
300
|
// the records of evicted runs still being removed from the store, waited for by close
|
|
132
301
|
this.removing = new Set();
|
|
302
|
+
// close wrote every run down with all it said: a later write (a stop, an extend, a run that
|
|
303
|
+
// ends after the server stopped waiting for it) has it all too
|
|
304
|
+
this.written = false;
|
|
133
305
|
this.maxConcurrent = options.maxConcurrent ?? 4;
|
|
134
306
|
this.keep = options.keep ?? 200;
|
|
135
307
|
this.maxEvents = options.maxEvents ?? 5000;
|
|
@@ -140,8 +312,8 @@ class RunRegistry {
|
|
|
140
312
|
}
|
|
141
313
|
/**
|
|
142
314
|
* 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
|
-
*
|
|
315
|
+
* again, for what is left of their time, and those that were asleep sleep on. Those that
|
|
316
|
+
* were queued, running or up go back in the queue under their id (resume).
|
|
145
317
|
*/
|
|
146
318
|
async restore() {
|
|
147
319
|
const store = this.options.store;
|
|
@@ -151,8 +323,10 @@ class RunRegistry {
|
|
|
151
323
|
return;
|
|
152
324
|
}
|
|
153
325
|
const { records, skipped } = await store.load();
|
|
326
|
+
// the queue as it was: the runs that executed first, then the others as they were queued
|
|
327
|
+
const resuming = [];
|
|
154
328
|
skipped.forEach((name) => this.options.log?.(`skip run record ${name}: it can not be read`));
|
|
155
|
-
for (const { run, events } of records) {
|
|
329
|
+
for (const { run, events, keys, cursors, outputs, clean } of records) {
|
|
156
330
|
// a move that executed went with the server
|
|
157
331
|
delete run.moving;
|
|
158
332
|
const entry = {
|
|
@@ -164,7 +338,9 @@ class RunRegistry {
|
|
|
164
338
|
bytes: 0,
|
|
165
339
|
tails: new Map(Object.entries(run.results ?? {}).map(([task, result]) => [task, result.lastLog])),
|
|
166
340
|
saving: Promise.resolve(),
|
|
167
|
-
keys:
|
|
341
|
+
keys: checkKeys(keys),
|
|
342
|
+
cursors: logCursors(cursors),
|
|
343
|
+
outputs: commandRecords(outputs),
|
|
168
344
|
};
|
|
169
345
|
this.entries.set(run.id, entry);
|
|
170
346
|
if (run.state === 'awaiting-approval') {
|
|
@@ -177,47 +353,115 @@ class RunRegistry {
|
|
|
177
353
|
this.finish(entry, 'rejected', 'nobody approved it in time');
|
|
178
354
|
}
|
|
179
355
|
}
|
|
180
|
-
else if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
run.volumesKept = true;
|
|
356
|
+
else if (run.state === 'asleep') {
|
|
357
|
+
if (this.maySleep(entry)) {
|
|
358
|
+
this.expireLater(entry);
|
|
359
|
+
if (!this.keepsVolumes()) {
|
|
360
|
+
// the backend no longer keeps them, the sweep below removes them
|
|
361
|
+
delete run.volumesKept;
|
|
187
362
|
}
|
|
188
|
-
this.sleep(entry);
|
|
189
363
|
}
|
|
190
364
|
else {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// the backend no longer keeps them, the sweep below removes them
|
|
195
|
-
delete run.volumesKept;
|
|
365
|
+
// its time ran out while the server was down: it ends now, and gives its place back
|
|
366
|
+
run.stoppedBy ?? (run.stoppedBy = 'ttl');
|
|
367
|
+
this.finish(entry, 'succeeded');
|
|
196
368
|
}
|
|
197
369
|
}
|
|
198
|
-
else if (run.state === '
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
370
|
+
else if (run.state === 'queued' || run.state === 'running' || run.state === 'up') {
|
|
371
|
+
resuming.push({
|
|
372
|
+
entry,
|
|
373
|
+
executed: run.state !== 'queued',
|
|
374
|
+
queuedAt: String(run.queuedAt ?? run.createdAt ?? ''),
|
|
375
|
+
clean: clean === true,
|
|
376
|
+
});
|
|
202
377
|
}
|
|
203
|
-
|
|
204
|
-
|
|
378
|
+
}
|
|
379
|
+
resuming
|
|
380
|
+
.sort((a, b) => Number(b.executed) - Number(a.executed) || a.queuedAt.localeCompare(b.queuedAt))
|
|
381
|
+
.forEach(({ entry, clean }) => this.resume(entry, clean));
|
|
382
|
+
this.evict();
|
|
383
|
+
try {
|
|
384
|
+
this.options.executor.restored?.([...this.entries.values()]
|
|
385
|
+
.filter((entry) => !isFinal(entry.run.state) && (entry.run.volumesKept || entry.resumed))
|
|
386
|
+
.map((entry) => this.copy(entry.run)));
|
|
387
|
+
}
|
|
388
|
+
catch (e) {
|
|
389
|
+
// the server starts all the same, what the backend kept is looked at again on the next start
|
|
390
|
+
this.options.log?.(`the backend could not take back what it kept: ${e instanceof Error ? e.message : e}`);
|
|
391
|
+
}
|
|
392
|
+
this.dispatch();
|
|
393
|
+
}
|
|
394
|
+
// A run that was queued or executing when the server stopped goes back in the queue, under
|
|
395
|
+
// its id and with what it was pinned to; its tasks that finished are taken from the cache.
|
|
396
|
+
// It ends instead when it was to end: a cancel or a stop came while the server stopped, its
|
|
397
|
+
// time ran out, or it took the server down too often. One that can not be read fails.
|
|
398
|
+
// `clean`: the server that stopped wrote it down with all it said.
|
|
399
|
+
resume(entry, clean) {
|
|
400
|
+
const run = entry.run;
|
|
401
|
+
const was = run.state;
|
|
402
|
+
// what an execution before the restart left goes when the run ends before it executes again
|
|
403
|
+
entry.resumed = !!run.startedAt;
|
|
404
|
+
const unreadable = unresumable(run);
|
|
405
|
+
if (unreadable) {
|
|
406
|
+
// enough of a run to show how it ended, to nobody when its owner is not known
|
|
407
|
+
if (!run.owner || typeof run.owner.iss !== 'string' || typeof run.owner.sub !== 'string') {
|
|
408
|
+
run.owner = { iss: '', sub: '' };
|
|
205
409
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
410
|
+
run.tasks = Array.isArray(run.tasks) ? run.tasks : [];
|
|
411
|
+
run.accounts = Array.isArray(run.accounts) ? run.accounts : [];
|
|
412
|
+
run.options = run.options && typeof run.options === 'object' ? run.options : {};
|
|
413
|
+
if (!run.source || typeof run.source !== 'object') {
|
|
414
|
+
run.source = { type: 'upload', snapshot: '' };
|
|
209
415
|
}
|
|
416
|
+
this.finish(entry, 'failed', `the server stopped while the run was ${was} and its record can not resume it: ${unreadable}, submit it again`);
|
|
417
|
+
return;
|
|
210
418
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
419
|
+
if (run.cancelling) {
|
|
420
|
+
this.finish(entry, 'cancelled');
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (run.up && run.stoppedBy) {
|
|
424
|
+
// stopped while it was up, or replaced
|
|
425
|
+
this.finish(entry, 'succeeded');
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (run.up && run.upAt && !(Date.parse(run.expiresAt ?? '') > this.now())) {
|
|
429
|
+
run.stoppedBy = 'ttl';
|
|
430
|
+
this.finish(entry, 'succeeded');
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
// the time a run gets counts from its first start; a service run that was up gets it again
|
|
434
|
+
const limit = this.options.maxDuration;
|
|
435
|
+
if (limit && run.startedAt && !run.upAt && this.now() - Date.parse(run.startedAt) >= limit) {
|
|
436
|
+
this.finish(entry, 'failed', this.tooLong());
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (was !== 'queued') {
|
|
440
|
+
run.resumes = (run.resumes ?? 0) + 1;
|
|
441
|
+
if (run.resumes > exports.MAX_RESUMES) {
|
|
442
|
+
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`);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (!clean) {
|
|
446
|
+
// a server that was killed: what the run said since it was last written down is gone
|
|
447
|
+
run.lostAfter = entry.events.length;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
// what it said counts against its limits as before
|
|
451
|
+
entry.bytes = entry.events.reduce((sum, event) => sum + eventSize(event), 0);
|
|
452
|
+
delete run.sleepsAt;
|
|
453
|
+
run.state = 'queued';
|
|
454
|
+
run.queuedAt = this.timestamp();
|
|
455
|
+
const lost = was !== 'queued' && !clean ? ', the last of its log before is lost' : '';
|
|
456
|
+
this.record(entry, { type: 'log', task: 'hammerkit', stream: 'stdout', text: `resumed after a server restart${lost}` }, true);
|
|
457
|
+
this.record(entry, { type: 'state', state: 'queued' });
|
|
458
|
+
this.queue.push(entry);
|
|
215
459
|
}
|
|
216
460
|
// A run that needs approvals (for the accounts named) waits for them, the others
|
|
217
461
|
// are queued at once.
|
|
218
462
|
submit(owner, spec, approvalAccounts = []) {
|
|
219
463
|
if (this.closed) {
|
|
220
|
-
throw new
|
|
464
|
+
throw new ShuttingDownError();
|
|
221
465
|
}
|
|
222
466
|
const waiting = approvalAccounts.length > 0;
|
|
223
467
|
if (waiting && this.waiting().length >= this.maxWaiting) {
|
|
@@ -254,6 +498,7 @@ class RunRegistry {
|
|
|
254
498
|
tails: new Map(),
|
|
255
499
|
saving: Promise.resolve(),
|
|
256
500
|
keys: {},
|
|
501
|
+
cursors: new Map(),
|
|
257
502
|
};
|
|
258
503
|
this.entries.set(run.id, entry);
|
|
259
504
|
if (waiting) {
|
|
@@ -466,6 +711,26 @@ class RunRegistry {
|
|
|
466
711
|
entry.run.state !== 'up' &&
|
|
467
712
|
entry.run.state !== 'asleep')?.run.id;
|
|
468
713
|
}
|
|
714
|
+
// The service runs of a sandbox that take its changed files: up, and not replaced or stopped
|
|
715
|
+
// (a run of the same name that replaces one stops it, and it is still up until it ended, so
|
|
716
|
+
// only the newest of a name syncs). A host's executor takes none: its sandbox's files are live.
|
|
717
|
+
upOn(sandbox) {
|
|
718
|
+
if (!this.options.executor.syncFiles) {
|
|
719
|
+
return [];
|
|
720
|
+
}
|
|
721
|
+
return [...this.entries.values()]
|
|
722
|
+
.filter((entry) => entry.run.state === 'up' && !entry.run.stoppedBy && onSandbox(entry.run, sandbox))
|
|
723
|
+
.map((entry) => this.copy(entry.run));
|
|
724
|
+
}
|
|
725
|
+
// Hands the changed files of a sandbox to the services of a service run that is up. False when
|
|
726
|
+
// the run is not known here, is stopped, or its executor can not take them.
|
|
727
|
+
syncFiles(id, changes) {
|
|
728
|
+
const entry = this.entries.get(id);
|
|
729
|
+
if (!entry || entry.run.stoppedBy) {
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
return this.options.executor.syncFiles?.(this.copy(entry.run), changes) ?? false;
|
|
733
|
+
}
|
|
469
734
|
// Cancels every run on a sandbox and resolves when none of them runs anymore.
|
|
470
735
|
async stopSandbox(sandbox, reason) {
|
|
471
736
|
const on = () => [...this.entries.values()].filter((entry) => onSandbox(entry.run, sandbox));
|
|
@@ -484,12 +749,16 @@ class RunRegistry {
|
|
|
484
749
|
this.finish(entry, 'cancelled', reason);
|
|
485
750
|
}
|
|
486
751
|
else if (entry.run.state === 'running') {
|
|
752
|
+
// written down at once: a server that stops before the run did ends it on its next start
|
|
753
|
+
entry.run.cancelling = true;
|
|
754
|
+
this.persist(entry);
|
|
487
755
|
entry.abort.abort();
|
|
488
756
|
}
|
|
489
757
|
else if (entry.run.state === 'up') {
|
|
490
758
|
(_a = entry.run).stoppedBy ?? (_a.stoppedBy = by ?? 'replaced');
|
|
491
759
|
entry.sleeping = false;
|
|
492
760
|
entry.restartTo = undefined;
|
|
761
|
+
this.persist(entry);
|
|
493
762
|
entry.mover?.abort.abort();
|
|
494
763
|
entry.abort.abort();
|
|
495
764
|
}
|
|
@@ -566,7 +835,11 @@ class RunRegistry {
|
|
|
566
835
|
if (!entry) {
|
|
567
836
|
return undefined;
|
|
568
837
|
}
|
|
569
|
-
|
|
838
|
+
// a cursor past the end comes from a server that was killed after it sent what it did not
|
|
839
|
+
// write down: those events are gone, the ones said again since have their numbers
|
|
840
|
+
const last = entry.events.length;
|
|
841
|
+
const from = after > last ? Math.min(entry.run.lostAfter ?? last, last) : after;
|
|
842
|
+
const events = entry.events.filter((event) => event.seq > from);
|
|
570
843
|
const finished = FINAL_STATES.includes(entry.run.state);
|
|
571
844
|
if (!finished) {
|
|
572
845
|
entry.listeners.add(listener);
|
|
@@ -614,46 +887,47 @@ class RunRegistry {
|
|
|
614
887
|
if (!entry) {
|
|
615
888
|
return undefined;
|
|
616
889
|
}
|
|
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 };
|
|
890
|
+
return this.logOf(entry);
|
|
621
891
|
}
|
|
622
892
|
// The log of any run, for a caller whose right to read it was checked already (an
|
|
623
893
|
// entitlement with `observe`).
|
|
624
894
|
logsOf(id) {
|
|
625
895
|
const entry = this.entries.get(id);
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
896
|
+
return entry ? this.logOf(entry) : undefined;
|
|
897
|
+
}
|
|
898
|
+
logOf(entry) {
|
|
899
|
+
// only the events since the last read are looked at
|
|
629
900
|
entry.index ?? (entry.index = new run_logs_1.LogIndex());
|
|
630
901
|
entry.index.update(entry.events);
|
|
631
|
-
return {
|
|
902
|
+
return {
|
|
903
|
+
events: entry.events,
|
|
904
|
+
index: entry.index,
|
|
905
|
+
truncated: entry.truncated,
|
|
906
|
+
...(entry.run.lostAfter !== undefined ? { lostAfter: entry.run.lostAfter } : {}),
|
|
907
|
+
};
|
|
632
908
|
}
|
|
633
|
-
|
|
909
|
+
/**
|
|
910
|
+
* Stops accepting runs. Without a store the runs in flight are cancelled. With one they are
|
|
911
|
+
* detached: a queued run stays in the queue of its record, and a running one or a service
|
|
912
|
+
* run that is up stops its execution without cancelling what it started (RunContext.detached).
|
|
913
|
+
* The next start takes them all back (restore). The executions get `closeTimeout` to stop,
|
|
914
|
+
* then every run is written down as it is.
|
|
915
|
+
*/
|
|
634
916
|
async close() {
|
|
635
917
|
this.closed = true;
|
|
918
|
+
const detach = !!this.options.store;
|
|
636
919
|
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 {
|
|
920
|
+
if (!detach) {
|
|
642
921
|
this.finish(entry, 'cancelled');
|
|
643
922
|
}
|
|
644
923
|
}
|
|
645
924
|
for (const entry of this.entries.values()) {
|
|
646
|
-
if (entry.run.state === '
|
|
647
|
-
this.
|
|
925
|
+
if (entry.run.state === 'awaiting-approval' && !detach) {
|
|
926
|
+
this.finish(entry, 'cancelled');
|
|
648
927
|
}
|
|
649
|
-
else if (entry.run.state
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
this.clearTimer(entry);
|
|
653
|
-
}
|
|
654
|
-
else {
|
|
655
|
-
this.finish(entry, 'cancelled');
|
|
656
|
-
}
|
|
928
|
+
else if (!isFinal(entry.run.state)) {
|
|
929
|
+
// the record keeps it waiting, asleep or up; the next start takes it back, with its time
|
|
930
|
+
this.clearTimer(entry);
|
|
657
931
|
}
|
|
658
932
|
}
|
|
659
933
|
// who waits for a run that waits for an approval is told how it stands now
|
|
@@ -661,19 +935,44 @@ class RunRegistry {
|
|
|
661
935
|
done();
|
|
662
936
|
}
|
|
663
937
|
for (const entry of this.entries.values()) {
|
|
938
|
+
if (detach && !isFinal(entry.run.state)) {
|
|
939
|
+
// the next server starts the run at the commit it is at
|
|
940
|
+
this.dropMove(entry);
|
|
941
|
+
}
|
|
664
942
|
if (entry.run.state === 'running' || entry.run.state === 'up') {
|
|
665
|
-
//
|
|
666
|
-
|
|
667
|
-
|
|
943
|
+
// one that stops already (a cancel, its ttl, a sleep, a move) ends as it would
|
|
944
|
+
if (detach && !entry.abort.signal.aborted) {
|
|
945
|
+
entry.detached = true;
|
|
946
|
+
clearTimeout(entry.durationTimer);
|
|
947
|
+
}
|
|
668
948
|
entry.mover?.abort.abort();
|
|
669
949
|
entry.abort.abort();
|
|
670
950
|
}
|
|
671
951
|
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
952
|
+
// a server without a store waits for its cancels to clean up, however long they take
|
|
953
|
+
const deadline = detach ? Date.now() + (this.options.closeTimeout ?? exports.CLOSE_TIMEOUT) : Infinity;
|
|
954
|
+
const stopping = () => new Set([
|
|
955
|
+
...this.executions.values(),
|
|
956
|
+
...this.moving.values(),
|
|
957
|
+
...[...this.entries.values()].filter((entry) => (entry.run.state === 'running' || entry.run.state === 'up') && !entry.detached),
|
|
958
|
+
]);
|
|
959
|
+
while (stopping().size > 0 && Date.now() < deadline) {
|
|
675
960
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
676
961
|
}
|
|
962
|
+
const left = stopping().size;
|
|
963
|
+
if (left > 0) {
|
|
964
|
+
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'}`);
|
|
965
|
+
}
|
|
966
|
+
if (detach) {
|
|
967
|
+
// with all they said, for the start that resumes them
|
|
968
|
+
this.written = true;
|
|
969
|
+
for (const entry of this.entries.values()) {
|
|
970
|
+
clearTimeout(entry.persistTimer);
|
|
971
|
+
if (!isFinal(entry.run.state)) {
|
|
972
|
+
this.persist(entry, true);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
}
|
|
677
976
|
// the runs that were stopped are written down before the server is gone, and the records
|
|
678
977
|
// of evicted runs are gone from the store
|
|
679
978
|
await Promise.all([...this.entries.values()].map((entry) => entry.saving));
|
|
@@ -808,24 +1107,26 @@ class RunRegistry {
|
|
|
808
1107
|
runningOf(owner) {
|
|
809
1108
|
return this.usage(owner).running.length;
|
|
810
1109
|
}
|
|
811
|
-
// The run on the same sandbox that executes: a sandbox runs one at a time.
|
|
1110
|
+
// The run on the same sandbox that executes: a sandbox runs one at a time. One that executed
|
|
1111
|
+
// before a restart and resumes holds it from the queue too: what it runs on in the sandbox's
|
|
1112
|
+
// runner is not another run's to clean up.
|
|
812
1113
|
sandboxBusy(run) {
|
|
813
1114
|
const source = run.source;
|
|
814
1115
|
if (source.type !== 'sandbox') {
|
|
815
1116
|
return undefined;
|
|
816
1117
|
}
|
|
817
|
-
|
|
818
|
-
|
|
1118
|
+
const resuming = this.entries.get(run.id)?.resumed;
|
|
1119
|
+
return [...this.entries.values()].find((entry) => onSandbox(entry.run, source.id) &&
|
|
1120
|
+
(entry.run.state === 'running' ||
|
|
1121
|
+
(entry.run.state === 'queued' && entry.resumed && !resuming && entry.run.id !== run.id)))?.run.id;
|
|
819
1122
|
}
|
|
820
|
-
// The run that executes and holds what the run would (RunExecutor.
|
|
1123
|
+
// The run that executes and holds what the run would (RunExecutor.excludes)
|
|
821
1124
|
held(run) {
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
if (key === undefined) {
|
|
1125
|
+
const executor = this.options.executor;
|
|
1126
|
+
if (!executor.excludes) {
|
|
825
1127
|
return undefined;
|
|
826
1128
|
}
|
|
827
|
-
return [...this.entries.values()].find((entry) => entry.run.state === 'running' &&
|
|
828
|
-
?.run.id;
|
|
1129
|
+
return [...this.entries.values()].find((entry) => entry.run.state === 'running' && entry.run.id !== run.id && executor.excludes?.(run, entry.run))?.run.id;
|
|
829
1130
|
}
|
|
830
1131
|
// What keeps a queued run from starting, checked as dispatch checks it: what holds it
|
|
831
1132
|
// even when a place frees up is named first.
|
|
@@ -854,7 +1155,8 @@ class RunRegistry {
|
|
|
854
1155
|
// does not keep the others from the machine. A run on a sandbox another run works on
|
|
855
1156
|
// waits for it, as does one that needs what an executing run holds.
|
|
856
1157
|
dispatch() {
|
|
857
|
-
|
|
1158
|
+
// the runs left in the queue start with the next server
|
|
1159
|
+
while (this.running < this.maxConcurrent && !this.closed) {
|
|
858
1160
|
const perCaller = this.options.limits?.running ?? Infinity;
|
|
859
1161
|
const index = this.queue.findIndex((entry) => this.runningOf(entry.run.owner) < perCaller && !this.sandboxBusy(entry.run) && !this.held(entry.run));
|
|
860
1162
|
if (index < 0) {
|
|
@@ -868,26 +1170,36 @@ class RunRegistry {
|
|
|
868
1170
|
}
|
|
869
1171
|
const [entry] = this.queue.splice(index, 1);
|
|
870
1172
|
this.running++;
|
|
871
|
-
this.execute(entry).finally(() => {
|
|
1173
|
+
const execution = this.execute(entry).finally(() => {
|
|
1174
|
+
this.executions.delete(execution);
|
|
872
1175
|
// a service run that is up gave its place back already
|
|
873
1176
|
if (!entry.released) {
|
|
874
1177
|
this.running--;
|
|
875
1178
|
}
|
|
876
1179
|
this.dispatch();
|
|
877
1180
|
});
|
|
1181
|
+
this.executions.set(execution, entry);
|
|
878
1182
|
}
|
|
879
1183
|
}
|
|
880
1184
|
async execute(entry) {
|
|
1185
|
+
// a run that resumes executes again: what it left is this execution's to remove, and its
|
|
1186
|
+
// time counts from its first start (a service run that was up gets it again)
|
|
1187
|
+
const resumed = !!entry.resumed;
|
|
1188
|
+
const continues = resumed && !!entry.run.startedAt && !entry.run.upAt;
|
|
1189
|
+
entry.resumed = false;
|
|
881
1190
|
entry.run.state = 'running';
|
|
882
|
-
|
|
1191
|
+
if (!continues) {
|
|
1192
|
+
entry.run.startedAt = this.timestamp();
|
|
1193
|
+
}
|
|
883
1194
|
this.record(entry, { type: 'state', state: 'running' });
|
|
884
1195
|
// a service run gets this long to be up, then its ttl counts
|
|
885
1196
|
const limit = this.options.maxDuration;
|
|
1197
|
+
const ran = continues ? Math.max(0, this.now() - Date.parse(entry.run.startedAt)) : 0;
|
|
886
1198
|
entry.durationTimer = limit
|
|
887
1199
|
? setTimeout(() => {
|
|
888
1200
|
entry.timedOut = true;
|
|
889
1201
|
entry.abort.abort();
|
|
890
|
-
}, limit)
|
|
1202
|
+
}, Math.max(0, limit - ran))
|
|
891
1203
|
: undefined;
|
|
892
1204
|
entry.durationTimer?.unref();
|
|
893
1205
|
const token = {};
|
|
@@ -902,7 +1214,26 @@ class RunRegistry {
|
|
|
902
1214
|
await this.options.executor.execute(run, {
|
|
903
1215
|
emit: (event) => this.record(entry, event),
|
|
904
1216
|
signal: entry.abort.signal,
|
|
1217
|
+
detached: () => !!entry.detached,
|
|
1218
|
+
...(resumed
|
|
1219
|
+
? {
|
|
1220
|
+
resumed: {
|
|
1221
|
+
logs: cursorsOf(entry.cursors),
|
|
1222
|
+
// the commands it started in a sandbox's runner, which a run that went on
|
|
1223
|
+
// takes up again
|
|
1224
|
+
...(continues ? { outputs: { ...entry.outputs } } : {}),
|
|
1225
|
+
},
|
|
1226
|
+
}
|
|
1227
|
+
: {}),
|
|
905
1228
|
...(Object.keys(durations).length > 0 ? { durations } : {}),
|
|
1229
|
+
...(this.options.artifacts && !entry.run.up
|
|
1230
|
+
? {
|
|
1231
|
+
artifacts: {
|
|
1232
|
+
directory: this.options.artifacts.directoryOf(run.id),
|
|
1233
|
+
maxBytes: this.options.artifacts.maxBytes,
|
|
1234
|
+
},
|
|
1235
|
+
}
|
|
1236
|
+
: {}),
|
|
906
1237
|
...(entry.run.up
|
|
907
1238
|
? {
|
|
908
1239
|
up: (targets) => this.markUp(entry, targets),
|
|
@@ -910,8 +1241,8 @@ class RunRegistry {
|
|
|
910
1241
|
}
|
|
911
1242
|
: {}),
|
|
912
1243
|
});
|
|
913
|
-
if (entry.holder !== token) {
|
|
914
|
-
// a move took the services over
|
|
1244
|
+
if (entry.holder !== token || entry.detached) {
|
|
1245
|
+
// a move took the services over, or the run resumes with the next server
|
|
915
1246
|
return;
|
|
916
1247
|
}
|
|
917
1248
|
if (this.fallBack(entry, entry.timedOut ? this.tooLong() : 'its services stopped before they were up')) {
|
|
@@ -930,7 +1261,7 @@ class RunRegistry {
|
|
|
930
1261
|
}
|
|
931
1262
|
catch (e) {
|
|
932
1263
|
const message = e instanceof Error ? e.message : String(e);
|
|
933
|
-
if (entry.holder !== token) {
|
|
1264
|
+
if (entry.holder !== token || entry.detached) {
|
|
934
1265
|
return;
|
|
935
1266
|
}
|
|
936
1267
|
if (this.fallBack(entry, entry.timedOut ? this.tooLong() : message)) {
|
|
@@ -969,6 +1300,7 @@ class RunRegistry {
|
|
|
969
1300
|
this.moveTo(entry, landing.change);
|
|
970
1301
|
}
|
|
971
1302
|
entry.run.state = 'up';
|
|
1303
|
+
delete entry.run.resumes;
|
|
972
1304
|
// one reading of the clock, so the ttl is exactly expiresAt - upAt
|
|
973
1305
|
const now = this.now();
|
|
974
1306
|
// the ttl counts from the first time, not from a wake
|
|
@@ -1175,7 +1507,7 @@ class RunRegistry {
|
|
|
1175
1507
|
...(change.plan ? { plan: change.plan } : {}),
|
|
1176
1508
|
};
|
|
1177
1509
|
const execution = this.executeMove(entry, token, abort, run);
|
|
1178
|
-
this.moving.
|
|
1510
|
+
this.moving.set(execution, entry);
|
|
1179
1511
|
void execution.finally(() => this.moving.delete(execution));
|
|
1180
1512
|
}
|
|
1181
1513
|
// A move beside the services of a run that is up: once its services are ready it holds them
|
|
@@ -1186,6 +1518,7 @@ class RunRegistry {
|
|
|
1186
1518
|
await this.options.executor.execute(run, {
|
|
1187
1519
|
emit: (event) => this.record(entry, event),
|
|
1188
1520
|
signal: abort.signal,
|
|
1521
|
+
detached: () => !!entry.detached,
|
|
1189
1522
|
up: (targets) => this.moved(entry, token, targets),
|
|
1190
1523
|
keep: () => (entry.holder === token ? this.keepOf(entry) : 'everything'),
|
|
1191
1524
|
});
|
|
@@ -1199,6 +1532,10 @@ class RunRegistry {
|
|
|
1199
1532
|
failure = `the move took longer than ${(0, units_1.formatDuration)(this.options.maxDuration)} and was stopped`;
|
|
1200
1533
|
}
|
|
1201
1534
|
this.releaseMover(mover);
|
|
1535
|
+
if (entry.detached) {
|
|
1536
|
+
// the run resumes with the next server, at the commit it is at
|
|
1537
|
+
return;
|
|
1538
|
+
}
|
|
1202
1539
|
if (entry.holder === token) {
|
|
1203
1540
|
// the run's services were this execution's
|
|
1204
1541
|
if (entry.restartTo || entry.sleeping) {
|
|
@@ -1266,12 +1603,7 @@ class RunRegistry {
|
|
|
1266
1603
|
if (this.closed) {
|
|
1267
1604
|
// the record keeps it asleep, at the commit it was up at
|
|
1268
1605
|
this.moveTo(entry, restart, 'the server stopped before the services were up at the commit');
|
|
1269
|
-
|
|
1270
|
-
this.sleep(entry);
|
|
1271
|
-
}
|
|
1272
|
-
else {
|
|
1273
|
-
this.finish(entry, 'failed', 'the server stopped while the services were up, start them again');
|
|
1274
|
-
}
|
|
1606
|
+
this.leaveForRestart(entry);
|
|
1275
1607
|
return;
|
|
1276
1608
|
}
|
|
1277
1609
|
// up at its commit, the run is there (markUp); else it goes back (fallBack)
|
|
@@ -1308,17 +1640,49 @@ class RunRegistry {
|
|
|
1308
1640
|
text: `did not get up at ${landing.change.source.commit}, starting again at ${from.commit}: ${error}`,
|
|
1309
1641
|
});
|
|
1310
1642
|
if (this.closed) {
|
|
1311
|
-
|
|
1312
|
-
this.sleep(entry);
|
|
1313
|
-
}
|
|
1314
|
-
else {
|
|
1315
|
-
this.finish(entry, 'failed', 'the server stopped while the services were up, start them again');
|
|
1316
|
-
}
|
|
1643
|
+
this.leaveForRestart(entry);
|
|
1317
1644
|
return true;
|
|
1318
1645
|
}
|
|
1319
1646
|
this.requeue(entry);
|
|
1320
1647
|
return true;
|
|
1321
1648
|
}
|
|
1649
|
+
// The server stops: a move that waits, executes beside the services or starts them at its
|
|
1650
|
+
// commit does not get there, the run stays at the commit it is at and says so
|
|
1651
|
+
dropMove(entry) {
|
|
1652
|
+
const landing = entry.landing && !entry.landing.recorded ? entry.landing : undefined;
|
|
1653
|
+
const change = entry.mover?.change ?? entry.pendingMove ?? landing?.change;
|
|
1654
|
+
if (!change) {
|
|
1655
|
+
return;
|
|
1656
|
+
}
|
|
1657
|
+
entry.pendingMove = undefined;
|
|
1658
|
+
if (landing) {
|
|
1659
|
+
entry.landing = undefined;
|
|
1660
|
+
entry.run.source = { ...landing.from.source };
|
|
1661
|
+
if (landing.from.plan) {
|
|
1662
|
+
entry.run.plan = landing.from.plan;
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
// the execution of a move ends with the server, its record says where it got
|
|
1666
|
+
const mover = entry.mover;
|
|
1667
|
+
entry.mover = undefined;
|
|
1668
|
+
this.moveTo(entry, change, MOVE_LOST);
|
|
1669
|
+
entry.mover = mover;
|
|
1670
|
+
}
|
|
1671
|
+
// The server stops while the services of a run stop to start again at another commit: it
|
|
1672
|
+
// sleeps in its record if it may, else the next server resumes it at the commit it was up at
|
|
1673
|
+
leaveForRestart(entry) {
|
|
1674
|
+
if (this.maySleep(entry) && this.options.store) {
|
|
1675
|
+
this.sleep(entry);
|
|
1676
|
+
}
|
|
1677
|
+
else if (this.options.store) {
|
|
1678
|
+
entry.targets = undefined;
|
|
1679
|
+
entry.run.state = 'queued';
|
|
1680
|
+
this.record(entry, { type: 'state', state: 'queued' });
|
|
1681
|
+
}
|
|
1682
|
+
else {
|
|
1683
|
+
this.finish(entry, 'failed', 'the server stopped while the services were up, start them again');
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1322
1686
|
// where a run that moves goes back to: the commit it is at (service runs start from a ref)
|
|
1323
1687
|
landingFrom(entry) {
|
|
1324
1688
|
return {
|
|
@@ -1387,8 +1751,8 @@ class RunRegistry {
|
|
|
1387
1751
|
// the record, so the volumes are removed (RunExecutor.forget) when the run ends asleep, or
|
|
1388
1752
|
// when a stop came after the sleep began.
|
|
1389
1753
|
keepOf(entry) {
|
|
1390
|
-
// a run that starts again at another commit keeps them on any server
|
|
1391
|
-
if (entry.restartTo || entry.landing || (entry.sleeping && this.keepsVolumes())) {
|
|
1754
|
+
// a run that starts again at another commit, or with the next server, keeps them on any server
|
|
1755
|
+
if (entry.restartTo || entry.landing || entry.detached || (entry.sleeping && this.keepsVolumes())) {
|
|
1392
1756
|
entry.run.volumesKept = true;
|
|
1393
1757
|
return 'volumes';
|
|
1394
1758
|
}
|
|
@@ -1425,7 +1789,7 @@ class RunRegistry {
|
|
|
1425
1789
|
}
|
|
1426
1790
|
const sleepTtl = entry.run.up.sleepTtl;
|
|
1427
1791
|
if (entry.run.state === 'up' && sleepTtl) {
|
|
1428
|
-
// only kept in memory: a
|
|
1792
|
+
// only kept in memory: a run that resumes after a restart is up for its sleepTtl from then
|
|
1429
1793
|
entry.run.sleepsAt = new Date(this.now() + sleepTtl).toISOString();
|
|
1430
1794
|
return undefined;
|
|
1431
1795
|
}
|
|
@@ -1461,7 +1825,7 @@ class RunRegistry {
|
|
|
1461
1825
|
return undefined;
|
|
1462
1826
|
}
|
|
1463
1827
|
// The end of a run that was up: stopped by somebody, its ttl or a newer run it succeeded,
|
|
1464
|
-
// stopped by
|
|
1828
|
+
// stopped by a server without a store going away it failed (one with a store detaches it).
|
|
1465
1829
|
endServices(entry, failure) {
|
|
1466
1830
|
if (entry.timedOut) {
|
|
1467
1831
|
this.finish(entry, 'failed', this.tooLong());
|
|
@@ -1489,14 +1853,17 @@ class RunRegistry {
|
|
|
1489
1853
|
entry.mover?.abort.abort();
|
|
1490
1854
|
entry.mover = undefined;
|
|
1491
1855
|
delete entry.run.moving;
|
|
1492
|
-
if (entry.run.volumesKept) {
|
|
1493
|
-
// it ends without waking: what was kept for it
|
|
1856
|
+
if (entry.run.volumesKept || entry.resumed) {
|
|
1857
|
+
// it ends without waking, or after a restart: what was kept for it, or what an execution
|
|
1858
|
+
// before the restart left, goes
|
|
1494
1859
|
delete entry.run.volumesKept;
|
|
1860
|
+
entry.resumed = false;
|
|
1495
1861
|
const run = this.copy(entry.run);
|
|
1496
1862
|
Promise.resolve()
|
|
1497
1863
|
.then(() => this.options.executor.forget?.(run))
|
|
1498
1864
|
.catch((e) => this.options.log?.(`the volumes run ${run.id} kept could not be removed: ${e instanceof Error ? e.message : e}`));
|
|
1499
1865
|
}
|
|
1866
|
+
delete entry.run.cancelling;
|
|
1500
1867
|
entry.run.state = state;
|
|
1501
1868
|
entry.run.finishedAt = this.timestamp();
|
|
1502
1869
|
if (error) {
|
|
@@ -1532,17 +1899,33 @@ class RunRegistry {
|
|
|
1532
1899
|
this.keepPhase(entry, recorded);
|
|
1533
1900
|
return;
|
|
1534
1901
|
}
|
|
1902
|
+
if (recorded.type === 'log' && 'cursor' in recorded) {
|
|
1903
|
+
// with the line it came with: a record has the cursor of the last line it has
|
|
1904
|
+
const { cursor, ...line } = recorded;
|
|
1905
|
+
if (isLogCursor(cursor)) {
|
|
1906
|
+
keepCursor(entry.cursors, recorded.task, cursor);
|
|
1907
|
+
}
|
|
1908
|
+
recorded = line;
|
|
1909
|
+
}
|
|
1910
|
+
if (recorded.type === 'output') {
|
|
1911
|
+
this.keepCommand(entry, recorded);
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1535
1914
|
const event = recorded.type === 'task' ? bounded(recorded) : recorded;
|
|
1536
1915
|
if (event.type === 'log') {
|
|
1537
1916
|
this.keepTail(entry, event.task, event.text);
|
|
1538
1917
|
}
|
|
1539
1918
|
else if (event.type === 'task') {
|
|
1540
1919
|
this.keepResult(entry, event);
|
|
1920
|
+
if (event.state === 'succeeded' || event.state === 'failed') {
|
|
1921
|
+
// it got somewhere since the last restart
|
|
1922
|
+
delete entry.run.resumes;
|
|
1923
|
+
}
|
|
1541
1924
|
}
|
|
1542
1925
|
// an executor that keeps talking can't fill the memory of the server or its disk; the
|
|
1543
1926
|
// state changes, which are few, are always kept, and the results above are kept apart
|
|
1544
1927
|
if (!force && event.type !== 'state') {
|
|
1545
|
-
const size =
|
|
1928
|
+
const size = eventSize(event);
|
|
1546
1929
|
if (entry.events.length >= this.maxEvents || entry.bytes + size > this.maxLogBytes) {
|
|
1547
1930
|
if (!entry.truncated) {
|
|
1548
1931
|
entry.truncated = true;
|
|
@@ -1554,6 +1937,29 @@ class RunRegistry {
|
|
|
1554
1937
|
}
|
|
1555
1938
|
this.push(entry, event);
|
|
1556
1939
|
}
|
|
1940
|
+
// the newest of each command; past MAX_OUTPUT_COMMANDS one that ended makes way for a new one,
|
|
1941
|
+
// which a run that resumes would start again
|
|
1942
|
+
keepCommand(entry, event) {
|
|
1943
|
+
const record = toCommandRecord(event);
|
|
1944
|
+
if (!record) {
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1947
|
+
const outputs = (entry.outputs ?? (entry.outputs = {}));
|
|
1948
|
+
const started = !(event.command in outputs);
|
|
1949
|
+
if (started && Object.keys(outputs).length >= runner_commands_1.MAX_OUTPUT_COMMANDS) {
|
|
1950
|
+
const ended = Object.keys(outputs).find((command) => outputs[command].done);
|
|
1951
|
+
if (!ended) {
|
|
1952
|
+
return;
|
|
1953
|
+
}
|
|
1954
|
+
delete outputs[ended];
|
|
1955
|
+
}
|
|
1956
|
+
outputs[event.command] = record;
|
|
1957
|
+
if (started && !this.closed) {
|
|
1958
|
+
// written down at once: a server killed before its next write would have the run resume
|
|
1959
|
+
// without it, start the command again and kill the one that runs
|
|
1960
|
+
this.persist(entry);
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1557
1963
|
// once each, from the executor: a phase it reports again (a service run that wakes) is the
|
|
1558
1964
|
// newest
|
|
1559
1965
|
keepPhase(entry, event) {
|
|
@@ -1625,18 +2031,38 @@ class RunRegistry {
|
|
|
1625
2031
|
for (const listener of entry.listeners) {
|
|
1626
2032
|
listener(stored);
|
|
1627
2033
|
}
|
|
1628
|
-
// what a restart must not lose: the runs that wait, and how a run ended
|
|
1629
|
-
// run
|
|
2034
|
+
// what a restart must not lose: the runs that wait, and how a run ended, at once; what a
|
|
2035
|
+
// run says while it executes every so often, as the next server resumes it
|
|
1630
2036
|
if (event.type === 'state' || event.type === 'approval' || event.type === 'extended' || event.type === 'moved') {
|
|
1631
2037
|
this.persist(entry);
|
|
1632
2038
|
}
|
|
2039
|
+
else if (this.options.store && !entry.persistTimer && !this.closed) {
|
|
2040
|
+
entry.persistTimer = setTimeout(() => this.persist(entry), this.options.persistEvery ?? PERSIST_EVERY);
|
|
2041
|
+
entry.persistTimer.unref();
|
|
2042
|
+
}
|
|
1633
2043
|
}
|
|
1634
|
-
|
|
2044
|
+
// `clean`: the server stops, and the record has all the run said
|
|
2045
|
+
persist(entry, clean = false) {
|
|
1635
2046
|
const store = this.options.store;
|
|
2047
|
+
clearTimeout(entry.persistTimer);
|
|
2048
|
+
entry.persistTimer = undefined;
|
|
1636
2049
|
if (!store) {
|
|
1637
2050
|
return;
|
|
1638
2051
|
}
|
|
1639
|
-
|
|
2052
|
+
// the check keys of a run that resumes are the ones from before anything of it ran
|
|
2053
|
+
const keys = isFinal(entry.run.state) || Object.keys(entry.keys).length === 0 ? {} : { keys: { ...entry.keys } };
|
|
2054
|
+
// where the logs of its pods got to, for the next server that resumes it
|
|
2055
|
+
const cursors = isFinal(entry.run.state) || entry.cursors.size === 0 ? {} : { cursors: cursorsOf(entry.cursors) };
|
|
2056
|
+
// what the log of a run that resumes holds of the output of its commands, as far as `events` go
|
|
2057
|
+
const outputs = isFinal(entry.run.state) || Object.keys(entry.outputs ?? {}).length === 0 ? {} : { outputs: { ...entry.outputs } };
|
|
2058
|
+
const record = {
|
|
2059
|
+
run: this.copy(entry.run),
|
|
2060
|
+
events: [...entry.events],
|
|
2061
|
+
...keys,
|
|
2062
|
+
...cursors,
|
|
2063
|
+
...outputs,
|
|
2064
|
+
...(clean || this.written ? { clean: true } : {}),
|
|
2065
|
+
};
|
|
1640
2066
|
entry.saving = entry.saving
|
|
1641
2067
|
.then(() => store.save(record))
|
|
1642
2068
|
.catch((e) => this.options.log?.(`could not keep run ${record.run.id}: ${e instanceof Error ? e.message : e}`));
|