utils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import async from "async";
  2. import { isAdminRequired, isLoginRequired, isOwnerRequired } 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.length(),
  22. jobsInProgress: module.jobQueue.running(),
  23. concurrency: module.jobQueue.concurrency
  24. };
  25. })
  26. );
  27. }
  28. ],
  29. async (err, modules) => {
  30. if (err && err !== true) {
  31. err = await utils.runJob("GET_ERROR", { error: err });
  32. console.log("ERROR", "GET_MODULES", `User ${session.userId} failed to get modules. '${err}'`);
  33. cb({ status: "failure", message: err });
  34. } else {
  35. console.log(
  36. "SUCCESS",
  37. "GET_MODULES",
  38. `User ${session.userId} has successfully got the modules info.`
  39. );
  40. cb({
  41. status: "success",
  42. message: "Successfully got modules.",
  43. modules
  44. });
  45. }
  46. }
  47. );
  48. }),
  49. getModule: isAdminRequired((session, moduleName, cb) => {
  50. async.waterfall(
  51. [
  52. next => {
  53. next(null, utils.moduleManager.modules[moduleName]);
  54. }
  55. ],
  56. async (err, module) => {
  57. const jobsInQueue = module.jobQueue._tasks.heap.map(task => task.data);
  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. runningJobs: module.runningJobs,
  73. jobStatistics: module.jobStatistics,
  74. jobsInQueue
  75. });
  76. }
  77. }
  78. );
  79. })
  80. };