This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
/
index.ts
81 lines (70 loc) · 2.35 KB
/
index.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
import {createResourceFromCallback} from "./factories";
import Resource from "./Resource";
import {ResourcesContainer} from "./types";
import workerThreads = require("worker_threads");
export type {Resource};
export * from "./factories";
export function extractResource(rawResource: ResourcesContainer): Resource {
if (rawResource instanceof Resource) {
return rawResource;
} else {
const prop = rawResource.resources;
if (prop instanceof Resource) {
return prop;
} else {
throw new Error("Resource not provided");
}
}
}
export const processResourceRoot = new Resource({
name: "Process",
getDetails: () => ({
type: "process",
self: true,
pid: process.pid,
tid: workerThreads.threadId,
command: process.execPath,
args: process.argv.slice(1),
}),
optional: true,
async finalize() {
process.exit(process.exitCode);
},
});
const processResourceRelease = processResourceRoot.release.bind(
processResourceRoot,
);
// Only ran when the event loop has ran out of work, we cannot "catch" exits si we need use safeProcessExit calls
process.on("beforeExit", processResourceRelease);
process.on("SIGINT", processResourceRelease);
process.on("SIGHUP", processResourceRelease);
process.on("SIGTERM", processResourceRelease);
// We explicitly do not attach this to processResourceRelease as those should always be attached during teardown
export const processResourceListeners = createResourceFromCallback(
"ProcessListeners",
() => {
process.removeListener("beforeExit", processResourceRelease);
process.removeListener("SIGINT", processResourceRelease);
process.removeListener("SIGHUP", processResourceRelease);
process.removeListener("SIGTERM", processResourceRelease);
},
{
optional: true,
},
);
export async function safeProcessExit(code: number): Promise<void> {
process.exitCode = code;
// Suppress all uncaught exceptions. We'll begin tearing down and don't want Node's error handlers to be used
process.setUncaughtExceptionCaptureCallback(null);
process.setUncaughtExceptionCaptureCallback(() => {});
process.removeAllListeners("unhandledRejection");
process.on("unhandledRejection", () => {});
try {
await processResourceRoot.releaseBlock();
} catch (err) {
console.error("safeProcessExit: Release failure");
console.error(err.stack);
}
console.error("safeProcessExit: Process not exited by resource release");
process.exit(2);
}