Browse Source

refactor(core): Move config reset to module rather than service

Michael Bromley 3 years ago
parent
commit
59dfc50181

+ 9 - 0
packages/core/src/config/config.module.ts

@@ -6,6 +6,7 @@ import { ConfigurableOperationDef } from '../common/configurable-operation';
 import { Injector } from '../common/injector';
 import { InjectableStrategy } from '../common/types/injectable-strategy';
 
+import { resetConfig } from './config-helpers';
 import { ConfigService } from './config.service';
 
 @Module({
@@ -23,6 +24,14 @@ export class ConfigModule implements OnApplicationBootstrap, OnApplicationShutdo
     async onApplicationShutdown(signal?: string) {
         await this.destroyInjectableStrategies();
         await this.destroyConfigurableOperations();
+        /**
+         * When the application shuts down, we reset the activeConfig to the default. Usually this is
+         * redundant, as the app shutdown would normally coincide with the process ending. However, in some
+         * circumstances, such as when running migrations immediately followed by app bootstrap, the activeConfig
+         * will persist between these two applications and mutations e.g. to the CustomFields will result in
+         * hard-to-debug errors. So resetting is a precaution against this scenario.
+         */
+        resetConfig();
     }
 
     private async initInjectableStrategies() {

+ 3 - 14
packages/core/src/config/config.service.ts

@@ -1,8 +1,8 @@
-import { DynamicModule, Injectable, OnApplicationShutdown, Type } from '@nestjs/common';
+import { DynamicModule, Injectable, Type } from '@nestjs/common';
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { ConnectionOptions } from 'typeorm';
 
-import { getConfig, resetConfig } from './config-helpers';
+import { getConfig } from './config-helpers';
 import { CustomFields } from './custom-field/custom-field-types';
 import { EntityIdStrategy } from './entity-id-strategy/entity-id-strategy';
 import { Logger, VendureLogger } from './logger/vendure-logger';
@@ -25,7 +25,7 @@ import {
 } from './vendure-config';
 
 @Injectable()
-export class ConfigService implements VendureConfig, OnApplicationShutdown {
+export class ConfigService implements VendureConfig {
     private activeConfig: RuntimeVendureConfig;
 
     constructor() {
@@ -36,17 +36,6 @@ export class ConfigService implements VendureConfig, OnApplicationShutdown {
         }
     }
 
-    /**
-     * When the application shuts down, we reset the activeConfig to the default. Usually this is
-     * redundant, as the app shutdown would normally coincide with the process ending. However, in some
-     * circumstances, such as when running migrations immediately followed by app bootstrap, the activeConfig
-     * will persist between these two applications and mutations e.g. to the CustomFields will result in
-     * hard-to-debug errors. So resetting is a precaution against this scenario.
-     */
-    onApplicationShutdown(signal?: string): any {
-        resetConfig();
-    }
-
     get apiOptions(): Required<ApiOptions> {
         return this.activeConfig.apiOptions;
     }