@trama-dev/runtime 0.3.0 → 0.3.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/dist/lifetime.js DELETED
@@ -1,85 +0,0 @@
1
- // What this process must undo if it ends while a run is still in flight:
2
- // stop the processes it started, and delete what it left in the temp directory.
3
- //
4
- // A program, and every shell command it runs, leads its own process group — so
5
- // that a timeout can take down everything it started. The cost is that none of
6
- // them die with the runner: without this, Ctrl-C on `trama run` would leave the
7
- // program running, and a program that coordinates others (a session) would
8
- // leave them running when it ended.
9
- import { rmSync } from "fs";
10
- import { stopGroupsOnExit } from "./child-process.js";
11
- const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
12
- const pendingGroups = new Set();
13
- const pendingDirs = new Set();
14
- let installed = false;
15
- function runPending() {
16
- const groups = [...pendingGroups];
17
- const dirs = [...pendingDirs];
18
- pendingGroups.clear();
19
- pendingDirs.clear();
20
- // The groups are asked to stop and given a moment; whatever sees that through
21
- // also removes the directories again once they are gone, in case they were
22
- // still writing to them.
23
- stopGroupsOnExit(groups, dirs);
24
- for (const dir of dirs)
25
- removeTempDir(dir);
26
- }
27
- function onSignal(signal) {
28
- runPending();
29
- // Having a handler at all cancelled the signal's default action. Step aside
30
- // and send it again, so that what happens next is what would have happened
31
- // had this handler never been installed: the process dies of it, or whoever
32
- // else handles the signal decides. (Common libraries only act on a signal
33
- // when they are its sole listener, so simply staying out of the way is not
34
- // enough — the signal has to arrive once more without us.)
35
- uninstall();
36
- process.kill(process.pid, signal);
37
- }
38
- function install() {
39
- if (installed)
40
- return;
41
- installed = true;
42
- process.on("exit", runPending);
43
- for (const signal of SIGNALS)
44
- process.on(signal, onSignal);
45
- }
46
- /** Leave the host process's signal handling untouched whenever nothing is in flight. */
47
- function uninstall() {
48
- if (!installed)
49
- return;
50
- installed = false;
51
- process.removeListener("exit", runPending);
52
- for (const signal of SIGNALS)
53
- process.removeListener(signal, onSignal);
54
- }
55
- /** Keep `item` pending until the returned function is called. */
56
- function track(pending, item) {
57
- pending.add(item);
58
- install();
59
- return () => {
60
- pending.delete(item);
61
- if (pendingGroups.size === 0 && pendingDirs.size === 0)
62
- uninstall();
63
- };
64
- }
65
- /**
66
- * Register a process group this process started: if this process ends before
67
- * the returned function is called, the group is stopped (see stopGroupsOnExit).
68
- */
69
- export function stopWithThisProcess(pgid) {
70
- return track(pendingGroups, pgid);
71
- }
72
- /**
73
- * Register a temp directory this process created: if this process ends before
74
- * the returned function is called, the directory is removed.
75
- */
76
- export function removeWithThisProcess(dir) {
77
- return track(pendingDirs, dir);
78
- }
79
- /** Remove a temp directory. Best-effort: failing to clean up must never change a run's outcome. */
80
- export function removeTempDir(dir) {
81
- try {
82
- rmSync(dir, { recursive: true, force: true });
83
- }
84
- catch { /* left for the OS to reap */ }
85
- }