From d247e0ba31811c37da39ea7e2b8706deb0d8d856 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Fri, 23 Jun 2017 11:14:33 -0700 Subject: [PATCH] added __heartbeat__ --- server/portal_server.js | 16 +++++++++++----- server/storage.js | 23 ++++++++++++++++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/server/portal_server.js b/server/portal_server.js index acf47b82..1cb8ab04 100644 --- a/server/portal_server.js +++ b/server/portal_server.js @@ -16,10 +16,13 @@ const log = mozlog('portal.server'); const app = express(); -app.engine('handlebars', exphbs({ - defaultLayout: 'main', - partialsDir: 'views/partials/' -})); +app.engine( + 'handlebars', + exphbs({ + defaultLayout: 'main', + partialsDir: 'views/partials/' + }) +); app.set('view engine', 'handlebars'); app.use(helmet()); @@ -27,7 +30,6 @@ app.use(busboy()); app.use(bodyParser.json()); app.use(express.static(path.join(__dirname, '../public'))); - app.get('/', (req, res) => { res.render('index', { shouldRenderAnalytics: notLocalHost, @@ -150,6 +152,10 @@ app.get('/__lbheartbeat__', (req, res) => { res.sendStatus(200); }); +app.get('/__heartbeat__', (req, res) => { + storage.ping().then(() => res.sendStatus(200), () => res.sendStatus(500)); +}); + app.listen(conf.listen_port, () => { log.info('startServer:', `Portal app listening on port ${conf.listen_port}!`); }); diff --git a/server/storage.js b/server/storage.js index 4acca3e6..62d14e4e 100644 --- a/server/storage.js +++ b/server/storage.js @@ -14,7 +14,8 @@ const log = mozlog('portal.storage'); const redis = require('redis'); const redis_client = redis.createClient({ - host: conf.redis_host + host: conf.redis_host, + connect_timeout: 10000 }); redis_client.on('error', err => { @@ -29,7 +30,8 @@ if (notLocalHost) { get: awsGet, set: awsSet, delete: awsDelete, - forceDelete: awsForceDelete + forceDelete: awsForceDelete, + ping: awsPing }; } else { module.exports = { @@ -39,7 +41,8 @@ if (notLocalHost) { get: localGet, set: localSet, delete: localDelete, - forceDelete: localForceDelete + forceDelete: localForceDelete, + ping: localPing }; } @@ -120,6 +123,14 @@ function localForceDelete(id) { }); } +function localPing() { + return new Promise((resolve, reject) => { + redis_client.ping(err => { + return err ? reject() : resolve(); + }); + }); +} + function awsLength(id) { const params = { Bucket: conf.s3_bucket, @@ -211,3 +222,9 @@ function awsForceDelete(id) { }); }); } + +function awsPing() { + return localPing().then(() => + s3.headBucket({ Bucket: conf.s3_bucket }).promise() + ); +}