hammerkit 2.2.0-next.6 → 2.2.0-next.7
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/kubernetes/sandbox-runner.js +76 -31
- package/dist/server/executor.js +83 -15
- package/dist/server/run-worker.js +38 -3
- package/package.json +1 -1
|
@@ -118,6 +118,13 @@ exports.getRunCommandsDirectory = getRunCommandsDirectory;
|
|
|
118
118
|
const RUNNER_TOOLS = { busybox: BUSYBOX, sandbox: exports.RUNNER_SANDBOX_PATH };
|
|
119
119
|
// How long a command waits for one of its run that still runs before it starts (s)
|
|
120
120
|
const RUN_COMMAND_WAIT = 3600;
|
|
121
|
+
// Time limits of the execs of a runner, which a cancel or a stalled connection must not outlast:
|
|
122
|
+
// an exec that does not connect in EXEC_CONNECT_MS is given up; a start may wait for the commands
|
|
123
|
+
// of its run as long as RUN_COMMAND_WAIT (and a little more); a follow session is opened again
|
|
124
|
+
// after FOLLOW_SESSION_MS, its command may run on (not a failure, see execInRunner)
|
|
125
|
+
const EXEC_CONNECT_MS = 30 * 1000;
|
|
126
|
+
const START_SESSION_MS = (RUN_COMMAND_WAIT + 600) * 1000;
|
|
127
|
+
const FOLLOW_SESSION_MS = 10 * 60 * 1000;
|
|
121
128
|
/**
|
|
122
129
|
* The scripts that start a command in the runner and follow, stop, find and remove the
|
|
123
130
|
* commands of a run, each run as `sh -c <script> hammerkit <args>`. `cleanUp` is what kills
|
|
@@ -766,10 +773,8 @@ root, cwd, command, scripts, where, task, abort) {
|
|
|
766
773
|
...(done ? { done: true } : {}),
|
|
767
774
|
...(cutSaid ? { cut: true } : {}),
|
|
768
775
|
});
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
current = socket;
|
|
772
|
-
};
|
|
776
|
+
// On abort every exec of this command ends with it: start and follow get the signal (runScript),
|
|
777
|
+
// so one that is still connecting, or stalled, is not waited for. The cancel is what kills it.
|
|
773
778
|
const cancel = () => runScript(instance, namespace, pod, scripts.cancel, [directory], { timeout: KILL_TIMEOUT_MS });
|
|
774
779
|
const kill = async () => {
|
|
775
780
|
const killed = await cancel();
|
|
@@ -777,7 +782,6 @@ root, cwd, command, scripts, where, task, abort) {
|
|
|
777
782
|
task.status.write('debug', `the command could not be stopped in the runner, it goes: ${killed.message ?? 'the kill did not end'}`);
|
|
778
783
|
await deletePod(instance, namespace, pod).catch(() => undefined);
|
|
779
784
|
}
|
|
780
|
-
current?.close();
|
|
781
785
|
};
|
|
782
786
|
let killing;
|
|
783
787
|
const aborted = () => {
|
|
@@ -792,7 +796,7 @@ root, cwd, command, scripts, where, task, abort) {
|
|
|
792
796
|
try {
|
|
793
797
|
// a start again after its connection broke takes the command it started, by its nonce
|
|
794
798
|
const nonce = (0, crypto_1.randomBytes)(12).toString('hex');
|
|
795
|
-
const start = (mode) => runScript(instance, namespace, pod, scripts.start, [directory, mode, resumed ? '' : nonce, root, cwd, ...command], {
|
|
799
|
+
const start = (mode) => runScript(instance, namespace, pod, scripts.start, [directory, mode, resumed ? '' : nonce, root, cwd, ...command], { signal: abort, timeout: START_SESSION_MS });
|
|
796
800
|
let started = await start(resumed ? 'adopt' : 'fresh');
|
|
797
801
|
for (let attempt = 0; started.exitCode === undefined && attempt < RECONNECTS && !abort.aborted; attempt++) {
|
|
798
802
|
await (0, sleep_1.sleep)(RUNNER_POLL_MS * 2 ** attempt);
|
|
@@ -876,7 +880,7 @@ root, cwd, command, scripts, where, task, abort) {
|
|
|
876
880
|
const before = offsets.stdout + offsets.stderr;
|
|
877
881
|
const stdout = lines('stdout');
|
|
878
882
|
const stderr = lines('stderr');
|
|
879
|
-
const followed = await runScript(instance, namespace, pod, scripts.follow, [directory, String(offsets.stdout), String(offsets.stderr)], { stdout: stdout.writable, stderr: stderr.writable,
|
|
883
|
+
const followed = await runScript(instance, namespace, pod, scripts.follow, [directory, String(offsets.stdout), String(offsets.stderr)], { stdout: stdout.writable, stderr: stderr.writable, signal: abort, timeout: FOLLOW_SESSION_MS });
|
|
880
884
|
if (abort.aborted) {
|
|
881
885
|
return await stopped();
|
|
882
886
|
}
|
|
@@ -885,8 +889,9 @@ root, cwd, command, scripts, where, task, abort) {
|
|
|
885
889
|
}
|
|
886
890
|
// 0: all of it was said. Else what followed it ended before: the end of the command killed
|
|
887
891
|
// it (137), its connection broke, it failed. It is followed again, a few times in a row
|
|
888
|
-
// without getting further at most: a command that keeps killing what follows it too
|
|
889
|
-
|
|
892
|
+
// without getting further at most: a command that keeps killing what follows it too. A
|
|
893
|
+
// session that ran its time limit is opened again, as a command that runs on is followed.
|
|
894
|
+
if (followed.exitCode !== 0 && !followed.timedOut) {
|
|
890
895
|
failures = offsets.stdout + offsets.stderr > before ? 1 : failures + 1;
|
|
891
896
|
if (failures > RECONNECTS) {
|
|
892
897
|
throw new Error(`the command in the runner of the sandbox could not be followed: ${followed.message ?? `exit code ${followed.exitCode}`}`);
|
|
@@ -940,6 +945,31 @@ async function reconnecting(abort, fn) {
|
|
|
940
945
|
}
|
|
941
946
|
return result;
|
|
942
947
|
}
|
|
948
|
+
// The value of `connecting` once it connects, or undefined when `ms` passed or `signal` aborted
|
|
949
|
+
// first. An exec that connects after that is closed, as nothing waits for it any more.
|
|
950
|
+
function connectWithin(connecting, ms, signal) {
|
|
951
|
+
return new Promise((resolve, reject) => {
|
|
952
|
+
let over = false;
|
|
953
|
+
const onAbort = () => end(() => resolve(undefined));
|
|
954
|
+
const end = (answer) => {
|
|
955
|
+
if (over) {
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
over = true;
|
|
959
|
+
clearTimeout(timer);
|
|
960
|
+
signal?.removeEventListener('abort', onAbort);
|
|
961
|
+
answer();
|
|
962
|
+
};
|
|
963
|
+
const timer = setTimeout(() => end(() => resolve(undefined)), ms);
|
|
964
|
+
if (signal?.aborted) {
|
|
965
|
+
onAbort();
|
|
966
|
+
}
|
|
967
|
+
else {
|
|
968
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
969
|
+
}
|
|
970
|
+
connecting.then((socket) => (over ? socket.close() : end(() => resolve(socket))), (e) => end(() => reject(e)));
|
|
971
|
+
});
|
|
972
|
+
}
|
|
943
973
|
// Runs a script of the runner's busybox with `args` as `$1`… in the runner
|
|
944
974
|
async function runScript(instance, namespace, pod, script, args, options = {}) {
|
|
945
975
|
let out = '';
|
|
@@ -952,34 +982,48 @@ async function runScript(instance, namespace, pod, script, args, options = {}) {
|
|
|
952
982
|
});
|
|
953
983
|
let status;
|
|
954
984
|
let broken;
|
|
985
|
+
let timedOut = false;
|
|
955
986
|
try {
|
|
956
|
-
const
|
|
987
|
+
const connecting = instance.exec.exec(namespace, pod, RUNNER_CONTAINER, [BUSYBOX, 'sh', '-c', script, 'hammerkit', ...args], stdout, options.stderr ?? null, null, false, (received) => {
|
|
957
988
|
status = received;
|
|
958
989
|
});
|
|
959
|
-
options.
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
990
|
+
const socket = await connectWithin(connecting, EXEC_CONNECT_MS, options.signal);
|
|
991
|
+
if (socket) {
|
|
992
|
+
// the end of the exec: its close, an error, the abort or the time limit, whichever comes first
|
|
993
|
+
// (a close that never comes is not waited for)
|
|
994
|
+
// resolves the wait for the exec: at its close, its error, the abort or the time limit
|
|
995
|
+
let resolveClosed = () => undefined;
|
|
996
|
+
const closed = new Promise((resolve) => {
|
|
997
|
+
resolveClosed = resolve;
|
|
998
|
+
socket.on('close', () => resolve());
|
|
999
|
+
socket.on('error', (e) => {
|
|
1000
|
+
broken = e;
|
|
1001
|
+
resolve();
|
|
1002
|
+
});
|
|
965
1003
|
});
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
1004
|
+
const onAbort = () => {
|
|
1005
|
+
socket.close();
|
|
1006
|
+
resolveClosed();
|
|
1007
|
+
};
|
|
1008
|
+
if (options.signal?.aborted) {
|
|
1009
|
+
onAbort();
|
|
1010
|
+
}
|
|
1011
|
+
else {
|
|
1012
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
1013
|
+
}
|
|
1014
|
+
const limit = options.timeout && !options.signal?.aborted
|
|
1015
|
+
? setTimeout(() => {
|
|
1016
|
+
timedOut = true;
|
|
1017
|
+
broken ?? (broken = new Error('it did not answer'));
|
|
1018
|
+
onAbort();
|
|
1019
|
+
}, options.timeout)
|
|
1020
|
+
: undefined;
|
|
1021
|
+
await closed;
|
|
1022
|
+
clearTimeout(limit);
|
|
1023
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
980
1024
|
}
|
|
981
1025
|
else {
|
|
982
|
-
|
|
1026
|
+
broken = new Error(options.signal?.aborted ? 'the run was cancelled' : 'it did not connect');
|
|
983
1027
|
}
|
|
984
1028
|
}
|
|
985
1029
|
catch (e) {
|
|
@@ -992,6 +1036,7 @@ async function runScript(instance, namespace, pod, script, args, options = {}) {
|
|
|
992
1036
|
...(exitCode === undefined
|
|
993
1037
|
? { message: status?.message ?? (broken ? (0, log_1.getErrorMessage)(broken) : 'the connection closed') }
|
|
994
1038
|
: {}),
|
|
1039
|
+
...(timedOut ? { timedOut } : {}),
|
|
995
1040
|
};
|
|
996
1041
|
}
|
|
997
1042
|
//# sourceMappingURL=sandbox-runner.js.map
|
package/dist/server/executor.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HostExecutor = exports.relayWorkerLine = exports.removeServiceRunVolumes = exports.serviceRunScopes = exports.volumeScope = exports.pushCacheNamespace = exports.cacheNamespace = exports.defaultWorker = void 0;
|
|
3
|
+
exports.HostExecutor = exports.killGroup = exports.relayWorkerLine = exports.removeServiceRunVolumes = exports.serviceRunScopes = exports.volumeScope = exports.pushCacheNamespace = exports.cacheNamespace = exports.defaultWorker = void 0;
|
|
4
4
|
const child_process_1 = require("child_process");
|
|
5
5
|
const crypto_1 = require("crypto");
|
|
6
6
|
const checks_1 = require("./checks");
|
|
@@ -12,6 +12,7 @@ const resolve_git_source_1 = require("../includes/resolve-git-source");
|
|
|
12
12
|
const git_checkout_1 = require("./git-checkout");
|
|
13
13
|
const runs_1 = require("./runs");
|
|
14
14
|
const run_plan_1 = require("./run-plan");
|
|
15
|
+
const run_worker_1 = require("./run-worker");
|
|
15
16
|
const work_network_1 = require("../planner/work-network");
|
|
16
17
|
const execute_docker_1 = require("../executer/execute-docker");
|
|
17
18
|
const run_source_environment_1 = require("./run-source-environment");
|
|
@@ -29,6 +30,11 @@ const STOP_GRACE_MS = 15000;
|
|
|
29
30
|
// A run the server leaves to the next one gets this long to stop its tasks on this machine: the
|
|
30
31
|
// rest of what CLOSE_TIMEOUT gives it is for its execution to end (its directory, its volumes)
|
|
31
32
|
const DETACH_GRACE_MS = runs_1.CLOSE_TIMEOUT - 5000;
|
|
33
|
+
// The worker's output is read until its stdio closes. After its exit that comes at once, unless a
|
|
34
|
+
// process of the run keeps the stdio open: it gets this long, then the wait ends with what the
|
|
35
|
+
// worker said. A worker that said its result and does not exit gets killed after RESULT_EXIT_MS.
|
|
36
|
+
const STDIO_CLOSE_MS = 2000;
|
|
37
|
+
const RESULT_EXIT_MS = 10000;
|
|
32
38
|
// what is kept of the error output of a process that died without saying why
|
|
33
39
|
const MAX_STDERR = 2 * 1024;
|
|
34
40
|
/**
|
|
@@ -160,6 +166,24 @@ function relayWorkerLine(line, runId, relay) {
|
|
|
160
166
|
}
|
|
161
167
|
}
|
|
162
168
|
exports.relayWorkerLine = relayWorkerLine;
|
|
169
|
+
// Signals the process group of a worker, which is started as one (`detached` on POSIX): what the
|
|
170
|
+
// worker started, and what it left running, is signalled with it. On Windows, the process alone.
|
|
171
|
+
function killGroup(child, signal) {
|
|
172
|
+
if (child.pid === undefined || process.platform === 'win32') {
|
|
173
|
+
child.kill(signal);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
process.kill(-child.pid, signal);
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
// the group is gone already, or the process is not its leader any more: the process itself goes
|
|
181
|
+
if (e.code !== 'ESRCH') {
|
|
182
|
+
child.kill(signal);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
exports.killGroup = killGroup;
|
|
163
187
|
class HostExecutor {
|
|
164
188
|
constructor(options) {
|
|
165
189
|
this.options = options;
|
|
@@ -422,6 +446,8 @@ class HostExecutor {
|
|
|
422
446
|
options: run.options,
|
|
423
447
|
...(push ? { cachePush: push } : {}),
|
|
424
448
|
cacheRegistries: this.options.cacheRegistries ?? [],
|
|
449
|
+
// how long a cancel's cleanup of the run's commands may take, from the server's environment
|
|
450
|
+
cleanupTimeoutMs: (0, run_worker_1.cleanupTimeoutMs)(process.env.HAMMERKIT_CLEANUP_TIMEOUT_MS),
|
|
425
451
|
accounts: run.accounts,
|
|
426
452
|
volumeScope: volumeScope(run, this.options.directory),
|
|
427
453
|
...((0, checks_1.isCheckRun)(run) ? { check: true } : {}),
|
|
@@ -533,6 +559,8 @@ class HostExecutor {
|
|
|
533
559
|
env,
|
|
534
560
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
535
561
|
windowsHide: true,
|
|
562
|
+
// its own process group on POSIX, so a kill reaches the children of the worker too
|
|
563
|
+
detached: process.platform !== 'win32',
|
|
536
564
|
});
|
|
537
565
|
child.stdin.on('error', () => undefined);
|
|
538
566
|
// the run first, so a cancel comes after it; kept open, it is how a cancel reaches the process
|
|
@@ -541,10 +569,13 @@ class HostExecutor {
|
|
|
541
569
|
let result;
|
|
542
570
|
let stderr = '';
|
|
543
571
|
let killTimer;
|
|
572
|
+
let resultTimer;
|
|
573
|
+
let stdioTimer;
|
|
574
|
+
let finished = false;
|
|
544
575
|
const stop = () => {
|
|
545
576
|
const detaching = (0, runs_1.isDetached)(context);
|
|
546
577
|
if (detaching && onCluster) {
|
|
547
|
-
child
|
|
578
|
+
killGroup(child, 'SIGKILL');
|
|
548
579
|
return;
|
|
549
580
|
}
|
|
550
581
|
child.stdin.write('cancel\n', (e) => {
|
|
@@ -553,7 +584,7 @@ class HostExecutor {
|
|
|
553
584
|
}
|
|
554
585
|
});
|
|
555
586
|
const grace = this.options.stopGraceMs ?? STOP_GRACE_MS;
|
|
556
|
-
killTimer = setTimeout(() => child
|
|
587
|
+
killTimer = setTimeout(() => killGroup(child, 'SIGKILL'), detaching ? Math.min(grace, DETACH_GRACE_MS) : grace);
|
|
557
588
|
killTimer.unref();
|
|
558
589
|
};
|
|
559
590
|
if (signal.aborted) {
|
|
@@ -562,17 +593,27 @@ class HostExecutor {
|
|
|
562
593
|
else {
|
|
563
594
|
signal.addEventListener('abort', stop, { once: true });
|
|
564
595
|
}
|
|
565
|
-
(0, readline_1.createInterface)({ input: child.stdout })
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
596
|
+
const lines = (0, readline_1.createInterface)({ input: child.stdout });
|
|
597
|
+
lines.on('line', (line) => {
|
|
598
|
+
// nothing is emitted for a run that has settled
|
|
599
|
+
if (finished) {
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
relayWorkerLine(line, runId, {
|
|
603
|
+
emit: (event) => context.emit(event),
|
|
604
|
+
up: (targets) => context.up?.(targets),
|
|
605
|
+
syncFailed: (id) => this.options.sandboxes?.syncFailed?.(id),
|
|
606
|
+
result: (message) => {
|
|
607
|
+
result = message;
|
|
608
|
+
// nothing left to tell the process
|
|
609
|
+
this.children.delete(runId);
|
|
610
|
+
child.stdin.end();
|
|
611
|
+
// the worker says nothing more: it should exit now, and its group is killed if it does not
|
|
612
|
+
resultTimer = setTimeout(() => killGroup(child, 'SIGKILL'), RESULT_EXIT_MS);
|
|
613
|
+
resultTimer.unref();
|
|
614
|
+
},
|
|
615
|
+
});
|
|
616
|
+
});
|
|
576
617
|
child.stderr.on('data', (chunk) => {
|
|
577
618
|
stderr = (stderr + chunk.toString('utf8')).slice(-MAX_STDERR);
|
|
578
619
|
});
|
|
@@ -581,9 +622,18 @@ class HostExecutor {
|
|
|
581
622
|
clearTimeout(killTimer);
|
|
582
623
|
reject(new Error(`the process of the run could not start: ${e.message}`));
|
|
583
624
|
});
|
|
584
|
-
|
|
625
|
+
// The wait ends on the worker's exit. Its stdio may stay open after it (a process of the run
|
|
626
|
+
// the cluster or the worker left holds it), so after the exit the wait ends at the close, or
|
|
627
|
+
// when STDIO_CLOSE_MS passed, with what the worker said.
|
|
628
|
+
const finish = (code, killed) => {
|
|
629
|
+
if (finished) {
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
finished = true;
|
|
585
633
|
signal.removeEventListener('abort', stop);
|
|
586
634
|
clearTimeout(killTimer);
|
|
635
|
+
clearTimeout(resultTimer);
|
|
636
|
+
clearTimeout(stdioTimer);
|
|
587
637
|
if (this.children.get(runId) === child) {
|
|
588
638
|
this.children.delete(runId);
|
|
589
639
|
}
|
|
@@ -600,7 +650,25 @@ class HostExecutor {
|
|
|
600
650
|
const reason = stderr.trim().split('\n').pop() ?? '';
|
|
601
651
|
reject(new Error(`the process of the run stopped (${killed ? `signal ${killed}` : `code ${code}`})${reason ? `: ${reason}` : ''}`));
|
|
602
652
|
}
|
|
653
|
+
};
|
|
654
|
+
// After the worker's exit the stdio may stay open, a process of the run holds it. Then the
|
|
655
|
+
// group is killed, the pipes are closed, and the wait ends with what the worker said.
|
|
656
|
+
const settleOpen = (code, killed) => {
|
|
657
|
+
if (finished) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
killGroup(child, 'SIGKILL');
|
|
661
|
+
lines.close();
|
|
662
|
+
child.stdout.destroy();
|
|
663
|
+
child.stderr.destroy();
|
|
664
|
+
child.stdin.destroy();
|
|
665
|
+
finish(code, killed);
|
|
666
|
+
};
|
|
667
|
+
child.on('exit', (code, killed) => {
|
|
668
|
+
stdioTimer = setTimeout(() => settleOpen(code, killed), STDIO_CLOSE_MS);
|
|
669
|
+
stdioTimer.unref();
|
|
603
670
|
});
|
|
671
|
+
child.on('close', (code, killed) => finish(code, killed));
|
|
604
672
|
});
|
|
605
673
|
}
|
|
606
674
|
// The published files go over the checkout; nothing may land outside of it or in
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.workerMain = exports.readInput = exports.ChangeQueue = exports.applyChanges = exports.parseChanges = exports.syncLine = exports.syncServices = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.relayStatus = exports.getRunHosted = exports.createRunSecrets = exports.removeCommandsOfRun = exports.getRunnerCommands = exports.getLogEvent = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = void 0;
|
|
3
|
+
exports.workerMain = exports.readInput = exports.ChangeQueue = exports.applyChanges = exports.parseChanges = exports.syncLine = exports.syncServices = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.relayStatus = exports.getRunHosted = exports.createRunSecrets = exports.removeCommandsOfRun = exports.getRunnerCommands = exports.getLogEvent = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = exports.cleanupTimeoutMs = void 0;
|
|
4
4
|
const readline_1 = require("readline");
|
|
5
5
|
const zod_1 = require("zod");
|
|
6
6
|
const cli_1 = require("../cli");
|
|
@@ -40,6 +40,18 @@ const MAX_LOG_TEXT = 8 * 1024;
|
|
|
40
40
|
* engine reads of its process (the home directory, the docker host, the cache directory)
|
|
41
41
|
* is the run's and not the server's.
|
|
42
42
|
*/
|
|
43
|
+
// The cleanup of the commands of a run after a cancel, bounded (ms): HAMMERKIT_CLEANUP_TIMEOUT_MS
|
|
44
|
+
// sets it when it is a plain integer in this range, else the default
|
|
45
|
+
const REMOVE_COMMANDS_MS = 60000;
|
|
46
|
+
const CLEANUP_LIMITS_MS = { min: 1000, max: 600000 };
|
|
47
|
+
function cleanupTimeoutMs(value) {
|
|
48
|
+
if (value === undefined || !/^\d+$/.test(value.trim())) {
|
|
49
|
+
return REMOVE_COMMANDS_MS;
|
|
50
|
+
}
|
|
51
|
+
const ms = Number(value.trim());
|
|
52
|
+
return ms >= CLEANUP_LIMITS_MS.min && ms <= CLEANUP_LIMITS_MS.max ? ms : REMOVE_COMMANDS_MS;
|
|
53
|
+
}
|
|
54
|
+
exports.cleanupTimeoutMs = cleanupTimeoutMs;
|
|
43
55
|
exports.workerInputSchema = zod_1.z
|
|
44
56
|
.object({
|
|
45
57
|
// the build file to start from, in the checkout
|
|
@@ -63,6 +75,8 @@ exports.workerInputSchema = zod_1.z
|
|
|
63
75
|
// on the server's docker host, the runtime a container that adds capabilities runs in
|
|
64
76
|
// (`backend.capabilities.runtime`)
|
|
65
77
|
capabilityRuntime: zod_1.z.string().min(1).optional(),
|
|
78
|
+
// how long the cleanup of the commands of a run after a cancel may take (ms), see cleanupTimeoutMs
|
|
79
|
+
cleanupTimeoutMs: zod_1.z.number().int().min(CLEANUP_LIMITS_MS.min).max(CLEANUP_LIMITS_MS.max).optional(),
|
|
66
80
|
// the server's credential to write a registry cache with, for a trusted run only (`cache.push`,
|
|
67
81
|
// see cache-push.ts); left out for any other run, which may only read the caches
|
|
68
82
|
cachePush: zod_1.z
|
|
@@ -227,10 +241,28 @@ async function removeCommandsOfRun(input, commands, emit) {
|
|
|
227
241
|
if (!kubernetes || !workspace?.runner || commands.executions.size === 0) {
|
|
228
242
|
return;
|
|
229
243
|
}
|
|
244
|
+
// The cluster may not answer: the cleanup ends after its limit, so the result is not held up by a
|
|
245
|
+
// removal that never settles. The command directories of the run may then stay in the runner; the
|
|
246
|
+
// cancel has killed their processes already.
|
|
247
|
+
const limit = input.cleanupTimeoutMs ?? REMOVE_COMMANDS_MS;
|
|
248
|
+
let timer;
|
|
249
|
+
const expired = new Promise((resolve) => {
|
|
250
|
+
timer = setTimeout(() => resolve('expired'), limit);
|
|
251
|
+
});
|
|
230
252
|
try {
|
|
231
|
-
|
|
253
|
+
// inside the try: a synchronous throw of the removal is logged like any other failure
|
|
254
|
+
const removal = Promise.resolve().then(() => (0, sandbox_runner_1.removeRunCommands)((0, kubernetes_instance_1.createKubernetesInstances)(kubernetes), kubernetes.namespace, workspace.label, kubernetes.server.run, {
|
|
232
255
|
clean: true,
|
|
233
|
-
});
|
|
256
|
+
}));
|
|
257
|
+
const outcome = await Promise.race([removal.then(() => 'removed'), expired]);
|
|
258
|
+
if (outcome === 'expired') {
|
|
259
|
+
emit({
|
|
260
|
+
type: 'log',
|
|
261
|
+
task: 'hammerkit',
|
|
262
|
+
stream: 'stderr',
|
|
263
|
+
text: `what the commands of the run left in the sandbox could not be removed: the cluster did not answer in ${limit} ms`,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
234
266
|
}
|
|
235
267
|
catch (e) {
|
|
236
268
|
emit({
|
|
@@ -240,6 +272,9 @@ async function removeCommandsOfRun(input, commands, emit) {
|
|
|
240
272
|
text: `what the commands of the run left in the sandbox could not be removed: ${(0, log_1.getErrorMessage)(e)}`,
|
|
241
273
|
});
|
|
242
274
|
}
|
|
275
|
+
finally {
|
|
276
|
+
clearTimeout(timer);
|
|
277
|
+
}
|
|
243
278
|
}
|
|
244
279
|
exports.removeCommandsOfRun = removeCommandsOfRun;
|
|
245
280
|
// The secrets of a run, masked in what it logs: the password of its cache push, as a secret of a
|
package/package.json
CHANGED