This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.js
71 lines (61 loc) · 2.02 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
const fs = require("fs");
const pm2 = require("pm2");
const path = require("path");
/** @type {pm2.StartOptions[]} */
const DefaultProcesses = [
{
name: "Server1",
env: {
OGARX_MODE: "default/mega",
OGARX_PORT: 3000,
OGARX_SERVER: "Megasplit",
OGARX_ENDPOINT: "mega",
OGARX_TOKEN: process.env.OGARX_TOKEN
}
}
];
/** @type {pm2.StartOptions[]} */
let config = null;
if (!fs.existsSync("config.json")) {
fs.writeFileSync("config.json", JSON.stringify(DefaultProcesses, null, 4));
config = DefaultProcesses;
} else {
config = require("./config.json");
}
const validateMode = mode => fs.existsSync(path.resolve(__dirname, "src", "modes", `${mode}.js`));
const { GATEWAY_PORT, GATEWAY_ORIGIN } = process.env;
/** @type {pm2.StartOptions[]} */
const procToStart = [];
for (const proc of config) {
proc.cwd = __dirname;
proc.script = "./src/index.js";
proc.max_memory_restart = "300M";
proc.kill_timeout = 3000;
if (validateMode(proc.env.OGARX_MODE)) {
procToStart.push(proc);
} else {
console.warn(`Unable to find mode "${proc.env.OGARX_MODE}", aborting process "${proc.name}"`);
}
}
pm2.connect(err => {
if (err) return console.error("Failed to connect to pm2", err);
pm2.start({
name: "Gateway",
cwd: __dirname,
script: "./src/gateway.js",
wait_ready: true,
env: { GATEWAY_PORT, GATEWAY_ORIGIN }
}, e => {
if (e) console.error(e);
else console.log("Gateway Process started");
Promise.all(procToStart.map(proc => new Promise(res => {
pm2.start(proc, e => {
if (e) console.error(e);
else console.log(`PM2 Process "${proc.name}" ` +
`(${proc.env.OGARX_MODE}-${proc.env.OGARX_SERVER}) mounted on ` +
`:${proc.env.OGARX_PORT || 443}/${proc.env.OGARX_ENDPOINT}`);
res();
});
}))).then(() => process.exit(0));
});
});