-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
140 lines (115 loc) · 3.7 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require("dotenv").config({ path: ".env" });
const fs = require("fs");
const colors = require("colors");
const { Worker } = require("worker_threads");
const NODE_ENV = process.env.NODE_ENV || "development";
const buildDir = "./dist";
const logDirectory = "./logs";
const cli = require(`./${buildDir}/ririkoCli`);
const configFileExists = fs.existsSync("./config.js");
process
.on("unhandledRejection", (reason, p) => {
console.error(reason, "Unhandled Rejection at Promise", p);
})
.on("uncaughtException", (err) => {
console.error(err, "Uncaught Exception thrown");
console.oLog(err);
});
function createLogDirectory() {
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
}
}
const { overrideLoggers } = require(`${buildDir}/helpers/logger`);
const { twitchClientId } = require("./src/helpers/getconfig");
overrideLoggers();
function handleUnhandledRejection(err, promise) {
console.error(`[ANTI-CRASH] Unhandled Rejection: ${err}`.red);
console.error(promise);
}
function handleUncaughtException(err) {
console.error(`Uncaught Exception: ${err.message}`);
console.error(err);
}
function startRirikoBotWorker() {
const worker_RirikoBot = new Worker(`./${buildDir}/ririkoBot`);
worker_RirikoBot.on("message", (message) => {
if (message.ready) {
cli.log("NODE_ENV:".red, NODE_ENV);
cli.log(
"\n" +
`[READY] ${message.botUsername} is up and ready to go. ${
process.env.npm_package_version
? "\nRunning ririko@" + process.env.npm_package_version
: ""
}`.brightGreen
);
cli.log(
"\n==============================================================\n"
.white
);
cli.enablePrompt();
}
});
worker_RirikoBot.on("error", (error) => {
console.error(`[UNCAUGHT EXCEPTION] Ririko Bot:`, error);
});
}
function startRirikoStreamCheckerWorker() {
if (twitchClientId()) {
console.log(
"Twitch Client ID found in environment variables. Starting stream checker."
);
} else {
console.log(
"Twitch Client ID not found in environment variables. Not starting stream checker."
);
return;
}
const worker_ririkoStreamChecker = new Worker(
`./${buildDir}/ririkoStreamChecker`
);
worker_ririkoStreamChecker.on("error", (error) => {
console.error(`[UNCAUGHT EXCEPTION] Ririko Stream Notifier:`, error);
});
worker_ririkoStreamChecker.on("message", (message) => {
if (message.exit) {
worker_ririkoStreamChecker.terminate().then(() => {
console.error("Terminating stream checker due to unrecoverable error");
});
}
});
}
function startRirikoQueueManagerWorker() {
const worker_RirikoQueueManager = new Worker(
`./${buildDir}/ririkoQueueManager`
);
worker_RirikoQueueManager.on("message", (message) => {
console.log(`Message from Ririko Queue Manager: ${message}`);
});
worker_RirikoQueueManager.on("error", (error) => {
console.error(`[UNCAUGHT EXCEPTION] Ririko Stream Notifier:`, error);
});
}
function startRirikoExpressWorker() {
const worker_RirikoExpress = new Worker(`./${buildDir}/ririkoExpress.js`);
worker_RirikoExpress.on("message", (message) => {
if (message.ready) {
}
});
worker_RirikoExpress.on("error", (error) => {
console.error(`[UNCAUGHT EXCEPTION] Ririko Express:`, error);
});
}
(function () {
createLogDirectory();
process.on("unhandledRejection", handleUnhandledRejection);
process.on("uncaughtException", handleUncaughtException);
startRirikoExpressWorker();
// check if config.js file exists
if (configFileExists === true) {
startRirikoBotWorker();
startRirikoStreamCheckerWorker();
startRirikoQueueManagerWorker();
}
})();