@@ -3,27 +3,30 @@ const { db: { uri: databaseURI } } = require('./index');
33const logger = require ( '../helpers/logger' ) ;
44const { gracefulShutdown } = require ( '../helpers/util' ) ;
55
6- const conn = async function connection ( ) {
6+ async function connect ( ) {
77 logger . info ( 'connecting to database' ) ;
8- await mongoose . connect ( databaseURI ) ;
9- } ;
10-
11- try {
12- conn ( ) ;
13- } catch ( error ) {
14- logger . error ( error ) ;
8+ return mongoose . connect ( databaseURI ) ;
159}
1610
11+ let connection = null ;
12+
13+ ( async ( ) => {
14+ try {
15+ connection = await connect ( ) ;
16+ logger . info ( 'connected to database' ) ;
17+ } catch ( error ) {
18+ logger . error ( error ) ;
19+ }
20+ } ) ( ) ;
21+
1722// graceful shutdown for nodemon restarts
1823process . once ( 'SIGUSR2' , ( ) => {
19- gracefulShutdown ( conn , 'nodemon restart' , logger . info , ( ) => {
20- process . kill ( process . pid , 'SIGUSR2' ) ;
21- } ) ;
24+ gracefulShutdown ( connection , 'nodemon restart' , logger . info ) ;
25+ process . kill ( process . pid , 'SIGUSR2' ) ;
2226} ) ;
2327
2428// graceful shutdown for app termination
2529process . on ( 'SIGINT' , ( ) => {
26- gracefulShutdown ( conn , 'app termination' , logger . info , ( ) => {
27- process . exit ( 0 ) ;
28- } ) ;
30+ gracefulShutdown ( connection , 'app termination' , logger . info ) ;
31+ process . exit ( 0 ) ;
2932} ) ;
0 commit comments