utils.js 2.7 KB

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