Repository navigation
Expand file tree
/
Copy pathprocess.ts
More file actions
129 lines (128 loc) · 3.98 KB
/
Copy pathprocess.ts
File metadata and controls
129 lines (128 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { spawn, ChildProcess } from 'child_process';
import { StringDecoder } from 'string_decoder';
import { BackendEvent } from '../backend';
/** Process ownership is independent of the frontend and its IPC transport. */
export class RuntimeProcess {
private child?: ChildProcess;
private pid?: number;
private exited = false;
private closed = false;
private monitor?: NodeJS.Timeout;
constructor(
readonly adopted: boolean,
private emit: (event: BackendEvent) => void,
) {}
launch(
command: string,
args: string[],
directory: string,
env: NodeJS.ProcessEnv,
output: (text: string, channel: string) => void,
): ChildProcess {
this.child = spawn(command, args, {
cwd: directory,
windowsHide: true,
env,
stdio: ['ignore', 'pipe', 'pipe'],
});
this.pid = this.child.pid;
this.child.on('error', (error) => {
this.emit({ type: 'error', message: error.message });
this.exit();
});
this.child.on('exit', (code, signal) => this.exit(code, signal));
for (const channel of ['stdout', 'stderr'] as const) {
const decoder = new StringDecoder('utf8');
this.child[channel]!.on('data', (chunk: Buffer) => {
if (!this.closed) {
output(decoder.write(chunk), channel);
}
});
}
return this.child;
}
setPid(pid: number): void {
this.pid = pid;
if (this.adopted && !this.monitor) {
this.monitor = setInterval(() => {
if (!this.alive()) {
this.exit();
}
}, 500);
this.monitor.unref();
}
}
alive(): boolean {
if (this.exited || !this.pid) {
return false;
}
if (this.child) {
return this.child.exitCode === null && this.child.signalCode === null;
}
try {
process.kill(this.pid, 0);
return true;
} catch (error) {
return (error as NodeJS.ErrnoException).code !== 'ESRCH';
}
}
private exit(code?: number | null, signal?: string | null): void {
if (this.exited) {
return;
}
this.exited = true;
clearInterval(this.monitor);
if (!this.closed) {
this.emit({ type: 'exit', code, signal });
}
}
/** Records an exit observed without a process event, such as an adopted PID that vanished. */
markExited(): void {
this.exit();
}
/**
* Sends a signal to the R process. The interrupt and stop paths use arf IPC
* instead: headless arf ends the whole session on SIGINT.
*/
send(signal: NodeJS.Signals): void {
if (!this.alive()) {
this.exit();
return;
}
try {
// Node's Windows signals forcibly terminate; never use them for interrupt.
process.kill(this.pid!, process.platform === 'win32' ? 'SIGKILL' : signal);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ESRCH') {
this.exit();
} else {
throw error;
}
}
}
/** Resolves true once the process has exited, or false after `timeout` ms. */
async waitForExit(timeout: number): Promise<boolean> {
const start = Date.now();
while (this.alive()) {
if (this.closed) {
return true;
}
if (Date.now() - start >= timeout) {
return false;
}
await new Promise((resolve) => setTimeout(resolve, 25));
}
this.exit();
return true;
}
dispose(): void {
if (this.closed) {
return;
}
if (!this.adopted) {
this.send('SIGKILL');
}
this.closed = true;
clearInterval(this.monitor);
}
}