@@ -4,7 +4,7 @@ const Joi = require('joi');
4
4
const tools = require ( '../tools' ) ;
5
5
const { successRes } = require ( '../schemas/response/general-schemas' ) ;
6
6
7
- module . exports = ( db , server ) => {
7
+ module . exports = ( db , server , loggelf ) => {
8
8
server . get (
9
9
{
10
10
path : '/health' ,
@@ -44,6 +44,10 @@ module.exports = (db, server) => {
44
44
} ) ;
45
45
}
46
46
} catch ( err ) {
47
+ loggelf ( {
48
+ short_message : '[HEALTH] MongoDb is down. MongoDb is not connected.'
49
+ } ) ;
50
+
47
51
res . status ( 500 ) ;
48
52
return res . json ( {
49
53
success : false ,
@@ -54,9 +58,14 @@ module.exports = (db, server) => {
54
58
// 2) test that mongoDb is writeable
55
59
56
60
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 } ) ;
59
63
} 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
+
60
69
res . status ( 500 ) ;
61
70
return res . json ( {
62
71
success : false ,
@@ -65,22 +74,36 @@ module.exports = (db, server) => {
65
74
}
66
75
67
76
// 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
+ }
77
90
78
91
// 4) test if redis is writeable
79
92
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 } ` ) ;
83
101
} 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
+
84
107
res . status ( 500 ) ;
85
108
return res . json ( {
86
109
success : false ,
0 commit comments