This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NodeChildProcessService.ts
483 lines (432 loc) · 19.4 KB
/
NodeChildProcessService.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) 2023. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { ChildProcess, SerializationType, spawn, SpawnOptions, StdioOptions } from "child_process";
import { filter } from "../core/functions/filter";
import { map } from "../core/functions/map";
import { forEach } from "../core/functions/forEach";
import { LogService } from "../core/LogService";
import { LogLevel } from "../core/types/LogLevel";
import { isNumber, parseInteger } from "../core/types/Number";
import { ChildProcessService, CommandOptions, CommandResponse } from "../core/ChildProcessService";
import { ChildProcessError, isChildProcessError } from "../core/types/ChildProcessError";
const LOG = LogService.createLogger('NodeChildProcessService');
interface StoredChild {
readonly name : string,
readonly args : readonly string[],
child ?: ChildProcess;
stdout : Buffer[];
stderr : Buffer[];
pid: number | undefined;
abort: boolean;
running: boolean;
streamsOpen: boolean;
initializing: boolean;
exitCode ?: number | null;
exitSignal ?: number | string | null;
killSignal ?: number | string;
promise ?: Promise<CommandResponse>;
}
/**
* Implementation to run child processes in the system using NodeJS APIs.
*
* @see {@link ChildProcessService}
*/
export class NodeChildProcessService implements ChildProcessService {
/**
* Set service log level
* @param level
*/
public static setLogLevel (level: LogLevel) : void {
LOG.setLogLevel(level);
}
/**
* Array of any started child processes
*/
private _children : StoredChild[];
private _destroyed : boolean;
/**
* Construct the service
*/
protected constructor () {
this._destroyed = false;
this._children = [];
}
public static create () : ChildProcessService {
return new NodeChildProcessService();
}
public isDestroyed () : boolean {
return this._destroyed;
}
/**
* @see {@link ChildProcessService.destroy}
* @inheritdoc
*/
public destroy () : void {
if (!this._destroyed) {
this._destroyed = true;
LOG.debug(`destroying ${this._children?.length} children: ${map(this._children, item => item?.child?.pid).join(', ')}`);
this._shutdownChildProcesses().catch((err: any) => {
LOG.error(`Error happened when shutting down the service: `, err);
});
} else {
LOG.warn(`The service was already destroyed.`);
}
}
/**
* @see {@link ChildProcessService.countRunningChildren}
* @inheritdoc
*/
public async countChildProcesses () : Promise<number> {
LOG.debug(`countChildProcesses: ${this._children?.length}: ${map(this._children, item => item?.child?.pid).join(', ')}`);
return this._children.length;
}
/**
* @see {@link ChildProcessService.waitUntilDown}
* @inheritdoc
*/
public async waitAllChildProcessesStopped () : Promise<void> {
if (this._children?.length) {
LOG.debug(`start: waitAllChildProcessesStopped: ${map(this._children, item => item?.child?.pid).join(', ')}`);
// Collect children's promises
const children = this._children;
const promises : Promise<void>[] = map(
children,
async (item : StoredChild) : Promise<void> => {
try {
if (item.promise) {
await item.promise;
}
} catch (err) {
if (isChildProcessError(err) && err.signal === (item.killSignal ?? 'SIGTERM')) {
const { name, pid, running, abort, initializing, streamsOpen, exitSignal, exitCode } = item;
LOG.debug(`Child #${pid} (name=${name}, running=${running}, initializing=${initializing}, abort=${abort}, streamsOpen=${streamsOpen}, exitCode=${exitCode}, exitSignal=${exitSignal}) successfully shutdown with signal ${err.signal}`);
} else {
const { name, pid, running, abort, initializing, streamsOpen, exitSignal, exitCode } = item;
LOG.debug(`Child #${pid} (name=${name}, running=${running}, initializing=${initializing}, abort=${abort}, streamsOpen=${streamsOpen}, exitCode=${exitCode}, exitSignal=${exitSignal}) failed to shutdown: `, err);
}
}
}
);
LOG.debug(`Waiting for ${promises.length} children to shutdown: ${map(this._children, item => item?.child?.pid).join(', ')}`);
// Wait for the children to shut down
await Promise.allSettled(promises);
LOG.debug(`end: waitAllChildProcessesStopped: ${map(this._children, item => item?.child?.pid).join(', ')}`);
const count = this._children?.length ?? 0;
if (count) {
LOG.warn(`Warning! ${count} children detected at the end of waitAllChildProcessesStopped(): ${map(this._children, item => item?.child?.pid).join(', ')}`);
}
} else {
LOG.debug(`waitAllChildProcessesStopped: No children detected.`);
}
}
/**
* @see {@link ChildProcessService.shutdown}
* @inheritdoc
*/
/** @inheritdoc */
public async shutdownChildProcesses () : Promise<void> {
if (this._destroyed) throw new TypeError(`shutdownChildProcesses: Service already destroyed`);
await this._shutdownChildProcesses();
}
private async _shutdownChildProcesses () : Promise<void> {
LOG.debug(`start: shutdownChildProcesses: `, this._children);
this._sendShutdownToChildProcesses();
await this.waitAllChildProcessesStopped();
LOG.debug(`end: shutdownChildProcesses: `, this._children);
}
/**
* @see {@link ChildProcessService.executeCommand}
* @inheritdoc
*/
public async executeCommand (
name : string,
args ?: readonly string[],
opts ?: CommandOptions
) : Promise<CommandResponse> {
if (this._destroyed) throw new TypeError(`The service has been destroyed`);
LOG.debug(`start: executeCommand: `, name, args, opts, `Running: ${map(this._children, item => item?.child?.pid).join(', ')}`);
const p = await this._execFile(name, args, opts);
LOG.debug(`end: executeCommand: `, name, args, opts, `Running: ${map(this._children, item => item?.child?.pid).join(', ')}`);
return p;
}
/**
*
* @param name
* @param args
* @param opts
* @private
*/
private async _execFile (
name : string,
args ?: readonly string[],
opts ?: CommandOptions
) : Promise<CommandResponse> {
if (this._destroyed) throw new TypeError(`_execFile: Service already destroyed`);
LOG.debug(`_execFile: `, name, args, opts, `Running: ${map(this._children, item => item?.child?.pid).join(', ')}`);
if (!args) args = [];
if (!opts) opts = {};
const {
cwd,
env,
argv0,
serialization,
timeout,
uid,
gid,
killSignal,
stdio,
detached
} = opts;
const storedItem : StoredChild = {
name,
args,
pid: undefined,
abort: false,
initializing: true,
streamsOpen: false,
running: false,
child: undefined,
stdout: [],
stderr: [],
killSignal
};
this._children.push(storedItem);
const nodeOpts : SpawnOptions = {
...(cwd !== undefined ? staticSpawnOptionsTypeGuard({cwd}) : {}),
...(env !== undefined ? staticSpawnOptionsTypeGuard({env}) : {}),
...(argv0 !== undefined ? staticSpawnOptionsTypeGuard({argv0}) : {}),
...(serialization !== undefined ? staticSpawnOptionsTypeGuard({serialization: serialization as SerializationType}) : {}),
...(detached !== undefined ? staticSpawnOptionsTypeGuard({detached}) : {}),
...(timeout !== undefined ? staticSpawnOptionsTypeGuard({timeout}) : {}),
...(uid !== undefined ? staticSpawnOptionsTypeGuard({uid}) : {}),
...(gid !== undefined ? staticSpawnOptionsTypeGuard({gid}) : {}),
...(stdio !== undefined ? staticSpawnOptionsTypeGuard({stdio: stdio as StdioOptions}) : {}),
...(killSignal !== undefined ? staticSpawnOptionsTypeGuard({killSignal: killSignal as NodeJS.Signals|number}) : {}),
};
return storedItem.promise = new Promise<CommandResponse>(
(resolve, reject) => {
try {
if ( this._destroyed ) {
reject(new TypeError(`Service destroyed`));
storedItem.promise = undefined;
return;
}
if ( storedItem?.child ) {
reject(new TypeError(`Child is already created`));
storedItem.promise = undefined;
return;
}
storedItem.running = false;
storedItem.streamsOpen = false;
const child: ChildProcess = spawn(name, args ?? [], nodeOpts);
storedItem.child = child;
storedItem.pid = child?.pid;
child.on('spawn', () => {
storedItem.initializing = false;
storedItem.running = true;
storedItem.streamsOpen = true;
if (storedItem.pid === undefined) {
storedItem.pid = child?.pid;
}
if (storedItem.abort) {
storedItem.abort = false;
this._stopChild(storedItem);
}
});
child.on('error', (error: any) => {
if ( storedItem.running ) {
LOG.warn(`Unexpected error: `, error);
if (error) {
reject(error);
} else {
reject(new TypeError(`Event "error" without error information detected`));
}
} else {
LOG.warn(`The child process could not be spawned: `, error);
}
});
if ( child.stdout ) {
child.stdout.on('data', (chunk: Buffer) => {
if ( this._destroyed ) {
LOG.debug(`Event 'data': Service already destroyed; stdout data ignored.`);
return;
}
const { child, running, streamsOpen, initializing } = storedItem;
const pid = child?.pid;
LOG.debug(`stdout data on: child #${pid}, running=${running}, streamsOpen=${streamsOpen}, initializing=${initializing}`);
storedItem.stdout.push(chunk);
});
}
if ( child.stderr ) {
child.stderr.on('data', (chunk: Buffer) => {
if ( this._destroyed ) {
LOG.debug(`Event 'data' on stderr: Service already destroyed; stderr data ignored.`);
return;
}
const { child, running, streamsOpen, initializing } = storedItem;
const pid = child?.pid;
LOG.debug(`stderr data on: child #${pid}, running=${running}, streamsOpen=${streamsOpen}, initializing=${initializing}`);
storedItem.stderr.push(chunk);
});
}
child.on('exit', (code, signal) => {
storedItem.running = false;
storedItem.exitCode = code;
storedItem.exitSignal = signal;
});
child.on('close', () => {
storedItem.running = false;
storedItem.streamsOpen = false;
if ( this._destroyed ) {
const { child, running, streamsOpen, initializing } = storedItem;
const pid = child?.pid;
LOG.debug(`close on destroyed service item: child #${pid}, running=${running}, streamsOpen=${streamsOpen}, initializing=${initializing}`);
} else {
const { child, running, streamsOpen, initializing } = storedItem;
const pid = child?.pid;
LOG.debug(`close on item: child #${pid}, running=${running}, streamsOpen=${streamsOpen}, initializing=${initializing}`);
}
this._onStoredChildClose(
name,
args ?? [],
storedItem,
parseInteger(storedItem.exitCode) ?? parseInteger(child.exitCode) ?? undefined,
storedItem.exitSignal ?? child.signalCode ?? undefined
).then((value) => {
resolve(value);
storedItem.promise = undefined;
}, (err: any) => {
reject(err);
storedItem.promise = undefined;
});
});
} catch (err) {
LOG.warn(`Exception handled from command "${name}${args?.length ? ' ' : ''}${(args ?? []).join(' ')}": `, err);
reject(new ChildProcessError(name, args ?? [], -4));
storedItem.promise = undefined;
}
}
);
}
public sendShutdownToChildProcesses () : void {
if (this._destroyed) throw new TypeError(`sendShutdownToChildProcesses: Service already destroyed`);
return this._sendShutdownToChildProcesses();
}
/**
* Send kill signals to all children
*
* @private
*/
private _sendShutdownToChildProcesses () : void {
const children = this._children;
LOG.debug(`Sending shutdown to ${children.length} children: ${map(this._children, item => item?.child?.pid).join(', ')}`);
forEach(
children,
(item: StoredChild) : void => {
try {
const { child, initializing, abort } = item;
LOG.debug(`#${child?.pid}: initializing=${initializing}, abort=${abort}`)
if ( initializing ) {
if (!abort) {
LOG.debug(`The child #${child?.pid} will be aborted later`);
item.abort = true;
} else {
LOG.debug(`The child #${child?.pid} was already aborting later`);
}
} else {
this._stopChild(item);
}
} catch (err) {
LOG.warn(`Warning! Could not send shutdown signal to child: `, err);
}
}
);
}
private _stopChild (
item: StoredChild
) {
const { child, killSignal, running } = item;
LOG.debug(`#${child?.pid}: killSignal=${killSignal}, running=${running}`)
if ( child && running ) {
LOG.debug(`Sending ${killSignal??'default signal'} to child #${child?.pid}`);
let status : boolean;
if (killSignal) {
status = child.kill(killSignal as number|NodeJS.Signals);
} else {
status = child.kill();
}
if (!status) {
LOG.warn(`Warning! Could not signal child process ${child?.pid} to stop`);
}
} else {
if (!child) LOG.warn(`Warning! The child was not yet created`);
else if (!running) LOG.warn(`Warning! The child #${child?.pid} was not running`);
}
}
private async _onStoredChildClose (
name: string,
args: readonly string[],
storedItem: StoredChild,
exitCode ?: number,
signalCode ?: string | number
) : Promise<CommandResponse> {
const { child, running, streamsOpen, initializing } = storedItem;
const pid = child?.pid;
LOG.debug(`_onStoredChildClose on child #${pid}, running=${running}, streamsOpen=${streamsOpen}, initializing=${initializing}`);
const stderr : string = Buffer.concat(storedItem.stderr).toString('utf8');
const stdout : string = Buffer.concat(storedItem.stdout).toString('utf8');
try {
return await this._onChildProcessClose(name, args, exitCode, signalCode, stdout, stderr);
} catch (err) {
if (isChildProcessError(err)) {
throw err;
}
LOG.warn(`Unexpected exception handled: "${name} ${(args??[]).join(' ')}": `, err);
throw new ChildProcessError(name, args ?? [], -3, undefined, stderr);
} finally {
try {
// Remove child from running children if found
if (storedItem) {
this._children = filter(this._children, (item) => item !== storedItem);
storedItem.child = undefined;
storedItem.killSignal = undefined;
}
} catch (err) {
LOG.warn(`Error when removing the child from internal array: `, err);
}
}
}
private async _onChildProcessClose (
name : string,
args : readonly string[],
exitCode ?: number,
signalCode ?: string | number,
stdout ?: string,
stderr ?: string
) : Promise<CommandResponse> {
if ( signalCode !== undefined ) {
LOG.debug(`Command failed: "${name}${args?.length?' ':''}${(args??[]).join(' ')}": Signal received: ${signalCode}: `, stdout, stderr);
throw new ChildProcessError(name, args ?? [], -2, signalCode as string|number, stderr);
}
if ( exitCode !== undefined && exitCode !== 0 ) {
const status = isNumber(exitCode) ? exitCode : -1;
if (status >= 0) {
LOG.debug(`Command failed: "${name}${args?.length?' ':''}${(args??[]).join(' ')}": Exit code: ${status}: `, stdout, stderr);
throw new ChildProcessError(name, args ?? [], status, undefined, stderr);
}
LOG.debug(`Command failed: "${name}${args?.length?' ':''}${(args??[]).join(' ')}": `, stdout, stderr);
throw new ChildProcessError(name, args ?? [], status, undefined, stderr);
}
LOG.debug(`Command succeed: "${name}${args?.length?' ':''}${(args??[]).join(' ')}": `, stdout, stderr);
return {
name,
args,
output: stdout ?? '',
...(stderr ? {errors: stderr} : {})
};
}
}
function staticSpawnOptionsTypeGuard (
value: SpawnOptions
) : SpawnOptions {
return value;
}