main.ts 5.4 KB

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