diff --git a/lib/temporal.explorer.ts b/lib/temporal.explorer.ts index 7aeda8e..028e28f 100644 --- a/lib/temporal.explorer.ts +++ b/lib/temporal.explorer.ts @@ -29,7 +29,7 @@ export class TemporalExplorer @Inject(TEMPORAL_MODULE_OPTIONS_TOKEN) private options: TemporalModuleOptions; private readonly logger = new Logger(TemporalExplorer.name); private worker: Worker; - private timerId: ReturnType; + private workerRunPromise: Promise constructor( private readonly discoveryService: DiscoveryService, @@ -37,32 +37,22 @@ export class TemporalExplorer private readonly metadataScanner: MetadataScanner, ) {} - clearInterval() { - this.timerId && clearInterval(this.timerId); - this.timerId = null; - } - async onModuleInit() { await this.explore(); } - onModuleDestroy() { + async onModuleDestroy() { try { this.worker?.shutdown(); + await this.workerRunPromise; + } catch (err: any) { this.logger.warn('Temporal worker was not cleanly shutdown.', { err }); } - - this.clearInterval(); } onApplicationBootstrap() { - this.timerId = setInterval(() => { - if (this.worker) { - this.worker.run(); - this.clearInterval(); - } - }, 1000); + this.workerRunPromise = this.worker?.run(); } async explore() {