main.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import * as readline from "node:readline";
  2. import mongoose from "mongoose";
  3. import ModuleManager from "@/ModuleManager";
  4. import LogBook from "@/LogBook";
  5. import JobQueue from "@/JobQueue";
  6. import JobStatistics from "@/JobStatistics";
  7. process.removeAllListeners("uncaughtException");
  8. process.on("uncaughtException", err => {
  9. if (err.name === "ECONNREFUSED" || err.name === "UNCERTAIN_STATE") return;
  10. LogBook.log({
  11. message: err.message,
  12. type: "error",
  13. category: "uncaught-exceptions",
  14. data: { error: err }
  15. });
  16. });
  17. ModuleManager.startup().then(async () => {
  18. const Model = await JobQueue.runJob("data", "getModel", { name: "news" });
  19. // console.log("Model", Model);
  20. const abcs = await Model.findOne({}).newest();
  21. console.log("Abcs", abcs);
  22. console.log(
  23. "getData",
  24. await Model.getData({
  25. page: 1,
  26. pageSize: 3,
  27. properties: [
  28. "title",
  29. "markdown",
  30. "status",
  31. "showToNewUsers",
  32. "createdBy"
  33. ],
  34. sort: {},
  35. queries: [
  36. {
  37. data: "v7",
  38. filter: { property: "title" },
  39. filterType: "contains"
  40. }
  41. ],
  42. operator: "and"
  43. })
  44. );
  45. // Model.create({
  46. // name: "Test name",
  47. // someNumbers: [1, 2, 3, 4],
  48. // songs: [],
  49. // aNumber: 941
  50. // });
  51. // Events schedule (was notifications)
  52. const now = Date.now();
  53. await JobQueue.runJob("events", "schedule", {
  54. channel: "test",
  55. time: 30000
  56. });
  57. await JobQueue.runJob("events", "subscribe", {
  58. channel: "test",
  59. type: "schedule",
  60. callback: async () => {
  61. console.log(`SCHEDULED: ${now} :: ${Date.now()}`);
  62. }
  63. });
  64. // Events (was cache pub/sub)
  65. await JobQueue.runJob("events", "subscribe", {
  66. channel: "test",
  67. callback: async value => {
  68. console.log(`PUBLISHED: ${value}`);
  69. }
  70. });
  71. await JobQueue.runJob("events", "publish", {
  72. channel: "test",
  73. value: "a value!"
  74. });
  75. });
  76. // TOOD remove, or put behind debug option
  77. // eslint-disable-next-line
  78. // @ts-ignore
  79. global.ModuleManager = ModuleManager;
  80. // eslint-disable-next-line
  81. // @ts-ignore
  82. global.JobQueue = JobQueue;
  83. // eslint-disable-next-line
  84. // @ts-ignore
  85. global.rs = () => {
  86. process.exit();
  87. };
  88. // setTimeout(async () => {
  89. // const start = Date.now();
  90. // const x = [];
  91. // while (x.length < 1) {
  92. // x.push(JobQueue.runJob("stations", "addC", {}).catch(() => {}));
  93. // }
  94. // const y = await Promise.all(x);
  95. // console.log(y);
  96. // // const a = await JobQueue.runJob("stations", "addC", {}).catch(() => {});
  97. // // console.log(555, a);
  98. // const difference = Date.now() - start;
  99. // console.log({ difference });
  100. // }, 100);
  101. const rl = readline.createInterface({
  102. input: process.stdin,
  103. output: process.stdout,
  104. completer: (command: string) => {
  105. const parts = command.split(" ");
  106. const commands = ["eval "];
  107. if (parts.length === 1) {
  108. const hits = commands.filter(c => c.startsWith(parts[0]));
  109. return [hits.length ? hits : commands, command];
  110. }
  111. return [];
  112. },
  113. removeHistoryDuplicates: true
  114. });
  115. const shutdown = async () => {
  116. if (rl) {
  117. rl.removeAllListeners();
  118. rl.close();
  119. }
  120. await ModuleManager.shutdown().catch(() => process.exit(1));
  121. process.exit(0);
  122. };
  123. process.on("SIGINT", shutdown);
  124. process.on("SIGQUIT", shutdown);
  125. process.on("SIGTERM", shutdown);
  126. const runCommand = (line: string) => {
  127. const [command, ...args] = line.split(" ");
  128. switch (command) {
  129. case "help": {
  130. console.log("Commands:");
  131. console.log("status - Show module manager and job queue status");
  132. console.log("stats - Shows jobs stats");
  133. console.log("queue - Shows a table of all jobs in the queue");
  134. console.log("active - Shows a table of all jobs currently running");
  135. console.log("jobinfo <jobId> - Print all info about a job");
  136. console.log("eval - Run a command");
  137. console.log("debug");
  138. console.log("log - Change LogBook settings");
  139. break;
  140. }
  141. case "status": {
  142. console.log("Module Manager Status:");
  143. console.table(ModuleManager.getStatus());
  144. console.log("Job Queue Status:");
  145. console.table(JobQueue.getStatus());
  146. break;
  147. }
  148. case "stats": {
  149. console.log("Job Queue Stats:");
  150. console.table(JobStatistics.getStats());
  151. break;
  152. }
  153. case "queue": {
  154. const queueStatus = JobQueue.getQueueStatus().queue;
  155. if (queueStatus.length === 0)
  156. console.log("There are no jobs in the queue.");
  157. else
  158. console.log(
  159. `There are ${queueStatus.length} jobs in the queue.`
  160. );
  161. console.table(queueStatus);
  162. break;
  163. }
  164. case "active": {
  165. const activeStatus = JobQueue.getQueueStatus().active;
  166. if (activeStatus.length === 0)
  167. console.log("There are no active jobs.");
  168. else console.log(`There are ${activeStatus.length} active jobs.`);
  169. console.table(activeStatus);
  170. break;
  171. }
  172. case "jobinfo": {
  173. if (args.length === 0) console.log("Please specify a jobId");
  174. else {
  175. const jobId = args[0];
  176. const job = JobQueue.getJob(jobId);
  177. if (!job) console.log(`Job "${jobId}" not found`);
  178. else {
  179. console.table(job.toJSON());
  180. }
  181. }
  182. break;
  183. }
  184. case "eval": {
  185. const evalCommand = args.join(" ");
  186. console.log(`Running eval command: ${evalCommand}`);
  187. // eslint-disable-next-line no-eval
  188. const response = eval(evalCommand);
  189. console.log(`Eval response: `, response);
  190. break;
  191. }
  192. case "debug": {
  193. // eslint-disable-next-line no-debugger
  194. debugger;
  195. break;
  196. }
  197. case "log": {
  198. const [output, key, action, ...values] = args;
  199. if (
  200. output === undefined ||
  201. key === undefined ||
  202. action === undefined
  203. ) {
  204. console.log(
  205. `Missing required parameters (log <output> <key> <action> [values])`
  206. );
  207. break;
  208. }
  209. let value: any[] | undefined;
  210. if (values !== undefined && values.length >= 1) {
  211. value = values.map(_filter => JSON.parse(_filter));
  212. if (value.length === 1) [value] = value;
  213. }
  214. LogBook
  215. // eslint-disable-next-line
  216. // @ts-ignore
  217. .updateOutput(output, key, action, value)
  218. .then(() => console.log("Successfully updated outputs"))
  219. .catch((err: Error) =>
  220. console.log(`Error updating outputs "${err.message}"`)
  221. );
  222. break;
  223. }
  224. case "getjobs": {
  225. console.log(ModuleManager.getJobs());
  226. break;
  227. }
  228. default: {
  229. if (!/^\s*$/.test(command))
  230. console.log(`Command "${command}" not found`);
  231. }
  232. }
  233. };
  234. rl.on("line", runCommand);