utils.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. const async = require("async");
  3. const hooks = require("./hooks");
  4. const utils = require("../utils");
  5. module.exports = {
  6. getModules: hooks.adminRequired((session, cb) => {
  7. async.waterfall(
  8. [
  9. (next) => {
  10. next(null, utils.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.length(),
  23. jobsInProgress: module.jobQueue.running(),
  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(
  34. "ERROR",
  35. "GET_MODULES",
  36. `User ${session.userId} failed to get modules. '${err}'`
  37. );
  38. cb({ status: "failure", message: err });
  39. } else {
  40. console.log(
  41. "SUCCESS",
  42. "GET_MODULES",
  43. `User ${session.userId} has successfully got the modules info.`
  44. );
  45. cb({
  46. status: "success",
  47. message: "Successfully got modules.",
  48. modules,
  49. });
  50. }
  51. }
  52. );
  53. }),
  54. getModule: hooks.adminRequired((session, moduleName, cb) => {
  55. async.waterfall(
  56. [
  57. (next) => {
  58. next(null, utils.moduleManager.modules[moduleName]);
  59. },
  60. ],
  61. async (err, module) => {
  62. const jobsInQueue = module.jobQueue._tasks.heap.map((task) => {
  63. return task.data;
  64. });
  65. // console.log(module.runningJobs);
  66. if (err && err !== true) {
  67. err = await utils.runJob("GET_ERROR", { error: err });
  68. console.log(
  69. "ERROR",
  70. "GET_MODULE",
  71. `User ${session.userId} failed to get module. '${err}'`
  72. );
  73. cb({ status: "failure", message: err });
  74. } else {
  75. console.log(
  76. "SUCCESS",
  77. "GET_MODULE",
  78. `User ${session.userId} has successfully got the module info.`
  79. );
  80. cb({
  81. status: "success",
  82. message: "Successfully got module info.",
  83. runningJobs: module.runningJobs,
  84. jobStatistics: module.jobStatistics,
  85. jobsInQueue,
  86. });
  87. }
  88. }
  89. );
  90. }),
  91. };