Added health check api endpoint

This commit is contained in:
Stefan Zermatten
2023-06-01 11:19:17 +02:00
parent 2b4ab6258d
commit 513c0f7148
4 changed files with 40 additions and 2 deletions

View File

@@ -1,3 +1,3 @@
import LibraryNodes from '/imports/api/library/LibraryNodes.js';
LibraryNodes.startCaching();
LibraryNodes.startCaching?.();

View File

@@ -0,0 +1,37 @@
// A simple endpoint that does a single round trip to the database to check everything is working
const HealthCheckCollection = new Mongo.Collection('healthCheck');
// Don't use redis oplog optimization on this collection, we want to hit the database every time
HealthCheckCollection.disableRedis?.();
const healthCheckDoc = {
status: 'ok',
};
// Add the health check doc on startup if it's missing
// There should only be this single doc in the collection
// A capped collection would be marginally faster, but it's a pain to make one in Meteor
Meteor.startup(function () {
if (!HealthCheckCollection.findOne()) {
HealthCheckCollection.insert(healthCheckDoc);
}
});
Meteor.method('api-status', function () {
let dbHealthDoc;
try {
dbHealthDoc = HealthCheckCollection.findOne();
} catch (e) {
this.setHttpStatusCode(503);
}
if (dbHealthDoc?.status === 'ok') {
this.setHttpStatusCode(200);
} else {
this.setHttpStatusCode(500);
}
return dbHealthDoc || {};
}, {
httpMethod: 'GET',
url: 'api/status'
});

View File

@@ -1 +1,2 @@
import './creature.js';
import './healthCheck.js';

2
app/package-lock.json generated
View File

@@ -1917,7 +1917,7 @@
"lodash.omit": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz",
"integrity": "sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA="
"integrity": "sha512-XeqSp49hNGmlkj2EJlfrQFIzQ6lXdNro9sddtQzcJY8QaoC2GO0DT7xaIokHeyM+mIT0mPMlPvkYzg2xCuHdZg=="
},
"lodash.template": {
"version": "4.5.0",