diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..c1a6f66 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "singleQuote": true, + "trailingComma": "es5" +} diff --git a/server/db/connect.js b/server/db/connect.js index d37c3e2..3283879 100644 --- a/server/db/connect.js +++ b/server/db/connect.js @@ -4,13 +4,32 @@ const mongoose = require('mongoose'); mongoose.set('debug', false); mongoose.Promise = global.Promise; -const MONGO_URL = process.env.MONGO_URL; -mongoose.connect(MONGO_URL || 'mongodb://localhost/data', { - useMongoClient: true -}); +const MONGO_URL = process.env.MONGO_URL || 'mongodb://localhost/data'; +const MONGO_OPT = { + useMongoClient: true, +}; + +async function connectMongo() { + try { + await mongoose.connect(MONGO_URL, MONGO_OPT); + } catch (err) { + console.log('Connect to database failed -> exiting'); + // k8s can restart the pod + process.exit(-1); + } +} +connectMongo().then(() => console.log('Database connected')); const db = mongoose.connection; db.on('error', (err) => console.log('Error connecting to database: ', err)); -db.on('disconnected', (err) => console.log('Disconnected from the database: ', err)); -db.on('close', (err) => console.log('Closed the connection to the database: ', err)); \ No newline at end of file +db.on('disconnected', async (err) => { + console.log('Disconnected from the database -> exiting: ', err); + // mongo/mongoose are stupid - can't retry normally. + // when I try to reconnect here, it fails with `MongooseError [OverwriteModelError]: Cannot overwrite `Place` model once compiled.` + // so it might be better to let k8s to restart the pod + process.exit(-2); +}); +db.on('close', (err) => + console.log('Closed the connection to the database: ', err) +); diff --git a/server/health-check/health-check.js b/server/health-check/health-check.js new file mode 100644 index 0000000..8616b8c --- /dev/null +++ b/server/health-check/health-check.js @@ -0,0 +1,29 @@ +const { getUserByEmail } = require('../service/userService'); + +const checkMongo = async () => { + await getUserByEmail('dummy@just-to-test.com'); +}; + +const checks = [checkMongo]; + +const healthCheck = async (req, res) => { + let ok = true; + const response = {}; + const performCheck = async (fn) => { + try { + await fn(); + response[fn.name] = 'ok'; + } catch (err) { + response[fn.name] = `${err}`; + ok = false; + } + }; + await Promise.all(checks.map((fn) => performCheck(fn))); + return res.status(ok ? 200 : 500).json(response); +}; + +const setupHealthCheck = (app) => { + app.use('/health-check', healthCheck); +}; + +module.exports = { setupHealthCheck }; diff --git a/server/index.js b/server/index.js index f4581ad..8551b9d 100644 --- a/server/index.js +++ b/server/index.js @@ -11,6 +11,7 @@ const cors = require('cors'); const { setupAttendenceApi } = require('./api/attendenceApi'); const exportMiddleware = require('./export/exportMiddleware'); +const { setupHealthCheck } = require('./health-check/health-check'); const argv = require('minimist')(process.argv.slice(2)); @@ -44,6 +45,7 @@ app.use('/graphql', graphqlHTTP((req) => ({ }))); setupAttendenceApi(app); +setupHealthCheck(app); app.use('/export/*', bodyParser.json()); app.post('/export/*', exportMiddleware);