Skip to content

Commit 224cbf8

Browse files
authored
Merge pull request #110 from PaystackHQ/chore/slight-error-handling
Chore: Graceful shutdown, return errors
2 parents 0a02ed2 + d8527ef commit 224cbf8

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

config/db.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@ const { db: { uri: databaseURI } } = require('./index');
33
const logger = require('../helpers/logger');
44
const { 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
1823
process.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
2529
process.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
});

controllers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module.exports = {
7777
slack.sendMonitorMessage(stringError);
7878
return res.status(500).send({
7979
message: 'Trigger is broken, check Slack',
80+
error,
8081
});
8182
}
8283
},

helpers/util.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ function sleep(durationInMs) {
5353
* @param {string} msg
5454
* @param {string} cb
5555
*/
56-
function gracefulShutdown(conn, msg, infoLogger, cb) {
57-
conn.close(() => {
58-
infoLogger(`Mongoose disconnected through ${msg}`);
59-
cb();
60-
});
56+
function gracefulShutdown(conn, msg, infoLogger) {
57+
conn.disconnect();
58+
infoLogger(`Mongoose disconnected through ${msg}`);
6159
}
6260

6361
/**

0 commit comments

Comments
 (0)