diff --git a/README.md b/README.md index be0b3d7..ea62312 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # kth-node-monitor [![Build Status](https://travis-ci.org/jhsware/kth-node-monitor.svg?branch=master)](https://travis-ci.org/jhsware/kth-node-monitor) -Helper utilities for KTH/node-projects (temp location) +Helper utilities for KTH/node-projects +Some use cases to consider: - no answer from _monitor - slow response times diff --git a/lib/utilities.js b/lib/utilities.js index 5e03268..fd5fa28 100644 --- a/lib/utilities.js +++ b/lib/utilities.js @@ -35,6 +35,50 @@ function _setCache (key, statusObj) { function _getCache (key) { return apiCallCache[key] } + +// TODO: We should probably cache kth-node-api-client requests but need to consider how +// to construct the cache key properly (preferably a fully qualified URI) +createUtility({ + implements: IHealthCheck, + name: 'kth-node-api-client', + + status: function (client, key, endpointPath, options) { + if (endpointPath === undefined) { + // We couldn't resolve the endpoint + return Promise.resolve(_createApiStatusObj(key, 400, options && options.required)) + } else { + // We call enpoint using pathSegment + const t0 = Date.now() + + // There are two different types of api clients created by kth-node-api-call: + if (client.getAsync) { + // 1 -- Handle clients created with require('kth-node-api-call').BasicAPI + return client.getAsync({ + uri: endpointPath + }).then((data) => { + const statusObj = _createApiStatusObj(key, data.statusCode, options && options.required, Date.now() - t0) + return Promise.resolve(statusObj) + }) + .catch(() => { + const statusObj = _createApiStatusObj(key, 503, options && options.required) + return Promise.resolve(statusObj) + }) + } else { + // 2 -- Handle clients created with require('kth-node-api-call') + return client.promisedApiCall('FreeSeatsCall') + .then(function (data) { + const statusObj = _createApiStatusObj(key, 200, options && options.required, Date.now() - t0) + return Promise.resolve(statusObj) + }) + .catch(function (err) { + const statusObj = _createApiStatusObj(key, 503, options && options.required) + return Promise.resolve(statusObj) + }) + } + } + } +}).registerWith(registry) + createUtility({ implements: IHealthCheck, name: 'kth-node-api', diff --git a/package.json b/package.json index 62cd280..72c4007 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "//": "JSHint configuration - http://jshint.com/docs/options/", "name": "kth-node-monitor", - "version": "0.1.2", + "version": "0.1.3", "description": "Helper utilities for KTH/node-projects", "main": "lib/index.js", "author": {