-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path5-cluster.js
More file actions
105 lines (91 loc) · 2.44 KB
/
5-cluster.js
File metadata and controls
105 lines (91 loc) · 2.44 KB
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
'use strict';
const cluster = require('node:cluster');
const timers = require('node:timers/promises');
const http = require('node:http');
const connections = new Map();
let server = null;
let child = null;
const SERVER_PORT = 8000;
const LONG_RESPONSE = 60000;
const SHUTDOWN_TIMEOUT = 5000;
const HTTP_REFRESH = {
'Content-Type': 'text/html',
Refresh: '5',
};
const start = () => {
console.log('Fork process');
child = cluster.fork('./5-cluster.js');
child.on('message', (message) => {
if (message.status === 'restarted') {
console.log('Restart worker');
start();
}
});
};
const showConnections = () => {
console.log('Connection:', [...connections.values()].length);
for (const connection of connections.keys()) {
const { remoteAddress, remotePort } = connection;
console.log(` ${remoteAddress}:${remotePort}`);
}
};
const closeConnections = async () => {
for (const [connection, res] of connections.entries()) {
connections.delete(connection);
res.writeHead(503, HTTP_REFRESH);
res.end('Service is unavailable');
connection.destroy();
}
};
const freeResources = async () => {
console.log('Free resources');
};
const gracefulShutdown = async () => {
process.send({ status: 'restarted' });
server.close((error) => {
console.log('Detach from HTTP listener');
if (error) {
console.log(error);
process.exit(1);
}
process.exit(0);
});
await timers.setTimeout(SHUTDOWN_TIMEOUT);
await freeResources();
await closeConnections();
};
if (cluster.isPrimary) {
start();
process.on('SIGINT', async () => {
child.send({ status: 'restart' });
});
} else {
server = http.createServer(async (req, res) => {
console.log('New request');
connections.set(res.connection, res);
await timers.setTimeout(LONG_RESPONSE);
res.end('Example output');
});
server.on('connection', (connection) => {
console.log('New connection');
connection.on('close', () => {
console.log('Close');
connections.delete(connection);
});
});
server.listen(SERVER_PORT);
server.on('listening', () => {
console.log('Attach to HTTP listener');
});
process.on('message', async (message) => {
if (message.status === 'restart') {
console.log();
console.log('Graceful shutdown');
showConnections();
await gracefulShutdown();
showConnections();
console.log('Worker exited');
}
});
process.on('SIGINT', () => {});
}