blob: 546465c039e68e9e671e397ca0d9dc891f277310 (
plain)
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
|
import { isMaster, fork, on } from "cluster";
import { cpus } from "os";
import { createServer } from "http";
const capacity = cpus().length;
let thrown = false;
if (isMaster) {
console.log(capacity);
for (let i = 0; i < capacity; i++) {
fork();
}
on("exit", (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
fork();
});
} else {
const port = 1234;
createServer().listen(port, () => {
console.log('process id local', process.pid);
console.log(`http server started at port ${port}`);
if (!thrown) {
thrown = true;
setTimeout(() => {
throw new Error("Hey I'm a fake error!");
}, 1000);
}
});
}
process.on('uncaughtException', function (err) {
console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
process.exit(1);
});
|