index.js 13 KB

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