utils.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. // eslint-disable-next-line
  4. import moduleManager from "../../index";
  5. const UtilsModule = moduleManager.modules.utils;
  6. const WSModule = moduleManager.modules.ws;
  7. export default {
  8. getModules: isAdminRequired(function getModules(session, cb) {
  9. async.waterfall(
  10. [
  11. next => {
  12. next(null, UtilsModule.moduleManager.modules);
  13. },
  14. (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. if (err && err !== true) {
  57. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  58. this.log("ERROR", "GET_MODULE", `User ${session.userId} failed to get module. '${err}'`);
  59. cb({ status: "error", message: err });
  60. } else {
  61. this.log("SUCCESS", "GET_MODULE", `User ${session.userId} has successfully got the module info.`);
  62. cb({
  63. status: "success",
  64. message: "Successfully got module info.",
  65. data: {
  66. jobStatistics: module.jobStatistics
  67. }
  68. });
  69. }
  70. }
  71. );
  72. }),
  73. getRooms(session, cb) {
  74. WSModule.runJob("GET_ROOMS_FOR_SOCKET", { socketId: session.socketId })
  75. .then(response => {
  76. this.log("SUCCESS", "GET_ROOMS", `User ${session.userId} has successfully got the module info.`);
  77. cb({
  78. status: "success",
  79. message: "Successfully got rooms.",
  80. data: {
  81. rooms: response
  82. }
  83. });
  84. })
  85. .catch(async err => {
  86. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  87. this.log("ERROR", "GET_ROOMS", `Failed to get rooms. '${err}'`);
  88. cb({ status: "error", message: err });
  89. });
  90. }
  91. };