aboutsummaryrefslogtreecommitdiff
path: root/src/server/session_manager/session_manager_cluster.ts
blob: bfe6187c3a3627d0d82c919995d66f18915118ca (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
import { isMaster, fork, on } from "cluster";
import { cpus } from "os";
import { createServer } from "http";

const capacity = cpus().length;

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`);
    });
} else {
    const port = 1234;
    createServer().listen(port, () => {
        console.log('process id local', process.pid);
        console.log(`http server started at port ${port}`);
    });
}

process.on('uncaughtException', function (err) {
    console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
    console.error(err.stack);
    process.exit(1);
});