Skip to content

Commit 16c46b5

Browse files
committed
Refactor start and start:prod scripts to set NODE_ENV to production
1 parent b27f82c commit 16c46b5

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"prebuild": "cpr README.md dist/README.md && cpr LICENSE dist/LICENSE && cpr package.json dist/package.json",
1010
"build": "nest build",
1111
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
12-
"start": "nest start",
12+
"start": "NODE_ENV=production nest start",
1313
"start:dev": "nest start --watch",
1414
"start:debug": "nest start --debug 0.0.0.0 --watch",
15-
"start:prod": "node dist/main",
15+
"start:prod": "NODE_ENV=production node dist/main",
1616
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
1717
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
1818
"test": "jest",
@@ -85,4 +85,4 @@
8585
"pkg": {
8686
"assets": "package.json"
8787
}
88-
}
88+
}

src/backend-runner/backend-config.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class BackendConfigService implements OnModuleInit {
1717
return this._backendsConfigData;
1818
}
1919

20-
public constructor(private readonly config: ConfigService) {}
20+
public constructor(private readonly config: ConfigService) { }
2121

2222
public async onModuleInit() {
2323
await this.initialize();
@@ -51,7 +51,7 @@ export class BackendConfigService implements OnModuleInit {
5151
}
5252
}
5353

54-
this.logger.verbose(backendsConfigData);
54+
this.logger.verbose(JSON.stringify(backendsConfigData));
5555
this._backendsConfigData = backendsConfigData;
5656
}
5757
}

src/backend-runner/backend-runner.service.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ export class BackendRunnerService implements OnApplicationBootstrap, OnModuleIni
6969
this.redis.on('connecting', () => this.logger.verbose(`Redis connecting... 🟡`));
7070
this.redis.on('connect', () => this.logger.log(`Redis connected 🟢`));
7171
this.redis.on('ready', () => this.logger.debug(`Redis ready to listen jobs 🟣`));
72-
this.redis.on('close', () => this.logger.fatal(`Redis connection closed 🟥`));
72+
this.redis.on('close', () => {
73+
this.logger.fatal(`Redis connection closed 🟥`);
74+
75+
if (process.env.NODE_ENV === 'production') {
76+
this.logger.fatal(`Closing application...`);
77+
process.exit(1);
78+
}
79+
});
7380

7481
this.logger.log('OnModuleInit initialized 🔴');
7582
}
@@ -121,7 +128,11 @@ export class BackendRunnerService implements OnApplicationBootstrap, OnModuleIni
121128
worker.on('error', (err) => this.logger.error(err));
122129
worker.on('closed', () => {
123130
this.logger.fatal(`Worker closed 🟥`);
124-
process.exit(1);
131+
132+
if (process.env.NODE_ENV === 'production') {
133+
this.logger.fatal(`Closing application...`);
134+
process.exit(1);
135+
}
125136
});
126137

127138
await worker.run();

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare const module: any;
99
const app = await NestFactory.createApplicationContext(AppModule, {
1010
logger: getLogLevel(cfg?.application?.logLevel),
1111
});
12+
app.enableShutdownHooks();
1213
await app.init();
1314

1415
if (module.hot) {

0 commit comments

Comments
 (0)