-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
93 lines (79 loc) · 3.56 KB
/
index.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
#!/usr/bin/env node
/*
* oooooooooo. ooo ooooo ooooooooo.
* `888' `Y8b `88. .888' `888 `Y88.
* 888 888 .ooooo. .oooo. ooo. .oo. .oo. 888b d'888 888 .d88'
* 888oooo888' d88' `88b `P )88b `888P"Y88bP"Y88b 8 Y88. .P 888 888ooo88P'
* 888 `88b 888ooo888 .oP"888 888 888 888 8 `888' 888 888
* 888 .88P 888 .o d8( 888 888 888 888 8 Y 888 888
* o888bood8P' `Y8bod8P' `Y888""8o o888o o888o o888o o8o o888o o888o
* ========================================================================
* Copyright (c) 2019-2023 BeamMP Ltd. All rights reserved.
*/
require('dotenv').config()
const pkg = require('./package.json')
const chalk = require('chalk');
const cluster = require('cluster');
const error = chalk.bold.keyword('red');
const warn = chalk.keyword('orange');
const good = chalk.keyword('lime');
process.on('warning', (warning) => {
console.log(warning.stack);
});
if (cluster.isMaster) {
const env = process.env.NODE_ENV || 'development'
console.log('oooooooooo. ooo ooooo ooooooooo. ')
console.log('`888\' `Y8b `88. .888\' `888 `Y88. ')
console.log(' 888 888 .ooooo. .oooo. ooo. .oo. .oo. 888b d\'888 888 .d88\' ')
console.log(' 888oooo888\' d88\' `88b `P )88b `888P"Y88bP"Y88b 8 Y88. .P 888 888ooo88P\' ')
console.log(' 888 `88b 888ooo888 .oP"888 888 888 888 8 `888\' 888 888 ')
console.log(' 888 .88P 888 .o d8( 888 888 888 888 8 Y 888 888 ')
console.log('o888bood8P\' `Y8bod8P\' `Y888""8o o888o o888o o888o o8o o888o o888o ')
console.log('=================================================================================')
console.log('Website v' + pkg.version + ' Copyright (C) 2019-2024 BeamMP Ltd')
console.log('')
console.log('Running in: ' + env)
console.log('Server Time: ' + new Date())
function start() {
process.title = pkg.name + "@" + pkg.version;
if (cluster.isMaster) {
console.log(`Master PID: ${process.pid}`)
console.log(`Creating ${process.env.INSTANCES} Instances of the Website Backend`)
for (let i = 0; i < process.env.INSTANCES; i++) {
cluster.fork();
}
// set console's directory so we can see output from workers
console.dir(cluster.workers, { depth: 0 });
cluster.on('exit', (worker, code) => {
// Good exit code is 0 :))
// exitedAfterDisconnect ensures that it is not killed by master cluster or manually
// if we kill it via .kill or .disconnect it will be set to true
// \x1b[XXm represents a color, and [0m represent the end of this
//color in the console ( 0m sets it to white again )
if (code !== 0 && !worker.exitedAfterDisconnect) {
console.error(`\x1b[34mWorker ${worker.process.pid} crashed... Starting a new worker...\x1b[0m`);
const nw = cluster.fork();
console.error(`\x1b[32mWorker ${nw.process.pid} will replace him \x1b[0m`);
}
});
} else {
console.error('FATAL: This script can only be run as a master process.')
}
}
start()
} else {
const ws = require('./src/webserver')
try {
ws.init(function (err) {
if (err) {
console.error(err)
return
}
ws.listen(function () {
console.info('BeamMP Website Ready')
})
})
} catch (e) {
throw e;
}
}