ModuleManager.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. websocket: "WebSocketModule"
  38. };
  39. const { default: Module }: { default: ModuleClass<Modules[T]> } =
  40. await import(`./modules/${mapper[moduleName]}`);
  41. return new Module();
  42. }
  43. /**
  44. * loadModules - Load and initialize all modules
  45. *
  46. * @returns Promise
  47. */
  48. private async loadModules() {
  49. this.modules = {
  50. data: await this.loadModule("data"),
  51. events: await this.loadModule("events"),
  52. stations: await this.loadModule("stations"),
  53. websocket: await this.loadModule("websocket")
  54. };
  55. }
  56. /**
  57. * startup - Handle startup
  58. */
  59. public async startup() {
  60. await this.loadModules().catch(async err => {
  61. await this.shutdown();
  62. throw err;
  63. });
  64. if (!this.modules) throw new Error("No modules were loaded");
  65. await Promise.all(
  66. Object.values(this.modules).map(async module => {
  67. await module.startup().catch(async err => {
  68. module.setStatus(ModuleStatus.ERROR);
  69. throw err;
  70. });
  71. })
  72. ).catch(async err => {
  73. await this.shutdown();
  74. throw err;
  75. });
  76. JobQueue.getPrimaryInstance().resume();
  77. }
  78. /**
  79. * shutdown - Handle shutdown
  80. */
  81. public async shutdown() {
  82. if (this.modules)
  83. await Promise.all(
  84. Object.values(this.modules).map(async module => {
  85. if (
  86. [
  87. ModuleStatus.STARTED,
  88. ModuleStatus.STARTING,
  89. ModuleStatus.ERROR
  90. ].includes(module.getStatus())
  91. )
  92. await module.shutdown();
  93. })
  94. );
  95. }
  96. static getPrimaryInstance(): ModuleManager {
  97. return this.primaryInstance;
  98. }
  99. static setPrimaryInstance(instance: ModuleManager) {
  100. this.primaryInstance = instance;
  101. }
  102. }