index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import "./loadEnvVariables.js";
  2. import util from "util";
  3. import config from "config";
  4. Array.prototype.remove = function (item) {
  5. this.splice(this.indexOf(item), 1);
  6. };
  7. process.on("uncaughtException", err => {
  8. if (err.code === "ECONNREFUSED" || err.code === "UNCERTAIN_STATE") return;
  9. console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
  10. });
  11. const blacklistedConsoleLogs = [
  12. "Running job IO",
  13. "Ran job IO successfully",
  14. "Running job HGET",
  15. "Ran job HGET successfully",
  16. "Running job HGETALL",
  17. "Ran job HGETALL successfully",
  18. "Running job GET_ERROR",
  19. "Ran job GET_ERROR successfully",
  20. "Running job GET_SCHEMA",
  21. "Ran job GET_SCHEMA successfully",
  22. "Running job SUB",
  23. "Ran job SUB successfully",
  24. "Running job GET_MODEL",
  25. "Ran job GET_MODEL successfully",
  26. "Running job HSET",
  27. "Ran job HSET successfully",
  28. "Running job CAN_USER_VIEW_STATION",
  29. "Ran job CAN_USER_VIEW_STATION successfully"
  30. ];
  31. const oldConsole = {};
  32. oldConsole.log = console.log;
  33. console.log = (...args) => {
  34. const string = util.format.apply(null, args);
  35. let blacklisted = false;
  36. blacklistedConsoleLogs.forEach(blacklistedConsoleLog => {
  37. if (string.indexOf(blacklistedConsoleLog) !== -1) blacklisted = true;
  38. });
  39. if (!blacklisted) oldConsole.log.apply(null, args);
  40. };
  41. const fancyConsole = config.get("fancyConsole");
  42. if (config.debug && config.debug.traceUnhandledPromises === true) {
  43. console.log("Enabled trace-unhandled/register");
  44. import("trace-unhandled/register");
  45. }
  46. // class ModuleManager {
  47. // constructor() {
  48. // this.modules = {};
  49. // this.modulesInitialized = 0;
  50. // this.totalModules = 0;
  51. // this.modulesLeft = [];
  52. // this.i = 0;
  53. // this.lockdown = false;
  54. // this.fancyConsole = fancyConsole;
  55. // }
  56. // addModule(moduleName) {
  57. // console.log("add module", moduleName);
  58. // const moduleClass = new require(`./logic/${moduleName}`);
  59. // this.modules[moduleName] = new moduleClass(moduleName, this);
  60. // this.totalModules++;
  61. // this.modulesLeft.push(moduleName);
  62. // }
  63. // initialize() {
  64. // if (!this.modules["logger"]) return console.error("There is no logger module");
  65. // this.logger = this.modules["logger"];
  66. // if (this.fancyConsole) {
  67. // this.replaceConsoleWithLogger();
  68. // this.logger.reservedLines = Object.keys(this.modules).length + 5;
  69. // }
  70. // for (let moduleName in this.modules) {
  71. // let module = this.modules[moduleName];
  72. // if (this.lockdown) break;
  73. // module._onInitialize().then(() => {
  74. // this.moduleInitialized(moduleName);
  75. // });
  76. // let dependenciesInitializedPromises = [];
  77. // module.dependsOn.forEach(dependencyName => {
  78. // let dependency = this.modules[dependencyName];
  79. // dependenciesInitializedPromises.push(dependency._onInitialize());
  80. // });
  81. // module.lastTime = Date.now();
  82. // Promise.all(dependenciesInitializedPromises).then((res, res2) => {
  83. // if (this.lockdown) return;
  84. // this.logger.info("MODULE_MANAGER", `${moduleName} dependencies have been completed`);
  85. // module._initialize();
  86. // });
  87. // }
  88. // }
  89. // async printStatus() {
  90. // try { await Promise.race([this.logger._onInitialize(), this.logger._isInitialized()]); } catch { return; }
  91. // if (!this.fancyConsole) return;
  92. // let colors = this.logger.colors;
  93. // const rows = process.stdout.rows;
  94. // process.stdout.cursorTo(0, rows - this.logger.reservedLines);
  95. // process.stdout.clearScreenDown();
  96. // process.stdout.cursorTo(0, (rows - this.logger.reservedLines) + 2);
  97. // process.stdout.write(`${colors.FgYellow}Modules${colors.FgWhite}:\n`);
  98. // for (let moduleName in this.modules) {
  99. // let module = this.modules[moduleName];
  100. // let tabsAmount = Math.max(0, Math.ceil(2 - (moduleName.length / 8)));
  101. // let tabs = Array(tabsAmount).fill(`\t`).join("");
  102. // let timing = module.timeDifferences.map((timeDifference) => {
  103. // return `${colors.FgMagenta}${timeDifference}${colors.FgCyan}ms${colors.FgWhite}`;
  104. // }).join(", ");
  105. // let stateColor;
  106. // if (module.state === "NOT_INITIALIZED") stateColor = colors.FgWhite;
  107. // else if (module.state === "INITIALIZED") stateColor = colors.FgGreen;
  108. // else if (module.state === "LOCKDOWN" && !module.failed) stateColor = colors.FgRed;
  109. // else if (module.state === "LOCKDOWN" && module.failed) stateColor = colors.FgMagenta;
  110. // else stateColor = colors.FgYellow;
  111. // process.stdout.write(`${moduleName}${tabs}${stateColor}${module.state}\t${colors.FgYellow}Stage: ${colors.FgRed}${module.stage}${colors.FgWhite}. ${colors.FgYellow}Timing${colors.FgWhite}: [${timing}]${colors.FgWhite}${colors.FgWhite}. ${colors.FgYellow}Total time${colors.FgWhite}: ${colors.FgRed}${module.totalTimeInitialize}${colors.FgCyan}ms${colors.Reset}\n`);
  112. // }
  113. // }
  114. // moduleInitialized(moduleName) {
  115. // this.modulesInitialized++;
  116. // this.modulesLeft.splice(this.modulesLeft.indexOf(moduleName), 1);
  117. // this.logger.info("MODULE_MANAGER", `Initialized: ${this.modulesInitialized}/${this.totalModules}.`);
  118. // if (this.modulesLeft.length === 0) this.allModulesInitialized();
  119. // }
  120. // allModulesInitialized() {
  121. // this.logger.success("MODULE_MANAGER", "All modules have started!");
  122. // this.modules["discord"].sendAdminAlertMessage("The backend server started successfully.", "#00AA00", "Startup", false, []);
  123. // }
  124. // aModuleFailed(failedModule) {
  125. // this.logger.error("MODULE_MANAGER", `A module has failed, locking down. Module: ${failedModule.name}`);
  126. // this.modules["discord"].sendAdminAlertMessage(`The backend server failed to start due to a failing module: ${failedModule.name}.`, "#AA0000", "Startup", false, []);
  127. // this._lockdown();
  128. // }
  129. // replaceConsoleWithLogger() {
  130. // this.oldConsole = {
  131. // log: console.log,
  132. // debug: console.debug,
  133. // info: console.info,
  134. // warn: console.warn,
  135. // error: console.error
  136. // };
  137. // console.log = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  138. // console.debug = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  139. // console.info = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  140. // console.warn = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  141. // console.error = (...args) => this.logger.error("CONSOLE", args.map(arg => util.format(arg)));
  142. // }
  143. // replaceLoggerWithConsole() {
  144. // console.log = this.oldConsole.log;
  145. // console.debug = this.oldConsole.debug;
  146. // console.info = this.oldConsole.info;
  147. // console.warn = this.oldConsole.warn;
  148. // console.error = this.oldConsole.error;
  149. // }
  150. // _lockdown() {
  151. // this.lockdown = true;
  152. // for (let moduleName in this.modules) {
  153. // let module = this.modules[moduleName];
  154. // if (module.lockdownImmune) continue;
  155. // module._lockdown();
  156. // }
  157. // }
  158. // }
  159. // const moduleManager = new ModuleManager();
  160. // module.exports = moduleManager;
  161. // moduleManager.addModule("cache");
  162. // moduleManager.addModule("db");
  163. // moduleManager.addModule("mail");
  164. // moduleManager.addModule("api");
  165. // moduleManager.addModule("app");
  166. // moduleManager.addModule("discord");
  167. // moduleManager.addModule("io");
  168. // moduleManager.addModule("logger");
  169. // moduleManager.addModule("notifications");
  170. // moduleManager.addModule("activities");
  171. // moduleManager.addModule("playlists");
  172. // moduleManager.addModule("punishments");
  173. // moduleManager.addModule("songs");
  174. // moduleManager.addModule("spotify");
  175. // moduleManager.addModule("stations");
  176. // moduleManager.addModule("tasks");
  177. // moduleManager.addModule("utils");
  178. // moduleManager.initialize();
  179. // process.stdin.on("data", function (data) {
  180. // if(data.toString() === "lockdown\r\n"){
  181. // console.log("Locking down.");
  182. // moduleManager._lockdown();
  183. // }
  184. // });
  185. // if (fancyConsole) {
  186. // const rows = process.stdout.rows;
  187. // for(let i = 0; i < rows; i++) {
  188. // process.stdout.write("\n");
  189. // }
  190. // }
  191. class ModuleManager {
  192. // eslint-disable-next-line require-jsdoc
  193. constructor() {
  194. this.modules = {};
  195. this.modulesNotInitialized = [];
  196. this.i = 0;
  197. this.lockdown = false;
  198. this.fancyConsole = fancyConsole;
  199. this.debugLogs = {
  200. stationIssue: []
  201. };
  202. this.debugJobs = {
  203. all: [],
  204. completed: []
  205. };
  206. this.name = "MODULE_MANAGER";
  207. }
  208. /**
  209. * Adds a new module to the backend server/module manager
  210. *
  211. * @param {string} moduleName - the name of the module (also needs to be the same as the filename of a module located in the logic folder or "logic/moduleName/index.js")
  212. */
  213. async addModule(moduleName) {
  214. this.log("INFO", "Adding module", moduleName);
  215. // import(`./logic/${moduleName}`).then(Module => {
  216. // // eslint-disable-next-line new-cap
  217. // const instantiatedModule = new Module.default();
  218. // this.modules[moduleName] = instantiatedModule;
  219. // this.modulesNotInitialized.push(instantiatedModule);
  220. // if (moduleName === "cache") console.log(56, this.modules);
  221. // });
  222. this.modules[moduleName] = import(`./logic/${moduleName}`);
  223. }
  224. /**
  225. * Initialises a new module to the backend server/module manager
  226. *
  227. */
  228. async initialize() {
  229. // if (!this.modules["logger"]) return console.error("There is no logger module");
  230. // this.logger = this.modules["logger"];
  231. // if (this.fancyConsole) {
  232. // this.replaceConsoleWithLogger();
  233. this.reservedLines = Object.keys(this.modules).length + 5;
  234. // }
  235. await Promise.all(Object.values(this.modules)).then(modules => {
  236. for (let module = 0; module < modules.length; module += 1) {
  237. this.modules[modules[module].default.name] = modules[module].default;
  238. this.modulesNotInitialized.push(modules[module].default);
  239. }
  240. }); // ensures all modules are imported, then converts promise to the default export of the import
  241. for (let moduleId = 0, moduleNames = Object.keys(this.modules); moduleId < moduleNames.length; moduleId += 1) {
  242. const module = this.modules[moduleNames[moduleId]];
  243. module.setModuleManager(this);
  244. if (this.lockdown) break;
  245. module._initialize();
  246. // let dependenciesInitializedPromises = [];
  247. // module.dependsOn.forEach(dependencyName => {
  248. // let dependency = this.modules[dependencyName];
  249. // dependenciesInitializedPromises.push(dependency._onInitialize());
  250. // });
  251. // module.lastTime = Date.now();
  252. // Promise.all(dependenciesInitializedPromises).then((res, res2) => {
  253. // if (this.lockdown) return;
  254. // this.logger.info("MODULE_MANAGER", `${moduleName} dependencies have been completed`);
  255. // module._initialize();
  256. // });
  257. }
  258. }
  259. /**
  260. * Called when a module is initialised
  261. *
  262. * @param {object} module - the module object/class
  263. */
  264. onInitialize(module) {
  265. if (this.modulesNotInitialized.indexOf(module) !== -1) {
  266. this.modulesNotInitialized.splice(this.modulesNotInitialized.indexOf(module), 1);
  267. this.log(
  268. "INFO",
  269. `Initialized: ${Object.keys(this.modules).length - this.modulesNotInitialized.length}/${
  270. Object.keys(this.modules).length
  271. }.`
  272. );
  273. if (this.modulesNotInitialized.length === 0) this.onAllModulesInitialized();
  274. }
  275. }
  276. /**
  277. * Called when a module fails to initialise
  278. *
  279. * @param {object} module - the module object/class
  280. */
  281. onFail(module) {
  282. if (this.modulesNotInitialized.indexOf(module) !== -1) {
  283. this.log("ERROR", "A module failed to initialize!");
  284. }
  285. }
  286. /**
  287. * Called when every module has initialised
  288. *
  289. */
  290. onAllModulesInitialized() {
  291. this.log("INFO", "All modules initialized!");
  292. if (this.modules.discord) {
  293. this.modules.discord.runJob("SEND_ADMIN_ALERT_MESSAGE", {
  294. message: "The backend server started successfully.",
  295. color: "#00AA00",
  296. type: "Startup",
  297. critical: false,
  298. extraFields: []
  299. });
  300. } else this.log("INFO", "No Discord module, so not sending an admin alert message.");
  301. }
  302. /**
  303. * Creates a new log message
  304. *
  305. * @param {...any} args - anything to be included in the log message, the first argument is the type of log
  306. */
  307. log(...args) {
  308. const _arguments = Array.from(args);
  309. const type = _arguments[0];
  310. _arguments.splice(0, 1);
  311. const start = `|${this.name.toUpperCase()}|`;
  312. const numberOfSpacesNeeded = 20 - start.length;
  313. _arguments.unshift(`${start}${Array(numberOfSpacesNeeded).join(" ")}`);
  314. if (type === "INFO") {
  315. _arguments[0] += "\x1b[36m";
  316. _arguments.push("\x1b[0m");
  317. console.log.apply(null, _arguments);
  318. } else if (type === "ERROR") {
  319. _arguments[0] += "\x1b[31m";
  320. _arguments.push("\x1b[0m");
  321. console.error.apply(null, _arguments);
  322. }
  323. }
  324. }
  325. const moduleManager = new ModuleManager();
  326. moduleManager.addModule("cache");
  327. moduleManager.addModule("db");
  328. moduleManager.addModule("mail");
  329. moduleManager.addModule("activities");
  330. moduleManager.addModule("api");
  331. moduleManager.addModule("app");
  332. moduleManager.addModule("discord");
  333. moduleManager.addModule("io");
  334. moduleManager.addModule("notifications");
  335. moduleManager.addModule("playlists");
  336. moduleManager.addModule("punishments");
  337. moduleManager.addModule("songs");
  338. moduleManager.addModule("spotify");
  339. moduleManager.addModule("stations");
  340. moduleManager.addModule("tasks");
  341. moduleManager.addModule("utils");
  342. moduleManager.initialize();
  343. process.stdin.on("data", data => {
  344. const command = data.toString().replace(/\r?\n|\r/g, "");
  345. if (command === "lockdown") {
  346. console.log("Locking down.");
  347. moduleManager._lockdown();
  348. }
  349. if (command === "status") {
  350. console.log("Status:");
  351. Object.keys(moduleManager.modules).forEach(moduleName => {
  352. const module = moduleManager.modules[moduleName];
  353. const tabsNeeded = 4 - Math.ceil((moduleName.length + 1) / 8);
  354. console.log(
  355. `${moduleName.toUpperCase()}${Array(tabsNeeded).join(
  356. "\t"
  357. )}${module.getStatus()}. Jobs in queue: ${module.jobQueue.lengthQueue()}. Jobs in progress: ${module.jobQueue.lengthRunning()}. Concurrency: ${
  358. module.jobQueue.concurrency
  359. }. Stage: ${module.getStage()}`
  360. );
  361. });
  362. // moduleManager._lockdown();
  363. }
  364. if (command.startsWith("running")) {
  365. const parts = command.split(" ");
  366. console.log(moduleManager.modules[parts[1]].jobQueue.runningTasks);
  367. }
  368. if (command.startsWith("queued")) {
  369. const parts = command.split(" ");
  370. console.log(moduleManager.modules[parts[1]].jobQueue.queue);
  371. }
  372. if (command.startsWith("paused")) {
  373. const parts = command.split(" ");
  374. console.log(moduleManager.modules[parts[1]].jobQueue.pausedTasks);
  375. }
  376. if (command.startsWith("stats")) {
  377. const parts = command.split(" ");
  378. console.log(moduleManager.modules[parts[1]].jobStatistics);
  379. }
  380. if (command.startsWith("debug")) {
  381. moduleManager.modules.utils.runJob("DEBUG");
  382. }
  383. });
  384. export default moduleManager;