1 | 'use strict';
|
2 |
|
3 | var toobusy = require('toobusy-js');
|
4 | var debug = require('debug')('gateway:healthcheck');
|
5 | var portastic = require('portastic')
|
6 |
|
7 | const HEALTHCHECK_URL = '/healthcheck';
|
8 |
|
9 | module.exports.init = function(config, logger, stats) {
|
10 | return {
|
11 | onrequest: function(req, res, next) {
|
12 | var healthcheck_url = config['healthcheck_url'] || HEALTHCHECK_URL
|
13 | if(healthcheck_url == req.url) {
|
14 | var statusCode = (toobusy() ? 503 : 200)
|
15 | debug(statusCode)
|
16 | var healthInfo = {
|
17 | memoryUsage: process.memoryUsage(),
|
18 | cpuUsage: process.cpuUsage(),
|
19 | uptime: process.uptime(),
|
20 | pid: process.pid
|
21 | }
|
22 |
|
23 | if(req.targetPort != '' && process.env.EDGEMICRO_DECORATOR){
|
24 | var port = req.targetPort
|
25 | portastic.test(port)
|
26 | .then(function(isOpen){
|
27 | if (isOpen){
|
28 | statusCode = 500
|
29 | var errorDescription = 'Application is not running on specified applicaiton port: ' + port
|
30 | healthInfo.decoratorError = errorDescription
|
31 | debug(errorDescription)
|
32 | debug(statusCode)
|
33 | }
|
34 | res.writeHead(statusCode, { 'Content-Type': 'application/json' })
|
35 | res.write(JSON.stringify(healthInfo))
|
36 | res.end()
|
37 | });
|
38 | }
|
39 | else{
|
40 | res.writeHead(statusCode, { 'Content-Type': 'application/json' })
|
41 | res.write(JSON.stringify(healthInfo))
|
42 | res.end()
|
43 | }
|
44 | }
|
45 | else {
|
46 | next()
|
47 | }
|
48 | }
|
49 | }
|
50 | }
|