index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 = 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. /**
  313. * Prints a job
  314. *
  315. * @param {object} job - the job
  316. * @param {number} layer - the layer
  317. */
  318. function printJob(job, layer) {
  319. const tabs = Array(layer).join("\t");
  320. console.log(`${tabs}${job.name} (${job.toString()}) ${job.status}`);
  321. job.childJobs.forEach(childJob => {
  322. printJob(childJob, layer + 1);
  323. });
  324. }
  325. /**
  326. * Prints a task
  327. *
  328. * @param {object} task - the task
  329. * @param {number} layer - the layer
  330. */
  331. function printTask(task, layer) {
  332. const tabs = Array(layer).join("\t");
  333. console.log(`${tabs}${task.job.name} (${task.job.toString()}) ${task.job.status} (priority: ${task.priority})`);
  334. task.job.childJobs.forEach(childJob => {
  335. printJob(childJob, layer + 1);
  336. });
  337. }
  338. process.stdin.on("data", data => {
  339. const command = data.toString().replace(/\r?\n|\r/g, "");
  340. if (command === "lockdown") {
  341. console.log("Locking down.");
  342. moduleManager._lockdown();
  343. }
  344. if (command === "status") {
  345. console.log("Status:");
  346. Object.keys(moduleManager.modules).forEach(moduleName => {
  347. const module = moduleManager.modules[moduleName];
  348. const tabsNeeded = 4 - Math.ceil((moduleName.length + 1) / 8);
  349. console.log(
  350. `${moduleName.toUpperCase()}${Array(tabsNeeded).join(
  351. "\t"
  352. )}${module.getStatus()}. Jobs in queue: ${module.jobQueue.lengthQueue()}. Jobs in progress: ${module.jobQueue.lengthRunning()}. Jobs paused: ${module.jobQueue.lengthPaused()} Concurrency: ${
  353. module.jobQueue.concurrency
  354. }. Stage: ${module.getStage()}`
  355. );
  356. });
  357. // moduleManager._lockdown();
  358. }
  359. if (command.startsWith("running")) {
  360. const parts = command.split(" ");
  361. moduleManager.modules[parts[1]].jobQueue.runningTasks.forEach(task => {
  362. printTask(task, 1);
  363. });
  364. }
  365. if (command.startsWith("queued")) {
  366. const parts = command.split(" ");
  367. moduleManager.modules[parts[1]].jobQueue.queue.forEach(task => {
  368. printTask(task, 1);
  369. });
  370. }
  371. if (command.startsWith("paused")) {
  372. const parts = command.split(" ");
  373. moduleManager.modules[parts[1]].jobQueue.pausedTasks.forEach(task => {
  374. printTask(task, 1);
  375. });
  376. }
  377. if (command.startsWith("stats")) {
  378. const parts = command.split(" ");
  379. console.log(moduleManager.modules[parts[1]].jobStatistics);
  380. }
  381. if (command.startsWith("jobinfo")) {
  382. const parts = command.split(" ");
  383. const uuid = parts[1];
  384. let jobFound = null;
  385. Object.keys(moduleManager.modules).forEach(moduleName => {
  386. const module = moduleManager.modules[moduleName];
  387. const task1 = module.jobQueue.runningTasks.find(task => task.job.uniqueId === uuid);
  388. const task2 = module.jobQueue.queue.find(task => task.job.uniqueId === uuid);
  389. const task3 = module.jobQueue.pausedTasks.find(task => task.job.uniqueId === uuid);
  390. if (task1) jobFound = task1.job;
  391. if (task2) jobFound = task2.job;
  392. if (task3) jobFound = task3.job;
  393. });
  394. if (jobFound) {
  395. let topParent = jobFound;
  396. let levelsDeep = 0;
  397. while (topParent.parentJob && topParent !== topParent.parentJob) {
  398. topParent = jobFound.parentJob;
  399. levelsDeep += 1;
  400. }
  401. console.log(
  402. `Found job, displaying that job and the full tree from the top parent job. The job is ${levelsDeep} levels deep from the top parent.`
  403. );
  404. console.log(jobFound);
  405. printJob(topParent, 1);
  406. } else console.log("Could not find job in any running, queued or paused lists in any module.");
  407. }
  408. // if (command.startsWith("debug")) {
  409. // }
  410. if (command.startsWith("eval")) {
  411. const evalCommand = command.replace("eval ", "");
  412. console.log(`Running eval command: ${evalCommand}`);
  413. // eslint-disable-next-line no-eval
  414. const response = eval(evalCommand);
  415. console.log(`Eval response: `, response);
  416. }
  417. });
  418. export default moduleManager;