-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathentryPoint.js
118 lines (112 loc) · 3.07 KB
/
entryPoint.js
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
const os = require("os");
const Sentry = require("@sentry/node");
require("tls").DEFAULT_ECDH_CURVE = "auto";
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: function (integrations) {
return integrations.filter(function (integration) {
return integration.name !== "Http";
});
},
});
function flushStdout(timeout = 60000) {
return new Promise((resolve) => {
if (process.stdout.write("")) {
process.nextTick(() => resolve());
return;
}
let resolved = false;
let timeoutToken;
const cb = function () {
if (resolved) {
return;
}
resolved = true;
if (timeoutToken) {
clearTimeout(timeoutToken);
timeoutToken = null;
}
process.nextTick(() => resolve());
};
process.stdout.once("drain", cb);
timeoutToken = setTimeout(function () {
Sentry.captureMessage("Stdout doesn't drain in time");
Sentry.flush(2000).finally(cb);
}, timeout);
});
}
function rejectWithSendError(e, extra = {}) {
return new Promise((resolve, reject) => {
const report = process.report?.getReport?.(e?.stack ? e : undefined) || {};
for (const key of Object.keys(report)) {
if (!report.hasOwnProperty(key)) {
continue;
}
let value = report[key];
if (typeof value !== "object" || Array.isArray(value)) {
value = { value };
}
Sentry.setContext("report_" + key, value);
}
if (e.stack) {
Sentry.captureException(e);
} else {
Sentry.captureException(
new Error(
e.message ||
e.error ||
e.status ||
e.toString() +
" -> " +
Object.keys(e)
.map((x) => `x: ${e[x]}`)
.join(", ")
.toString()
),
{
extra: {
rawError: e,
...extra,
},
}
);
}
Sentry.flush(2000).finally(() => reject(e));
});
}
process.on("unhandledRejection", (e) => {
console.error("Unhandled rejection:", e);
rejectWithSendError(e, { source: "unhandledRejection" }).finally(() => process.exit(1));
});
function wrappedMain(main) {
Sentry.setContext("info", {
argv: process.argv,
startTime: new Date(),
memoryUsage: process.memoryUsage(),
});
Sentry.setContext("os", {
uptime: os.uptime(),
release: os.release(),
version: os.version(),
networkInterfaces: os.networkInterfaces(),
hostname: os.hostname(),
loadavg: os.loadavg(),
freemem: os.freemem(),
userInfo: os.userInfo(),
});
Sentry.setContext("env", process.env);
return main().catch((e) => {
console.error("[wrappedMain] Error:", e);
return rejectWithSendError(e)
.finally(flushStdout)
.finally(() => process.exit(255));
});
}
exports["wrappedMain"] = (main) => () => wrappedMain(main);
exports["wrappedRun"] = (main) =>
wrappedMain(main)
.then((x) => console.log(require("util").inspect(x, { depth: 10 })))
.then(flushStdout)
.then(() => process.exit(0))
.catch(() => {});
// vim: sw=2:ts=2:expandtab:fdm=syntax