utils.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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(function getModules(session, cb) {
  7. async.waterfall(
  8. [
  9. next => {
  10. next(null, UtilsModule.moduleManager.modules);
  11. },
  12. (modules, next) => {
  13. // this.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 }, this);
  34. this.log("ERROR", "GET_MODULES", `User ${session.userId} failed to get modules. '${err}'`);
  35. cb({ status: "failure", 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. 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. // this.log(module.runningJobs);
  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: "failure", 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. // runningTasks: module.jobQueue.runningTasks,
  66. // pausedTasks: module.jobQueue.pausedTasks,
  67. // queuedTasks: module.jobQueue.queue,
  68. jobStatistics: module.jobStatistics
  69. });
  70. }
  71. }
  72. );
  73. })
  74. };