core.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const async = require("async");
  2. class DeferredPromise {
  3. constructor() {
  4. this.promise = new Promise((resolve, reject) => {
  5. this.reject = reject;
  6. this.resolve = resolve;
  7. });
  8. }
  9. }
  10. class CoreClass {
  11. constructor(name) {
  12. this.name = name;
  13. this.status = "UNINITIALIZED";
  14. // this.log("Core constructor");
  15. this.jobQueue = async.priorityQueue(
  16. (job, callback) => this._runJob(job, callback),
  17. 10 // How many jobs can run concurrently
  18. );
  19. this.jobQueue.pause();
  20. this.runningJobs = [];
  21. this.priorities = {};
  22. this.stage = 0;
  23. }
  24. setStatus(status) {
  25. this.status = status;
  26. this.log("INFO", `Status changed to: ${status}`);
  27. if (this.status === "READY") this.jobQueue.resume();
  28. else if (this.status === "FAIL" || this.status === "LOCKDOWN")
  29. this.jobQueue.pause();
  30. }
  31. getStatus() {
  32. return this.status;
  33. }
  34. setStage(stage) {
  35. this.stage = stage;
  36. }
  37. getStage() {
  38. return this.stage;
  39. }
  40. _initialize() {
  41. this.setStatus("INITIALIZING");
  42. this.initialize()
  43. .then(() => {
  44. this.setStatus("READY");
  45. this.moduleManager.onInitialize(this);
  46. })
  47. .catch((err) => {
  48. console.error(err);
  49. this.setStatus("FAILED");
  50. this.moduleManager.onFail(this);
  51. });
  52. }
  53. log() {
  54. let _arguments = Array.from(arguments);
  55. const type = _arguments[0];
  56. _arguments.splice(0, 1);
  57. const start = `|${this.name.toUpperCase()}|`;
  58. const numberOfTabsNeeded = 4 - Math.ceil(start.length / 8);
  59. _arguments.unshift(`${start}${Array(numberOfTabsNeeded).join("\t")}`);
  60. if (type === "INFO") {
  61. _arguments[0] = _arguments[0] + "\x1b[36m";
  62. _arguments.push("\x1b[0m");
  63. console.log.apply(null, _arguments);
  64. } else if (type === "ERROR") {
  65. _arguments[0] = _arguments[0] + "\x1b[31m";
  66. _arguments.push("\x1b[0m");
  67. console.error.apply(null, _arguments);
  68. }
  69. }
  70. runJob(name, payload, options = {}) {
  71. let deferredPromise = new DeferredPromise();
  72. const job = { name, payload, onFinish: deferredPromise };
  73. if (options.bypassQueue) {
  74. this._runJob(job, () => {});
  75. } else {
  76. const priority = this.priorities[name] ? this.priorities[name] : 10;
  77. this.jobQueue.push(job, priority);
  78. }
  79. return deferredPromise.promise;
  80. }
  81. setModuleManager(moduleManager) {
  82. this.moduleManager = moduleManager;
  83. }
  84. _runJob(job, cb) {
  85. this.log("INFO", `Running job ${job.name}`);
  86. this.runningJobs.push(job);
  87. this[job.name](job.payload)
  88. .then((response) => {
  89. this.log("INFO", `Ran job ${job.name} successfully`);
  90. job.onFinish.resolve(response);
  91. })
  92. .catch((error) => {
  93. this.log("INFO", `Running job ${job.name} failed`);
  94. job.onFinish.reject(error);
  95. })
  96. .finally(() => {
  97. this.runningJobs.splice(this.runningJobs.indexOf(job), 1);
  98. cb();
  99. });
  100. }
  101. }
  102. module.exports = CoreClass;