@@ -4,7 +4,7 @@ const Joi = require('joi');
44const tools = require ( '../tools' ) ;
55const { successRes } = require ( '../schemas/response/general-schemas' ) ;
66
7- module . exports = ( db , server ) => {
7+ module . exports = ( db , server , loggelf ) => {
88 server . get (
99 {
1010 path : '/health' ,
@@ -44,6 +44,10 @@ module.exports = (db, server) => {
4444 } ) ;
4545 }
4646 } catch ( err ) {
47+ loggelf ( {
48+ short_message : '[HEALTH] MongoDb is down. MongoDb is not connected.'
49+ } ) ;
50+
4751 res . status ( 500 ) ;
4852 return res . json ( {
4953 success : false ,
@@ -54,9 +58,14 @@ module.exports = (db, server) => {
5458 // 2) test that mongoDb is writeable
5559
5660 try {
57- await db . database . collection ( `${ currentTimestamp } ` ) . insert ( { a : 'testWrite' } ) ;
58- await db . database . collection ( ` ${ currentTimestamp } ` ) . deleteOne ( { a : 'testWrite' } ) ;
61+ const insertData = await db . database . collection ( 'health' ) . insertOne ( { [ `${ currentTimestamp } ` ] : 'testWrite' } ) ;
62+ await db . database . collection ( 'health' ) . deleteOne ( { _id : insertData . insertedId } ) ;
5963 } catch ( err ) {
64+ loggelf ( {
65+ short_message :
66+ '[HEALTH] could not write to MongoDb. MongoDB is not writeable, cannot write document to collection `health` and delete the document at that path.'
67+ } ) ;
68+
6069 res . status ( 500 ) ;
6170 return res . json ( {
6271 success : false ,
@@ -65,22 +74,36 @@ module.exports = (db, server) => {
6574 }
6675
6776 // 3) test redis PING
68- db . redis . ping ( err => {
69- if ( err ) {
70- res . status ( 500 ) ;
71- return res . json ( {
72- success : false ,
73- message : 'Redis is down'
74- } ) ;
75- }
76- } ) ;
77+ try {
78+ await db . redis . ping ( ) ;
79+ } catch ( err ) {
80+ loggelf ( {
81+ short_message : '[HEALTH] Redis is down. PING to Redis failed.'
82+ } ) ;
83+
84+ res . status ( 500 ) ;
85+ return res . json ( {
86+ success : false ,
87+ message : 'Redis is down'
88+ } ) ;
89+ }
7790
7891 // 4) test if redis is writeable
7992 try {
80- await db . redis . set ( `${ currentTimestamp } ` , 'testVal' ) ;
81- await db . redis . get ( `${ currentTimestamp } ` ) ;
82- await db . redis . del ( `${ currentTimestamp } ` ) ;
93+ await db . redis . hset ( 'health' , `${ currentTimestamp } ` , `${ currentTimestamp } ` ) ;
94+ const data = await db . redis . hget ( `health` , `${ currentTimestamp } ` ) ;
95+
96+ if ( data !== `${ currentTimestamp } ` ) {
97+ throw Error ( 'Received data is not the same!' ) ;
98+ }
99+
100+ await db . redis . hdel ( 'health' , `${ currentTimestamp } ` ) ;
83101 } catch ( err ) {
102+ loggelf ( {
103+ short_message :
104+ '[HEALTH] Redis is not writeable/readable. Could not set hashkey `health` in redis, failed to get the key and/or delete the key.'
105+ } ) ;
106+
84107 res . status ( 500 ) ;
85108 return res . json ( {
86109 success : false ,
0 commit comments