index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import "./loadEnvVariables.js";
  2. import util from "util";
  3. import config from "config";
  4. const REQUIRED_CONFIG_VERSION = 8;
  5. // eslint-disable-next-line
  6. Array.prototype.remove = function (item) {
  7. this.splice(this.indexOf(item), 1);
  8. };
  9. process.on("uncaughtException", err => {
  10. if (err.code === "ECONNREFUSED" || err.code === "UNCERTAIN_STATE") return;
  11. console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
  12. });
  13. const blacklistedConsoleLogs = [];
  14. const oldConsole = {};
  15. oldConsole.log = console.log;
  16. console.log = (...args) => {
  17. const string = util.format.apply(null, args);
  18. let blacklisted = false;
  19. blacklistedConsoleLogs.forEach(blacklistedConsoleLog => {
  20. if (string.indexOf(blacklistedConsoleLog) !== -1) blacklisted = true;
  21. });
  22. if (!blacklisted) oldConsole.log.apply(null, args);
  23. };
  24. if (
  25. (!config.has("configVersion") || config.get("configVersion") !== REQUIRED_CONFIG_VERSION) &&
  26. !(config.has("skipConfigVersionCheck") && config.get("skipConfigVersionCheck"))
  27. ) {
  28. console.log(
  29. "CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
  30. );
  31. process.exit();
  32. }
  33. if (config.debug && config.debug.traceUnhandledPromises === true) {
  34. console.log("Enabled trace-unhandled/register");
  35. import("trace-unhandled/register");
  36. }
  37. class JobManager {
  38. // eslint-disable-next-line require-jsdoc
  39. constructor() {
  40. this.runningJobs = {};
  41. }
  42. /**
  43. * Adds a job to the list of running jobs
  44. *
  45. * @param {object} job - the job object
  46. */
  47. addJob(job) {
  48. if (!this.runningJobs[job.module.name]) this.runningJobs[job.module.name] = {};
  49. this.runningJobs[job.module.name][job.toString()] = job;
  50. }
  51. /**
  52. * Removes a job from the list of running jobs (after it's completed)
  53. *
  54. * @param {object} job - the job object
  55. */
  56. removeJob(job) {
  57. if (!this.runningJobs[job.module.name]) this.runningJobs[job.module.name] = {};
  58. delete this.runningJobs[job.module.name][job.toString()];
  59. }
  60. /**
  61. * Returns detail about a job via a identifier
  62. *
  63. * @param {string} uuid - the job identifier
  64. * @returns {object} - the job object
  65. */
  66. getJob(uuid) {
  67. let job = null;
  68. Object.keys(this.runningJobs).forEach(moduleName => {
  69. if (this.runningJobs[moduleName][uuid]) job = this.runningJobs[moduleName][uuid];
  70. });
  71. return job;
  72. }
  73. }
  74. class ModuleManager {
  75. // eslint-disable-next-line require-jsdoc
  76. constructor() {
  77. this.modules = {};
  78. this.modulesNotInitialized = [];
  79. this.jobManager = new JobManager();
  80. this.i = 0;
  81. this.lockdown = false;
  82. this.debugLogs = {
  83. stationIssue: []
  84. };
  85. this.debugJobs = {
  86. all: [],
  87. completed: []
  88. };
  89. this.name = "MODULE_MANAGER";
  90. }
  91. /**
  92. * Adds a new module to the backend server/module manager
  93. *
  94. * @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")
  95. */
  96. async addModule(moduleName) {
  97. this.log("INFO", "Adding module", moduleName);
  98. this.modules[moduleName] = import(`./logic/${moduleName}`);
  99. }
  100. /**
  101. * Initialises a new module to the backend server/module manager
  102. *
  103. */
  104. async initialize() {
  105. this.reservedLines = Object.keys(this.modules).length + 5;
  106. await Promise.all(Object.values(this.modules)).then(modules => {
  107. for (let module = 0; module < modules.length; module += 1) {
  108. this.modules[modules[module].default.name] = modules[module].default;
  109. this.modulesNotInitialized.push(modules[module].default);
  110. }
  111. }); // ensures all modules are imported, then converts promise to the default export of the import
  112. Object.keys(this.modules).every(moduleKey => {
  113. const module = this.modules[moduleKey];
  114. module.setModuleManager(this);
  115. if (this.lockdown) return false;
  116. module._initialize();
  117. return true;
  118. });
  119. }
  120. /**
  121. * Called when a module is initialised
  122. *
  123. * @param {object} module - the module object/class
  124. */
  125. onInitialize(module) {
  126. if (this.modulesNotInitialized.indexOf(module) !== -1) {
  127. this.modulesNotInitialized.splice(this.modulesNotInitialized.indexOf(module), 1);
  128. this.log(
  129. "INFO",
  130. `Initialized: ${Object.keys(this.modules).length - this.modulesNotInitialized.length}/${
  131. Object.keys(this.modules).length
  132. }.`
  133. );
  134. if (this.modulesNotInitialized.length === 0) this.onAllModulesInitialized();
  135. }
  136. }
  137. /**
  138. * Called when a module fails to initialise
  139. *
  140. * @param {object} module - the module object/class
  141. */
  142. onFail(module) {
  143. if (this.modulesNotInitialized.indexOf(module) !== -1) {
  144. this.log("ERROR", "A module failed to initialize!");
  145. }
  146. }
  147. /**
  148. * Called when every module has initialised
  149. *
  150. */
  151. onAllModulesInitialized() {
  152. this.log("INFO", "All modules initialized!");
  153. }
  154. /**
  155. * Creates a new log message
  156. *
  157. * @param {...any} args - anything to be included in the log message, the first argument is the type of log
  158. */
  159. log(...args) {
  160. const _arguments = Array.from(args);
  161. const type = _arguments[0];
  162. _arguments.splice(0, 1);
  163. const start = `|${this.name.toUpperCase()}|`;
  164. const numberOfSpacesNeeded = 20 - start.length;
  165. _arguments.unshift(`${start}${Array(numberOfSpacesNeeded).join(" ")}`);
  166. if (type === "INFO") {
  167. _arguments[0] += "\x1b[36m";
  168. _arguments.push("\x1b[0m");
  169. console.log.apply(null, _arguments);
  170. } else if (type === "ERROR") {
  171. _arguments[0] += "\x1b[31m";
  172. _arguments.push("\x1b[0m");
  173. console.error.apply(null, _arguments);
  174. }
  175. }
  176. /**
  177. * Locks down all modules
  178. */
  179. _lockdown() {
  180. this.lockdown = true;
  181. Object.keys(this.modules).every(moduleKey => {
  182. const module = this.modules[moduleKey];
  183. module.setStatus("LOCKDOWN");
  184. return true;
  185. });
  186. }
  187. }
  188. const moduleManager = new ModuleManager();
  189. if (!config.get("migration")) {
  190. moduleManager.addModule("cache");
  191. moduleManager.addModule("db");
  192. moduleManager.addModule("mail");
  193. moduleManager.addModule("activities");
  194. moduleManager.addModule("api");
  195. moduleManager.addModule("app");
  196. moduleManager.addModule("ws");
  197. moduleManager.addModule("notifications");
  198. moduleManager.addModule("playlists");
  199. moduleManager.addModule("punishments");
  200. moduleManager.addModule("songs");
  201. moduleManager.addModule("stations");
  202. moduleManager.addModule("tasks");
  203. moduleManager.addModule("utils");
  204. moduleManager.addModule("youtube");
  205. } else {
  206. moduleManager.addModule("migration");
  207. }
  208. moduleManager.initialize();
  209. /**
  210. * Prints a job
  211. *
  212. * @param {object} job - the job
  213. * @param {number} layer - the layer
  214. */
  215. function printJob(job, layer) {
  216. const tabs = Array(layer).join("\t");
  217. if (job) {
  218. console.log(`${tabs}${job.name} (${job.toString()}) ${job.status}`);
  219. job.childJobs.forEach(childJob => {
  220. printJob(childJob, layer + 1);
  221. });
  222. } else console.log(`${tabs}JOB WAS REMOVED`);
  223. }
  224. /**
  225. * Prints a task
  226. *
  227. * @param {object} task - the task
  228. * @param {number} layer - the layer
  229. */
  230. function printTask(task, layer) {
  231. const tabs = Array(layer).join("\t");
  232. console.log(`${tabs}${task.job.name} (${task.job.toString()}) ${task.job.status} (priority: ${task.priority})`);
  233. task.job.childJobs.forEach(childJob => {
  234. printJob(childJob, layer + 1);
  235. });
  236. }
  237. process.stdin.on("data", data => {
  238. const command = data.toString().replace(/\r?\n|\r/g, "");
  239. if (command === "lockdown") {
  240. console.log("Locking down.");
  241. moduleManager._lockdown();
  242. }
  243. if (command === "status") {
  244. console.log("Status:");
  245. Object.keys(moduleManager.modules).forEach(moduleName => {
  246. const module = moduleManager.modules[moduleName];
  247. const tabsNeeded = 4 - Math.ceil((moduleName.length + 1) / 8);
  248. console.log(
  249. `${moduleName.toUpperCase()}${Array(tabsNeeded).join(
  250. "\t"
  251. )}${module.getStatus()}. Jobs in queue: ${module.jobQueue.lengthQueue()}. Jobs in progress: ${module.jobQueue.lengthRunning()}. Jobs paused: ${module.jobQueue.lengthPaused()} Concurrency: ${
  252. module.jobQueue.concurrency
  253. }. Stage: ${module.getStage()}`
  254. );
  255. });
  256. }
  257. if (command.startsWith("running")) {
  258. const parts = command.split(" ");
  259. moduleManager.modules[parts[1]].jobQueue.runningTasks.forEach(task => {
  260. printTask(task, 1);
  261. });
  262. }
  263. if (command.startsWith("queued")) {
  264. const parts = command.split(" ");
  265. moduleManager.modules[parts[1]].jobQueue.queue.forEach(task => {
  266. printTask(task, 1);
  267. });
  268. }
  269. if (command.startsWith("paused")) {
  270. const parts = command.split(" ");
  271. moduleManager.modules[parts[1]].jobQueue.pausedTasks.forEach(task => {
  272. printTask(task, 1);
  273. });
  274. }
  275. if (command.startsWith("stats")) {
  276. const parts = command.split(" ");
  277. console.log(moduleManager.modules[parts[1]].jobStatistics);
  278. }
  279. if (command.startsWith("jobinfo")) {
  280. const parts = command.split(" ");
  281. const uuid = parts[1];
  282. const jobFound = moduleManager.jobManager.getJob(uuid);
  283. if (jobFound) {
  284. let topParent = jobFound;
  285. let levelsDeep = 0;
  286. while (topParent.parentJob && topParent !== topParent.parentJob) {
  287. topParent = jobFound.parentJob;
  288. levelsDeep += 1;
  289. }
  290. console.log(
  291. `Found job, displaying that job and the full tree from the top parent job. The job is ${levelsDeep} levels deep from the top parent.`
  292. );
  293. console.log(jobFound);
  294. printJob(topParent, 1);
  295. } else console.log("Could not find job in job manager.");
  296. }
  297. if (command.startsWith("runjob")) {
  298. const parts = command.split(" ");
  299. const module = parts[1];
  300. const jobName = parts[2];
  301. const payload = JSON.parse(parts[3]);
  302. moduleManager.modules[module]
  303. .runJob(jobName, payload)
  304. .then(response => {
  305. console.log("runjob success", response);
  306. })
  307. .catch(err => {
  308. console.log("runjob error", err);
  309. });
  310. }
  311. if (command.startsWith("eval")) {
  312. const evalCommand = command.replace("eval ", "");
  313. console.log(`Running eval command: ${evalCommand}`);
  314. // eslint-disable-next-line no-eval
  315. const response = eval(evalCommand);
  316. console.log(`Eval response: `, response);
  317. }
  318. });
  319. export default moduleManager;