This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.js
72 lines (65 loc) · 2.09 KB
/
run.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
"use strict"
const yaml = require('js-yaml');
const fs = require('fs');
const createLogger = require('logging').default;
const cluster = require('cluster');
// Read the configuration files.
const config = yaml.load(fs.readFileSync('config.yaml'));
// Get credentials and such from environment variables
const credentials = {
web: {
endpoint: process.env.WEB_ENDPOINT,
token: process.env.WEB_TOKEN,
},
redis: {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
},
discord: {
client: process.env.DISCORD_CLIENT,
token: process.env.DISCORD_TOKEN,
channel: process.env.DISCORD_CHANNEL,
}
};
if (cluster.isMaster) {
// Fork out workers
for (let i = 0; i < (config['cores'] || 1); i++) {
const env = {};
if (i == 0) {
env.FIRST_WORKER = true;
}
const worker = cluster.fork(env);
worker.on('message', (message) => {
for (const id in cluster.workers) {
cluster.workers[id].send(message);
}
});
}
} else {
// Worker
const NetworkListener = require('./net/NetworkListener');
const Redis = require('./database/Redis');
const Discord = require('./discord/Discord');
global.server = new NetworkListener(config['network'])
global.redis = new Redis(credentials.redis);
if (process.env.DATABASE == 'web') {
const WebAPI = require('./database/WebAPI');
global.database = new WebAPI(credentials.web);
} else {
global.database = global.redis;
}
// Load message functions
require('./net/DatabaseMessages.js');
require('./net/AreaMessages.js');
require('./net/ChallengeMessages.js');
require('./net/SessionMessages.js');
if (process.env.FIRST_WORKER && credentials.discord.client) {
global.discord = new Discord(credentials['discord']);
}
// Handle messages from other processes.
process.on('message', (message) => {
const cmd = message.cmd;
delete message.cmd;
process.emit(cmd, message);
});
}