Skip to content

Commit

Permalink
Added utility to check endpoint accessed with a kth-node-api-client
Browse files Browse the repository at this point in the history
  • Loading branch information
jhsware committed Dec 6, 2016
1 parent e2206d9 commit 108318b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion 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
Expand Down
44 changes: 44 additions & 0 deletions lib/utilities.js
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion 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": {
Expand Down

0 comments on commit 108318b

Please sign in to comment.