@shumkov/orchestra 0.6.0 → 0.7.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.
@@ -0,0 +1,94 @@
1
+ import { spawn } from 'node:child_process';
2
+
3
+ const GRACE_FLAG = '--group-term-grace-ms=';
4
+ const argv = process.argv.slice(2);
5
+ const graceMs = argv[0]?.startsWith(GRACE_FLAG)
6
+ ? Number(argv.shift().slice(GRACE_FLAG.length))
7
+ : Number.NaN;
8
+ const [binary, ...args] = argv;
9
+ if (!binary || !Number.isSafeInteger(graceMs) || graceMs < 0) process.exit(64);
10
+
11
+ const child = spawn(binary, args, {
12
+ cwd: process.cwd(),
13
+ env: process.env,
14
+ stdio: ['pipe', 'pipe', 'pipe'],
15
+ });
16
+
17
+ let childGone = false;
18
+ let terminating = false;
19
+ let graceTimer = null;
20
+
21
+ // Signalling our own group is safe precisely because we are its live leader:
22
+ // unlike the parent, we cannot have exited between deciding to signal and
23
+ // signalling, so the group id cannot have been recycled underneath the call.
24
+ function signalOwnGroup(signal) {
25
+ try { process.kill(-process.pid, signal); } catch {}
26
+ }
27
+
28
+ // SIGKILL reaches this process too, so it is the last thing we do. The exit is
29
+ // only reached if the signal could not be sent, and the parent's read-only
30
+ // proof is what catches that.
31
+ function terminateGroup() {
32
+ if (graceTimer !== null) {
33
+ clearTimeout(graceTimer);
34
+ graceTimer = null;
35
+ }
36
+ signalOwnGroup('SIGKILL');
37
+ process.exit(0);
38
+ }
39
+
40
+ function shutdown() {
41
+ if (terminating) return;
42
+ terminating = true;
43
+ if (childGone) {
44
+ terminateGroup();
45
+ return;
46
+ }
47
+ signalOwnGroup('SIGTERM');
48
+ graceTimer = setTimeout(terminateGroup, graceMs);
49
+ }
50
+
51
+ // Piped, so the app-server's own read rate applies backpressure to ours. The
52
+ // pipe must not end the child's stdin for us, because we end it deliberately
53
+ // on EOF below.
54
+ process.stdin.pipe(child.stdin, { end: false });
55
+
56
+ // The parent ending stdin — or dying — is the shutdown request. The parent
57
+ // never signals the group, so this is the only path that ends it. Ending the
58
+ // child's stdin hands the app-server its own EOF alongside the group signal.
59
+ function onParentStdinEnd() {
60
+ try { child.stdin.end(); } catch {}
61
+ shutdown();
62
+ }
63
+
64
+ process.stdin.once('end', onParentStdinEnd);
65
+ process.stdin.once('close', onParentStdinEnd);
66
+
67
+ child.stdout.pipe(process.stdout, { end: false });
68
+ child.stderr.pipe(process.stderr, { end: false });
69
+ child.stdin.on('error', () => {});
70
+ child.stdout.once('error', () => {});
71
+ child.stderr.once('error', () => {});
72
+ // 'error' can arrive post-spawn — an abort, a failed kill, a failed message
73
+ // send — while the child is still running, so it proves nothing. 'close' is the
74
+ // only report of a gone child, and a failed spawn closes too.
75
+ child.once('error', () => {});
76
+
77
+ child.once('close', () => {
78
+ childGone = true;
79
+ // The pipe does not detach itself from a dead child's stdin, and while it
80
+ // stays attached our stdin never surfaces EOF — which would lose the only
81
+ // shutdown signal we have whenever the app-server exits before the parent
82
+ // closes, stranding us as a permanent group leader. Detaching by hand and
83
+ // resuming restores that; with the child gone the drained bytes have nowhere
84
+ // to go and are discarded.
85
+ process.stdin.unpipe(child.stdin);
86
+ process.stdin.resume();
87
+ process.stdout.end();
88
+ if (terminating) terminateGroup();
89
+ });
90
+
91
+ // We must outlive the group we lead, so we ignore the TERM we send it.
92
+ process.on('SIGTERM', () => {});
93
+
94
+ setInterval(() => {}, 60_000);