index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. "use strict";
  2. const util = require("util");
  3. process.env.NODE_CONFIG_DIR = `${__dirname}/config`;
  4. const config = require("config");
  5. process.on("uncaughtException", (err) => {
  6. if (err.code === "ECONNREFUSED" || err.code === "UNCERTAIN_STATE") return;
  7. console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
  8. });
  9. const blacklistedConsoleLogs = [
  10. "Running job IO",
  11. "Ran job IO successfully",
  12. "Running job HGET",
  13. "Ran job HGET successfully",
  14. "Running job HGETALL",
  15. "Ran job HGETALL successfully",
  16. "Running job GET_ERROR",
  17. "Ran job GET_ERROR successfully",
  18. "Running job GET_SCHEMA",
  19. "Ran job GET_SCHEMA successfully",
  20. "Running job SUB",
  21. "Ran job SUB successfully",
  22. "Running job GET_MODEL",
  23. "Ran job GET_MODEL successfully",
  24. "Running job HSET",
  25. "Ran job HSET successfully",
  26. "Running job CAN_USER_VIEW_STATION",
  27. "Ran job CAN_USER_VIEW_STATION successfully",
  28. ];
  29. const oldConsole = {};
  30. oldConsole.log = console.log;
  31. console.log = (...args) => {
  32. const string = util.format.apply(null, args);
  33. let blacklisted = false;
  34. blacklistedConsoleLogs.forEach((blacklistedConsoleLog) => {
  35. if (string.indexOf(blacklistedConsoleLog) !== -1) blacklisted = true;
  36. });
  37. if (!blacklisted) oldConsole.log.apply(null, args);
  38. };
  39. const fancyConsole = config.get("fancyConsole");
  40. // class ModuleManager {
  41. // constructor() {
  42. // this.modules = {};
  43. // this.modulesInitialized = 0;
  44. // this.totalModules = 0;
  45. // this.modulesLeft = [];
  46. // this.i = 0;
  47. // this.lockdown = false;
  48. // this.fancyConsole = fancyConsole;
  49. // }
  50. // addModule(moduleName) {
  51. // console.log("add module", moduleName);
  52. // const moduleClass = new require(`./logic/${moduleName}`);
  53. // this.modules[moduleName] = new moduleClass(moduleName, this);
  54. // this.totalModules++;
  55. // this.modulesLeft.push(moduleName);
  56. // }
  57. // initialize() {
  58. // if (!this.modules["logger"]) return console.error("There is no logger module");
  59. // this.logger = this.modules["logger"];
  60. // if (this.fancyConsole) {
  61. // this.replaceConsoleWithLogger();
  62. // this.logger.reservedLines = Object.keys(this.modules).length + 5;
  63. // }
  64. // for (let moduleName in this.modules) {
  65. // let module = this.modules[moduleName];
  66. // if (this.lockdown) break;
  67. // module._onInitialize().then(() => {
  68. // this.moduleInitialized(moduleName);
  69. // });
  70. // let dependenciesInitializedPromises = [];
  71. // module.dependsOn.forEach(dependencyName => {
  72. // let dependency = this.modules[dependencyName];
  73. // dependenciesInitializedPromises.push(dependency._onInitialize());
  74. // });
  75. // module.lastTime = Date.now();
  76. // Promise.all(dependenciesInitializedPromises).then((res, res2) => {
  77. // if (this.lockdown) return;
  78. // this.logger.info("MODULE_MANAGER", `${moduleName} dependencies have been completed`);
  79. // module._initialize();
  80. // });
  81. // }
  82. // }
  83. // async printStatus() {
  84. // try { await Promise.race([this.logger._onInitialize(), this.logger._isInitialized()]); } catch { return; }
  85. // if (!this.fancyConsole) return;
  86. // let colors = this.logger.colors;
  87. // const rows = process.stdout.rows;
  88. // process.stdout.cursorTo(0, rows - this.logger.reservedLines);
  89. // process.stdout.clearScreenDown();
  90. // process.stdout.cursorTo(0, (rows - this.logger.reservedLines) + 2);
  91. // process.stdout.write(`${colors.FgYellow}Modules${colors.FgWhite}:\n`);
  92. // for (let moduleName in this.modules) {
  93. // let module = this.modules[moduleName];
  94. // let tabsAmount = Math.max(0, Math.ceil(2 - (moduleName.length / 8)));
  95. // let tabs = Array(tabsAmount).fill(`\t`).join("");
  96. // let timing = module.timeDifferences.map((timeDifference) => {
  97. // return `${colors.FgMagenta}${timeDifference}${colors.FgCyan}ms${colors.FgWhite}`;
  98. // }).join(", ");
  99. // let stateColor;
  100. // if (module.state === "NOT_INITIALIZED") stateColor = colors.FgWhite;
  101. // else if (module.state === "INITIALIZED") stateColor = colors.FgGreen;
  102. // else if (module.state === "LOCKDOWN" && !module.failed) stateColor = colors.FgRed;
  103. // else if (module.state === "LOCKDOWN" && module.failed) stateColor = colors.FgMagenta;
  104. // else stateColor = colors.FgYellow;
  105. // 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`);
  106. // }
  107. // }
  108. // moduleInitialized(moduleName) {
  109. // this.modulesInitialized++;
  110. // this.modulesLeft.splice(this.modulesLeft.indexOf(moduleName), 1);
  111. // this.logger.info("MODULE_MANAGER", `Initialized: ${this.modulesInitialized}/${this.totalModules}.`);
  112. // if (this.modulesLeft.length === 0) this.allModulesInitialized();
  113. // }
  114. // allModulesInitialized() {
  115. // this.logger.success("MODULE_MANAGER", "All modules have started!");
  116. // this.modules["discord"].sendAdminAlertMessage("The backend server started successfully.", "#00AA00", "Startup", false, []);
  117. // }
  118. // aModuleFailed(failedModule) {
  119. // this.logger.error("MODULE_MANAGER", `A module has failed, locking down. Module: ${failedModule.name}`);
  120. // this.modules["discord"].sendAdminAlertMessage(`The backend server failed to start due to a failing module: ${failedModule.name}.`, "#AA0000", "Startup", false, []);
  121. // this._lockdown();
  122. // }
  123. // replaceConsoleWithLogger() {
  124. // this.oldConsole = {
  125. // log: console.log,
  126. // debug: console.debug,
  127. // info: console.info,
  128. // warn: console.warn,
  129. // error: console.error
  130. // };
  131. // console.log = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  132. // console.debug = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  133. // console.info = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  134. // console.warn = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
  135. // console.error = (...args) => this.logger.error("CONSOLE", args.map(arg => util.format(arg)));
  136. // }
  137. // replaceLoggerWithConsole() {
  138. // console.log = this.oldConsole.log;
  139. // console.debug = this.oldConsole.debug;
  140. // console.info = this.oldConsole.info;
  141. // console.warn = this.oldConsole.warn;
  142. // console.error = this.oldConsole.error;
  143. // }
  144. // _lockdown() {
  145. // this.lockdown = true;
  146. // for (let moduleName in this.modules) {
  147. // let module = this.modules[moduleName];
  148. // if (module.lockdownImmune) continue;
  149. // module._lockdown();
  150. // }
  151. // }
  152. // }
  153. // const moduleManager = new ModuleManager();
  154. // module.exports = moduleManager;
  155. // moduleManager.addModule("cache");
  156. // moduleManager.addModule("db");
  157. // moduleManager.addModule("mail");
  158. // moduleManager.addModule("api");
  159. // moduleManager.addModule("app");
  160. // moduleManager.addModule("discord");
  161. // moduleManager.addModule("io");
  162. // moduleManager.addModule("logger");
  163. // moduleManager.addModule("notifications");
  164. // moduleManager.addModule("activities");
  165. // moduleManager.addModule("playlists");
  166. // moduleManager.addModule("punishments");
  167. // moduleManager.addModule("songs");
  168. // moduleManager.addModule("spotify");
  169. // moduleManager.addModule("stations");
  170. // moduleManager.addModule("tasks");
  171. // moduleManager.addModule("utils");
  172. // moduleManager.initialize();
  173. // process.stdin.on("data", function (data) {
  174. // if(data.toString() === "lockdown\r\n"){
  175. // console.log("Locking down.");
  176. // moduleManager._lockdown();
  177. // }
  178. // });
  179. // if (fancyConsole) {
  180. // const rows = process.stdout.rows;
  181. // for(let i = 0; i < rows; i++) {
  182. // process.stdout.write("\n");
  183. // }
  184. // }
  185. class ModuleManager {
  186. constructor() {
  187. this.modules = {};
  188. this.modulesNotInitialized = [];
  189. this.i = 0;
  190. this.lockdown = false;
  191. this.fancyConsole = fancyConsole;
  192. }
  193. addModule(moduleName) {
  194. console.log("add module", moduleName);
  195. const module = require(`./logic/${moduleName}`);
  196. this.modules[moduleName] = module;
  197. this.modulesNotInitialized.push(module);
  198. }
  199. initialize() {
  200. // if (!this.modules["logger"]) return console.error("There is no logger module");
  201. // this.logger = this.modules["logger"];
  202. // if (this.fancyConsole) {
  203. // this.replaceConsoleWithLogger();
  204. this.reservedLines = Object.keys(this.modules).length + 5;
  205. // }
  206. for (let moduleName in this.modules) {
  207. let module = this.modules[moduleName];
  208. module.setModuleManager(this);
  209. if (this.lockdown) break;
  210. module._initialize();
  211. // let dependenciesInitializedPromises = [];
  212. // module.dependsOn.forEach(dependencyName => {
  213. // let dependency = this.modules[dependencyName];
  214. // dependenciesInitializedPromises.push(dependency._onInitialize());
  215. // });
  216. // module.lastTime = Date.now();
  217. // Promise.all(dependenciesInitializedPromises).then((res, res2) => {
  218. // if (this.lockdown) return;
  219. // this.logger.info("MODULE_MANAGER", `${moduleName} dependencies have been completed`);
  220. // module._initialize();
  221. // });
  222. }
  223. }
  224. onInitialize(module) {
  225. if (this.modulesNotInitialized.indexOf(module) !== -1) {
  226. this.modulesNotInitialized.splice(
  227. this.modulesNotInitialized.indexOf(module),
  228. 1
  229. );
  230. console.log(
  231. "MODULE_MANAGER",
  232. `Initialized: ${Object.keys(this.modules).length -
  233. this.modulesNotInitialized.length}/${
  234. Object.keys(this.modules).length
  235. }.`
  236. );
  237. if (this.modulesNotInitialized.length === 0)
  238. this.onAllModulesInitialized();
  239. }
  240. }
  241. onFail(module) {
  242. if (this.modulesNotInitialized.indexOf(module) !== -1) {
  243. console.log("A module failed to initialize!");
  244. }
  245. }
  246. onAllModulesInitialized() {
  247. console.log("All modules initialized!");
  248. this.modules["discord"].runJob("SEND_ADMIN_ALERT_MESSAGE", {
  249. message: "The backend server started successfully.",
  250. color: "#00AA00",
  251. type: "Startup",
  252. critical: false,
  253. extraFields: [],
  254. });
  255. }
  256. }
  257. const moduleManager = new ModuleManager();
  258. moduleManager.addModule("cache");
  259. moduleManager.addModule("db");
  260. moduleManager.addModule("mail");
  261. moduleManager.addModule("activities");
  262. moduleManager.addModule("api");
  263. moduleManager.addModule("app");
  264. moduleManager.addModule("discord");
  265. moduleManager.addModule("io");
  266. moduleManager.addModule("notifications");
  267. moduleManager.addModule("playlists");
  268. moduleManager.addModule("punishments");
  269. moduleManager.addModule("songs");
  270. moduleManager.addModule("spotify");
  271. moduleManager.addModule("stations");
  272. moduleManager.addModule("tasks");
  273. moduleManager.addModule("utils");
  274. moduleManager.initialize();
  275. process.stdin.on("data", function(data) {
  276. if (data.toString() === "lockdown\r\n") {
  277. console.log("Locking down.");
  278. moduleManager._lockdown();
  279. }
  280. if (data.toString() === "status\r\n") {
  281. console.log("Status:");
  282. for (let moduleName in moduleManager.modules) {
  283. let module = moduleManager.modules[moduleName];
  284. const tabsNeeded = 4 - Math.ceil((moduleName.length + 1) / 8);
  285. console.log(
  286. `${moduleName.toUpperCase()}${Array(tabsNeeded).join(
  287. "\t"
  288. )}${module.getStatus()}. Jobs in queue: ${module.jobQueue.length()}. Jobs in progress: ${module.jobQueue.running()}. Concurrency: ${
  289. module.jobQueue.concurrency
  290. }. Stage: ${module.getStage()}`
  291. );
  292. }
  293. // moduleManager._lockdown();
  294. }
  295. if (data.toString().startsWith("running")) {
  296. const parts = data
  297. .toString()
  298. .substr(0, data.toString().length - 2)
  299. .split(" ");
  300. console.log(moduleManager.modules[parts[1]].runningJobs);
  301. }
  302. if (data.toString().startsWith("stats")) {
  303. const parts = data
  304. .toString()
  305. .substr(0, data.toString().length - 2)
  306. .split(" ");
  307. console.log(moduleManager.modules[parts[1]].jobStatistics);
  308. }
  309. if (data.toString().startsWith("debug")) {
  310. moduleManager.modules["utils"].runJob("DEBUG");
  311. }
  312. });
  313. module.exports = moduleManager;