Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix workflow run promise awaiting, so that workers exit cleanly #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions lib/temporal.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,30 @@ export class TemporalExplorer
@Inject(TEMPORAL_MODULE_OPTIONS_TOKEN) private options: TemporalModuleOptions;
private readonly logger = new Logger(TemporalExplorer.name);
private worker: Worker;
private timerId: ReturnType<typeof setInterval>;
private workerRunPromise: Promise<void>

constructor(
private readonly discoveryService: DiscoveryService,
private readonly metadataAccessor: TemporalMetadataAccessor,
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();
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per https://docs.nestjs.com/fundamentals/lifecycle-events, onModuleInit is called before onApplicationBootstrap, and no other code calls .explore in this repo, so I don't think it can be set at a later time with any of the publicly described interfaces, but I can reinstate this timer if it is needed for some reason


async explore() {
Expand Down