ModuleManager.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { ModuleStatus } from "./BaseModule";
  2. import JobQueue from "./JobQueue";
  3. import { Modules, ModuleClass } from "./types/Modules";
  4. export default class ModuleManager {
  5. static primaryInstance = new this();
  6. private modules?: Modules;
  7. /**
  8. * getStatus - Get status of modules
  9. *
  10. * @returns Module statuses
  11. */
  12. public getStatus() {
  13. const status: Record<string, ModuleStatus> = {};
  14. Object.entries(this.modules || {}).forEach(([name, module]) => {
  15. status[name] = module.getStatus();
  16. });
  17. return status;
  18. }
  19. /**
  20. * Gets a module
  21. *
  22. */
  23. public getModule(moduleName: keyof Modules) {
  24. return this.modules && this.modules[moduleName];
  25. }
  26. /**
  27. * loadModule - Load and initialize module
  28. *
  29. * @param moduleName - Name of the module
  30. * @returns Module
  31. */
  32. private async loadModule<T extends keyof Modules>(moduleName: T) {
  33. const mapper = {
  34. data: "DataModule",
  35. // events: "EventsModule",
  36. stations: "StationModule"
  37. };
  38. const { default: Module }: { default: ModuleClass<Modules[T]> } =
  39. await import(`./modules/${mapper[moduleName]}`);
  40. return new Module();
  41. }
  42. /**
  43. * loadModules - Load and initialize all modules
  44. *
  45. * @returns Promise
  46. */
  47. private async loadModules() {
  48. this.modules = {
  49. data: await this.loadModule("data"),
  50. // events: await this.loadModule("events"),
  51. stations: await this.loadModule("stations")
  52. };
  53. }
  54. /**
  55. * startup - Handle startup
  56. */
  57. public async startup() {
  58. await this.loadModules().catch(async err => {
  59. await this.shutdown();
  60. throw err;
  61. });
  62. if (!this.modules) throw new Error("No modules were loaded");
  63. await Promise.all(
  64. Object.values(this.modules).map(async module => {
  65. await module.startup().catch(async err => {
  66. module.setStatus(ModuleStatus.ERROR);
  67. throw err;
  68. });
  69. })
  70. ).catch(async err => {
  71. await this.shutdown();
  72. throw err;
  73. });
  74. JobQueue.getPrimaryInstance().resume();
  75. }
  76. /**
  77. * shutdown - Handle shutdown
  78. */
  79. public async shutdown() {
  80. if (this.modules)
  81. await Promise.all(
  82. Object.values(this.modules).map(async module => {
  83. if (
  84. [
  85. ModuleStatus.STARTED,
  86. ModuleStatus.STARTING,
  87. ModuleStatus.ERROR
  88. ].includes(module.getStatus())
  89. )
  90. await module.shutdown();
  91. })
  92. );
  93. }
  94. static getPrimaryInstance(): ModuleManager {
  95. return this.primaryInstance;
  96. }
  97. static setPrimaryInstance(instance: ModuleManager) {
  98. this.primaryInstance = instance;
  99. }
  100. }