Total Complexity | 9 |
Complexity/F | 1.8 |
Lines of Code | 53 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | "use strict"; |
||
2 | |||
3 | if (['dev', 'prod'].indexOf(process.env.NODE_ENV) < 0) { |
||
4 | console.warn(`Undefined node environment "${process.env.NODE_ENV}", fallback to dev !`); |
||
5 | process.env.NODE_ENV = 'dev'; |
||
6 | } |
||
7 | |||
8 | const onDeath = require('death')({ |
||
9 | SIGTERM: false, |
||
10 | SIGQUIT: false |
||
11 | }); |
||
12 | |||
13 | const logger = require('./lib/logger'); |
||
14 | const taskLoggerFactory = require('./lib/logger/taskLogger'); |
||
15 | const server = require('./lib/server'); |
||
16 | const pkg = require('./package.json'); |
||
17 | const NestedError = require('nested-error-stacks'); |
||
18 | |||
19 | const appName = pkg.name; |
||
20 | const taskLogger = taskLoggerFactory(appName); |
||
21 | |||
22 | onDeath(signal => { |
||
23 | let exitCode = 0; |
||
24 | if (signal === 'SIGTERM' || signal === 'SIGQUIT') { |
||
25 | exitCode = 1; |
||
26 | } |
||
27 | cleanAndExit(exitCode); |
||
28 | }); |
||
29 | |||
30 | const cleanAndExit = (exitCode = 0) => { |
||
31 | taskLogger.stopping(); |
||
32 | const _exit = error => { |
||
33 | if (error instanceof Error) { |
||
34 | console.error(error); |
||
35 | } |
||
36 | taskLogger.stopped(); |
||
37 | process.exit(exitCode); |
||
38 | }; |
||
39 | |||
40 | return server.stop() |
||
41 | .then(_exit, _exit); |
||
42 | }; |
||
43 | |||
44 | taskLogger.starting(); |
||
45 | |||
46 | server.start() |
||
47 | .then(() => taskLogger.started()) |
||
48 | .catch(error => { |
||
49 | const newError = new NestedError(`Exit ${appName} after an error at initialisation`, error); |
||
50 | logger.error(newError.stack); |
||
51 | |||
52 | return Promise.reject(cleanAndExit(1)); |
||
53 | }); |
||
54 |