-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (54 loc) · 1.66 KB
/
index.js
File metadata and controls
65 lines (54 loc) · 1.66 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
const cluster = require('cluster');
const CPUs = require('os').cpus();
const { createApp } = require('./src/app');
const expressSession = require('express-session');
const { createClient } = require('redis');
const { persistCredentials } = require('./src/handlers/authHandlers.js');
require('dotenv').config();
const forkWorkers = () => {
CPUs.forEach((CPU) => {
console.log('\nCreating fork');
console.info(CPU);
cluster.fork();
console.log('Finished creating fork');
});
};
const onExit = (worker, code, signal) => {
console.error(`\nWorker ${worker.process.pid} died`);
console.info({ code, signal });
console.log("Let's fork another worker!");
cluster.fork();
};
const main = () => {
const { PORT, URL } = process.env;
const config = { ...process.env, persistCredentials };
const session = expressSession({
secret: config.SECRET,
resave: false,
saveUninitialized: false,
});
// Redis DB is properly working.
// const DB = createClient({ url: URL });
// DB.on('error', (err) => console.log('DB Client Error', err));
// DB.connect().then(() => {
// const app = createApp(config, session, DB);
// app.listen(PORT,
// () => console.log(`Server bound to http://localhost:${PORT}`));
// }
// );
if (cluster.isMaster) {
console.log(`Number of CPUs is ${CPUs.length}`);
console.log(`Master ${process.pid} is running`);
forkWorkers();
cluster.on('exit', onExit);
} else {
const app = createApp(config, session);
app.listen(+PORT, () =>
console.log(
`\nWorker ${process.pid} listening on port ${PORT}
Server started at http://localhost:${PORT}`
)
);
}
};
main();