utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const UtilsModule = moduleManager.modules.utils;
  5. const WSModule = moduleManager.modules.ws;
  6. export default {
  7. getModules: isAdminRequired(function getModules(session, cb) {
  8. async.waterfall(
  9. [
  10. next => {
  11. next(null, UtilsModule.moduleManager.modules);
  12. },
  13. (modules, next) => {
  14. // this.log(modules, next);
  15. next(
  16. null,
  17. Object.keys(modules).map(moduleName => {
  18. const module = modules[moduleName];
  19. return {
  20. name: module.name,
  21. status: module.status,
  22. stage: module.stage,
  23. jobsInQueue: module.jobQueue.lengthQueue(),
  24. jobsInProgress: module.jobQueue.lengthRunning(),
  25. jobsPaused: module.jobQueue.lengthPaused(),
  26. concurrency: module.jobQueue.concurrency
  27. };
  28. })
  29. );
  30. }
  31. ],
  32. async (err, modules) => {
  33. if (err && err !== true) {
  34. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  35. this.log("ERROR", "GET_MODULES", `User ${session.userId} failed to get modules. '${err}'`);
  36. cb({ status: "error", message: err });
  37. } else {
  38. this.log("SUCCESS", "GET_MODULES", `User ${session.userId} has successfully got the modules info.`);
  39. cb({
  40. status: "success",
  41. message: "Successfully got modules.",
  42. data: { modules }
  43. });
  44. }
  45. }
  46. );
  47. }),
  48. getModule: isAdminRequired(function getModule(session, moduleName, cb) {
  49. async.waterfall(
  50. [
  51. next => {
  52. next(null, UtilsModule.moduleManager.modules[moduleName]);
  53. }
  54. ],
  55. async (err, module) => {
  56. // this.log(module.runningJobs);
  57. if (err && err !== true) {
  58. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  59. this.log("ERROR", "GET_MODULE", `User ${session.userId} failed to get module. '${err}'`);
  60. cb({ status: "error", message: err });
  61. } else {
  62. this.log("SUCCESS", "GET_MODULE", `User ${session.userId} has successfully got the module info.`);
  63. cb({
  64. status: "success",
  65. message: "Successfully got module info.",
  66. data: {
  67. // runningTasks: module.jobQueue.runningTasks,
  68. // pausedTasks: module.jobQueue.pausedTasks,
  69. // queuedTasks: module.jobQueue.queue,
  70. jobStatistics: module.jobStatistics
  71. }
  72. });
  73. }
  74. }
  75. );
  76. }),
  77. getRooms(session, cb) {
  78. WSModule.runJob("GET_ROOMS_FOR_SOCKET", { socketId: session.socketId })
  79. .then(response => {
  80. this.log("SUCCESS", "GET_ROOMS", `User ${session.userId} has successfully got the module info.`);
  81. cb({
  82. status: "success",
  83. message: "Successfully got rooms.",
  84. data: {
  85. rooms: response
  86. }
  87. });
  88. })
  89. .catch(async err => {
  90. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  91. this.log("ERROR", "GET_ROOMS", `Failed to get rooms. '${err}'`);
  92. cb({ status: "error", message: err });
  93. });
  94. }
  95. };