Merge branch '6.6.0' into feat/resolve-scoped-classes

This commit is contained in:
Kamil Mysliwiec
2019-08-26 12:38:15 +02:00
committed by GitHub
150 changed files with 3576 additions and 2475 deletions

View File

@@ -1,9 +1,9 @@
import {
INestApplicationContext,
Logger,
LoggerService,
ShutdownSignal,
} from '@nestjs/common';
import { Logger } from '@nestjs/common';
import { Abstract } from '@nestjs/common/interfaces';
import { Type } from '@nestjs/common/interfaces/type.interface';
import { isEmpty } from '@nestjs/common/utils/shared.utils';
@@ -12,6 +12,7 @@ import { UnknownModuleException } from './errors/exceptions/unknown-module.excep
import { createContextId } from './helpers';
import {
callAppShutdownHook,
callBeforeAppShutdownHook,
callModuleBootstrapHook,
callModuleDestroyHook,
callModuleInitHook,
@@ -23,6 +24,7 @@ import { Injector } from './injector/injector';
import { InstanceWrapper } from './injector/instance-wrapper';
import { Module } from './injector/module';
import { ModuleTokenFactory } from './injector/module-token-factory';
import { MESSAGES } from './constants';
export class NestApplicationContext implements INestApplicationContext {
protected isInitialized: boolean = false;
@@ -100,8 +102,16 @@ export class NestApplicationContext implements INestApplicationContext {
return this;
}
protected async dispose(): Promise<void> {
// Nest application context has no server
// to dispose, therefore just call a noop
return Promise.resolve();
}
public async close(): Promise<void> {
await this.callDestroyHook();
await this.callBeforeShutdownHook();
await this.dispose();
await this.callShutdownHook();
}
@@ -150,17 +160,27 @@ export class NestApplicationContext implements INestApplicationContext {
* @param {string[]} signals The system signals it should listen to
*/
protected listenToShutdownSignals(signals: string[]) {
const cleanup = async (signal: string) => {
try {
signals.forEach(sig => process.removeListener(sig, cleanup));
await this.callDestroyHook();
await this.callBeforeShutdownHook(signal);
await this.dispose();
await this.callShutdownHook(signal);
process.kill(process.pid, signal);
} catch (err) {
Logger.error(
MESSAGES.ERROR_DURING_SHUTDOWN,
(err as Error).stack,
NestApplicationContext.name,
);
process.exit(1);
}
};
signals.forEach((signal: string) => {
this.activeShutdownSignals.push(signal);
process.on(signal as any, async code => {
// Call the destroy and shutdown hook
// in case the process receives a shutdown signal
await this.callDestroyHook();
await this.callShutdownHook(signal);
process.exit(code || 1);
});
process.on(signal as any, cleanup);
});
}
@@ -208,6 +228,17 @@ export class NestApplicationContext implements INestApplicationContext {
}
}
/**
* Calls the `beforeApplicationShutdown` function on the registered
* modules and children.
*/
protected async callBeforeShutdownHook(signal?: string): Promise<void> {
const modulesContainer = this.container.getModules();
for (const module of [...modulesContainer.values()].reverse()) {
await callBeforeAppShutdownHook(module, signal);
}
}
protected find<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | Abstract<TInput> | string | symbol,
): TResult {