main.ts 5.3 KB

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