diff --git a/app/imports/server/config/redisCaching.js b/app/imports/server/config/redisCaching.js index 45132ac3..7ed21c26 100644 --- a/app/imports/server/config/redisCaching.js +++ b/app/imports/server/config/redisCaching.js @@ -1,3 +1,3 @@ import LibraryNodes from '/imports/api/library/LibraryNodes.js'; -LibraryNodes.startCaching(); +LibraryNodes.startCaching?.(); diff --git a/app/imports/server/rest/apiPublications/healthCheck.js b/app/imports/server/rest/apiPublications/healthCheck.js new file mode 100644 index 00000000..c6aa00a8 --- /dev/null +++ b/app/imports/server/rest/apiPublications/healthCheck.js @@ -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' +}); diff --git a/app/imports/server/rest/apiPublications/index.js b/app/imports/server/rest/apiPublications/index.js index 765d7988..e95d23db 100644 --- a/app/imports/server/rest/apiPublications/index.js +++ b/app/imports/server/rest/apiPublications/index.js @@ -1 +1,2 @@ import './creature.js'; +import './healthCheck.js'; diff --git a/app/package-lock.json b/app/package-lock.json index dee36e3a..9fe1b74e 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -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",