-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Wait for worker tasks to complete on app shutdown
- Loading branch information
1 parent
33b2fe1
commit 2a9fb0b
Showing
6 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { finalize, tap } from 'rxjs/operators'; | ||
|
||
import { WorkerMonitor } from './worker-monitor'; | ||
|
||
/** | ||
* This interceptor is used to keep track of open worker tasks, so that the WorkerModule | ||
* is not allowed to be destroyed while tasks are in progress. | ||
*/ | ||
@Injectable() | ||
export class MessageInterceptor implements NestInterceptor { | ||
constructor(private monitor: WorkerMonitor) {} | ||
|
||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
this.monitor.increment(); | ||
return next | ||
.handle() | ||
.pipe( | ||
finalize(() => { | ||
this.monitor.decrement(); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { debounceTime, takeWhile, tap } from 'rxjs/operators'; | ||
|
||
import { Logger } from '../config/logger/vendure-logger'; | ||
|
||
/** | ||
* This service is responsible for keeping track of incomplete worker tasks | ||
* to ensure that the WorkerModule is not destroyed before active tasks complete. | ||
*/ | ||
@Injectable() | ||
export class WorkerMonitor { | ||
openTasks = new BehaviorSubject<number>(0); | ||
get openTaskCount(): number { | ||
return this.openTasks.value; | ||
} | ||
increment() { | ||
this.openTasks.next(this.openTasks.value + 1); | ||
} | ||
decrement() { | ||
this.openTasks.next(this.openTasks.value - 1); | ||
} | ||
waitForOpenTasksToComplete(): Promise<number> { | ||
if (0 < this.openTaskCount) { | ||
Logger.info('Waiting for open worker tasks to complete...'); | ||
} | ||
return this.openTasks.asObservable().pipe( | ||
tap(count => { | ||
if (0 < count) { | ||
Logger.info(`${count} tasks open`); | ||
} | ||
}), | ||
debounceTime(100), | ||
takeWhile(value => value > 0), | ||
).toPromise(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { Module, OnApplicationShutdown, OnModuleDestroy } from '@nestjs/common'; | ||
import { APP_INTERCEPTOR } from '@nestjs/core'; | ||
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils'; | ||
|
||
import { getConfig } from '../config/config-helpers'; | ||
import { ConfigModule } from '../config/config.module'; | ||
import { Logger } from '../config/logger/vendure-logger'; | ||
import { PluginModule } from '../plugin/plugin.module'; | ||
import { ServiceModule } from '../service/service.module'; | ||
|
||
import { MessageInterceptor } from './message-interceptor'; | ||
import { WorkerMonitor } from './worker-monitor'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
ServiceModule.forWorker(), | ||
PluginModule.forWorker(), | ||
], | ||
providers: [ | ||
WorkerMonitor, | ||
{ | ||
provide: APP_INTERCEPTOR, | ||
useClass: MessageInterceptor, | ||
}, | ||
], | ||
controllers: getWorkerControllers(), | ||
}) | ||
export class WorkerModule {} | ||
export class WorkerModule implements OnModuleDestroy, OnApplicationShutdown { | ||
constructor(private monitor: WorkerMonitor) {} | ||
onModuleDestroy() { | ||
return this.monitor.waitForOpenTasksToComplete(); | ||
} | ||
|
||
onApplicationShutdown(signal?: string) { | ||
if (signal) { | ||
Logger.info('Worker Received shutdown signal:' + signal); | ||
} | ||
} | ||
} | ||
|
||
function getWorkerControllers() { | ||
const plugins = getConfig().plugins; | ||
return plugins | ||
.map(p => (p.defineWorkers ? p.defineWorkers() : undefined)) | ||
.filter(notNullOrUndefined) | ||
.reduce((flattened, controllers) => flattened.concat(controllers), []); | ||
} |