Skip to content

Commit

Permalink
fixes and add graylog logging
Browse files Browse the repository at this point in the history
  • Loading branch information
NickOvt committed Jan 22, 2024
1 parent 511b673 commit 7f4b05e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ module.exports = done => {
certsRoutes(db, server);
webhooksRoutes(db, server);
settingsRoutes(db, server, settingsHandler);
healthRoutes(db, server);
healthRoutes(db, server, loggelf);

if (process.env.NODE_ENV === 'test') {
server.get(
Expand Down
53 changes: 38 additions & 15 deletions lib/api/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Joi = require('joi');
const tools = require('../tools');
const { successRes } = require('../schemas/response/general-schemas');

module.exports = (db, server) => {
module.exports = (db, server, loggelf) => {
server.get(
{
path: '/health',
Expand Down Expand Up @@ -44,6 +44,10 @@ module.exports = (db, server) => {
});
}
} catch (err) {
loggelf({
short_message: '[HEALTH] MongoDb is down. MongoDb is not connected.'
});

res.status(500);
return res.json({
success: false,
Expand All @@ -54,9 +58,14 @@ module.exports = (db, server) => {
// 2) test that mongoDb is writeable

try {
await db.database.collection(`${currentTimestamp}`).insert({ a: 'testWrite' });
await db.database.collection(`${currentTimestamp}`).deleteOne({ a: 'testWrite' });
const insertData = await db.database.collection('health').insertOne({ [`${currentTimestamp}`]: 'testWrite' });
await db.database.collection('health').deleteOne({ _id: insertData.insertedId });
} catch (err) {
loggelf({
short_message:
'[HEALTH] could not write to MongoDb. MongoDB is not writeable, cannot write document to collection `health` and delete the document at that path.'
});

res.status(500);
return res.json({
success: false,
Expand All @@ -65,22 +74,36 @@ module.exports = (db, server) => {
}

// 3) test redis PING
db.redis.ping(err => {
if (err) {
res.status(500);
return res.json({
success: false,
message: 'Redis is down'
});
}
});
try {
await db.redis.ping();
} catch (err) {
loggelf({
short_message: '[HEALTH] Redis is down. PING to Redis failed.'
});

res.status(500);
return res.json({
success: false,
message: 'Redis is down'
});
}

// 4) test if redis is writeable
try {
await db.redis.set(`${currentTimestamp}`, 'testVal');
await db.redis.get(`${currentTimestamp}`);
await db.redis.del(`${currentTimestamp}`);
await db.redis.hset('health', `${currentTimestamp}`, `${currentTimestamp}`);
const data = await db.redis.hget(`health`, `${currentTimestamp}`);

if (data !== `${currentTimestamp}`) {
throw Error('Received data is not the same!');
}

await db.redis.hdel('health', `${currentTimestamp}`);
} catch (err) {
loggelf({
short_message:
'[HEALTH] Redis is not writeable/readable. Could not set hashkey `health` in redis, failed to get the key and/or delete the key.'
});

res.status(500);
return res.json({
success: false,
Expand Down

0 comments on commit 7f4b05e

Please sign in to comment.