utils.js 2.2 KB

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