utils.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const UtilsModule = moduleManager.modules.utils;
  5. export default {
  6. getModules: isAdminRequired((session, cb) => {
  7. async.waterfall(
  8. [
  9. next => {
  10. next(null, UtilsModule.moduleManager.modules);
  11. },
  12. (modules, next) => {
  13. // console.log(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 });
  34. console.log("ERROR", "GET_MODULES", `User ${session.userId} failed to get modules. '${err}'`);
  35. cb({ status: "failure", message: err });
  36. } else {
  37. console.log(
  38. "SUCCESS",
  39. "GET_MODULES",
  40. `User ${session.userId} has successfully got the modules info.`
  41. );
  42. cb({
  43. status: "success",
  44. message: "Successfully got modules.",
  45. modules
  46. });
  47. }
  48. }
  49. );
  50. }),
  51. getModule: isAdminRequired((session, moduleName, cb) => {
  52. async.waterfall(
  53. [
  54. next => {
  55. next(null, UtilsModule.moduleManager.modules[moduleName]);
  56. }
  57. ],
  58. async (err, module) => {
  59. // console.log(module.runningJobs);
  60. if (err && err !== true) {
  61. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  62. console.log("ERROR", "GET_MODULE", `User ${session.userId} failed to get module. '${err}'`);
  63. cb({ status: "failure", message: err });
  64. } else {
  65. console.log(
  66. "SUCCESS",
  67. "GET_MODULE",
  68. `User ${session.userId} has successfully got the module info.`
  69. );
  70. cb({
  71. status: "success",
  72. message: "Successfully got module info.",
  73. // runningTasks: module.jobQueue.runningTasks,
  74. // pausedTasks: module.jobQueue.pausedTasks,
  75. // queuedTasks: module.jobQueue.queue,
  76. jobStatistics: module.jobStatistics
  77. });
  78. }
  79. }
  80. );
  81. })
  82. };