main.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import * as readline from "node:readline";
  2. import ModuleManager from "./ModuleManager";
  3. import LogBook from "./LogBook";
  4. const logBook = new LogBook();
  5. const moduleManager = new ModuleManager(logBook);
  6. moduleManager.startup();
  7. // TOOD remove, or put behind debug option
  8. // eslint-disable-next-line
  9. // @ts-ignore
  10. global.moduleManager = moduleManager;
  11. // eslint-disable-next-line
  12. // @ts-ignore
  13. global.rs = () => {
  14. process.exit();
  15. };
  16. const interval = setInterval(() => {
  17. moduleManager
  18. .runJob("stations", "addToQueue", { songId: "TestId" })
  19. .catch(() => {});
  20. moduleManager
  21. .runJob("stations", "addA", void 0, { priority: 5 })
  22. .catch(() => {});
  23. moduleManager
  24. .runJob("others", "doThing", { test: "Test", test2: 123 })
  25. .catch(() => {});
  26. }, 40);
  27. setTimeout(() => {
  28. clearTimeout(interval);
  29. }, 3000);
  30. process.on("uncaughtException", err => {
  31. if (err.name === "ECONNREFUSED" || err.name === "UNCERTAIN_STATE") return;
  32. console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
  33. });
  34. const shutdown = async () => {
  35. await moduleManager.shutdown().catch(() => process.exit(1));
  36. process.exit(0);
  37. };
  38. process.on("SIGINT", shutdown);
  39. process.on("SIGQUIT", shutdown);
  40. process.on("SIGTERM", shutdown);
  41. // const shutdown = () => {
  42. // moduleManager
  43. // .shutdown()
  44. // .then(() => process.exit(0))
  45. // .catch(() => process.exit(1));
  46. // };
  47. // process.on("SIGINT", shutdown);
  48. // process.on("SIGQUIT", shutdown);
  49. // process.on("SIGTERM", shutdown);
  50. // process.on("SIGUSR2", shutdown);
  51. const runCommand = (line: string) => {
  52. const [command, ...args] = line.split(" ");
  53. switch (command) {
  54. case "help": {
  55. console.log("Commands:");
  56. console.log("status");
  57. console.log("stats");
  58. console.log("queue");
  59. console.log("active");
  60. console.log("eval");
  61. console.log("debug");
  62. console.log("log");
  63. break;
  64. }
  65. case "status": {
  66. console.log("Module Manager Status:");
  67. console.table(moduleManager.getStatus());
  68. console.log("Job Queue Status:");
  69. console.table(moduleManager.getJobsStatus());
  70. break;
  71. }
  72. case "stats": {
  73. console.log("Job Queue Stats:");
  74. console.table(moduleManager.getJobsStats());
  75. break;
  76. }
  77. case "queue": {
  78. const queueStatus = moduleManager.getQueueStatus().queue;
  79. if (queueStatus.length === 0)
  80. console.log("There are no jobs in the queue.");
  81. else
  82. console.log(
  83. `There are ${queueStatus.length} jobs in the queue.`
  84. );
  85. console.table(queueStatus);
  86. break;
  87. }
  88. case "active": {
  89. const activeStatus = moduleManager.getQueueStatus().active;
  90. if (activeStatus.length === 0)
  91. console.log("There are no active jobs.");
  92. else console.log(`There are ${activeStatus.length} active jobs.`);
  93. console.table(activeStatus);
  94. break;
  95. }
  96. case "eval": {
  97. const evalCommand = args.join(" ");
  98. console.log(`Running eval command: ${evalCommand}`);
  99. // eslint-disable-next-line no-eval
  100. const response = eval(evalCommand);
  101. console.log(`Eval response: `, response);
  102. break;
  103. }
  104. case "debug": {
  105. // eslint-disable-next-line no-debugger
  106. debugger;
  107. break;
  108. }
  109. case "log": {
  110. const [output, key, action, ...values] = args;
  111. if (
  112. output === undefined ||
  113. key === undefined ||
  114. action === undefined
  115. ) {
  116. console.log(
  117. `Missing required parameters (log <output> <key> <action> [values])`
  118. );
  119. break;
  120. }
  121. let value: any[] | undefined;
  122. if (values !== undefined && values.length >= 1) {
  123. value = values.map(_filter => JSON.parse(_filter));
  124. if (value.length === 1) [value] = value;
  125. }
  126. logBook
  127. // @ts-ignore
  128. .updateOutput(output, key, action, value)
  129. .then(() => console.log("Successfully updated outputs"))
  130. .catch((err: Error) =>
  131. console.log(`Error updating outputs "${err.message}"`)
  132. );
  133. break;
  134. }
  135. default: {
  136. if (!/^\s*$/.test(command))
  137. console.log(`Command "${command}" not found`);
  138. }
  139. }
  140. };
  141. const rl = readline.createInterface({
  142. input: process.stdin,
  143. output: process.stdout,
  144. completer: (command: string) => {
  145. const parts = command.split(" ");
  146. const commands = ["eval "];
  147. if (parts.length === 1) {
  148. const hits = commands.filter(c => c.startsWith(parts[0]));
  149. return [hits.length ? hits : commands, command];
  150. }
  151. return [];
  152. }
  153. });
  154. rl.on("line", runCommand);