All files / mesos-framework/lib taskHealthHelper.js

100% Statements 88/88
100% Branches 61/61
100% Functions 15/15
100% Lines 88/88
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166    1x                 29x 1x     28x   28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x   28x 27x   1x     27x 1x                 27x   2x   2x 4x         1x 10x 10x 3x   7x     1x   26x   26x 7x   26x 26x 5x 5x 5x 5x 21x 2x         1x   11x   11x 10x 9x 7x   6x   6x 3x   3x 6x     3x 3x 3x   3x 1x 1x 1x   2x         3x 3x 3x       1x   7x   9x 2x 2x   9x         1x 8x 8x   8x 7x 7x 4x   7x         1x 6x   6x 1x 1x       1x 3x   3x 3x     1x  
"use strict";
 
var http = require("http");
 
/**
 * Represents a TaskHealthHelper object
 * @constructor
 * @param {object} scheduler - The scheduler object.
 * @param {object} options - The option map object.
 */
function TaskHealthHelper(scheduler, options) {
    if (!(this instanceof TaskHealthHelper)) {
        return new TaskHealthHelper(scheduler, options);
    }
 
    var self = this;
 
    self.scheduler = scheduler;
    self.logger = scheduler.logger;
    self.options = {};
    self.options.interval = options.interval || 30;
    self.options.graceCount = options.graceCount || 4;
    self.options.portIndex = options.portIndex || 0;
    self.options.propertyPrefix = options.propertyPrefix || "";
    self.options.errorEvent = options.errorEvent || self.options.propertyPrefix + "task_unhealthy";
    self.options.additionalProperties = options.additionalProperties || [];
    self.options.taskNameFilter = options.taskNameFilter || null;
    self.options.statusCodes = options.statusCodes || [200];
    self.options.checkBodyFunction = options.checkBodyFunction || null;
 
    if (options.url) {
        self.options.url = options.url;
    } else {
        throw new Error("Must set URL");
    }
 
    self.healthRequestCreate = function (host, port) {
        return {
            "host": host,
            "port": port,
            "path": options.url,
            "method": "GET",
            headers: {}
        };
    };
 
    self.checkRunningInstances = function () {
 
        self.logger.debug("Running periodic healthcheck" + (self.options.propertyPrefix.length ? ", prefix: " + self.options.propertyPrefix : ""));
 
        self.scheduler.launchedTasks.forEach(function (task) {
            self.checkInstance.call(self, task);
        });
    };
}
 
TaskHealthHelper.prototype.taskFilter = function (name) {
    var self = this;
    if (self.options.taskNameFilter) {
        return name.match(self.options.taskNameFilter) !== null;
    }
    return true;
};
 
TaskHealthHelper.prototype.setCheckFailed = function (task) {
 
    var self = this;
 
    if (task.runtimeInfo[self.options.propertyPrefix + "checkFailCount"] === undefined) {
        task.runtimeInfo[self.options.propertyPrefix + "checkFailCount"] = 0;
    }
    task.runtimeInfo[self.options.propertyPrefix + "checkFailCount"] += 1;
    if (task.runtimeInfo[self.options.propertyPrefix + "checkFailCount"] === self.options.graceCount) {
        self.logger.debug("Task marked unhealthy" + (self.options.propertyPrefix.length ? ", prefix: " + self.options.propertyPrefix : ""));
        task.runtimeInfo[self.options.propertyPrefix + "healthy"] = false;
        self.setProperties(task, false);
        self.scheduler.emit(self.options.errorEvent, task);
    } else if (task.runtimeInfo[self.options.propertyPrefix + "healthy"] === false) {
        self.scheduler.emit(self.options.errorEvent, task);
    }
};
 
 
TaskHealthHelper.prototype.checkInstance = function (task) {
 
    var self = this;
 
    if (task.runtimeInfo && task.runtimeInfo.state === "TASK_RUNNING" && self.taskFilter(task.name)) {
        if (task.runtimeInfo.network && task.runtimeInfo.network.hostname && task.runtimeInfo.network.ports && task.runtimeInfo.network.ports[self.options.portIndex]) {
            var req = http.request(self.healthRequestCreate(task.runtimeInfo.network.hostname, task.runtimeInfo.network.ports[self.options.portIndex]), function (res) {
                if (self.options.statusCodes.indexOf(res.statusCode) > -1) {
 
                    var value = false;
 
                    if (self.options.checkBodyFunction) {
                        var responseBodyBuilder = '';
 
                        res.on("data", function (chunk) {
                            responseBodyBuilder += chunk;
                        });
 
                        res.on("end", function () {
                            value = self.options.checkBodyFunction.call(self, task, responseBodyBuilder);
                            self.logger.debug("Checking the response body: " + value);
 
                            if (value) {
                                task.runtimeInfo[self.options.propertyPrefix + "checkFailCount"] = 0;
                                task.runtimeInfo[self.options.propertyPrefix + "healthy"] = value;
                                self.setProperties(task, value);
                            } else {
                                self.setCheckFailed.call(self, task);
                            }
 
                        });
                    } else {
                        task.runtimeInfo[self.options.propertyPrefix + "checkFailCount"] = 0;
                        task.runtimeInfo[self.options.propertyPrefix + "healthy"] = true;
                        self.setProperties(task, true);
                    }
 
                } else {
                    self.setCheckFailed.call(self, task);
                }
                res.resume();
            });
            req.on("error", function (error) {
                self.logger.error("Error checking task health:" + JSON.stringify(error) + (self.options.propertyPrefix.length ? ", prefix: " + self.options.propertyPrefix : ""));
                self.setCheckFailed.call(self, task);
            });
            req.end();
        }
    }
};
 
TaskHealthHelper.prototype.setProperties = function (task, value) {
    var self = this;
    self.options.additionalProperties.forEach(function (property) {
        // If healthy or setting unhealthy
        if (value || property.setUnhealthy) {
            var propertyValue = value;
            if (property.inverse) {
                propertyValue = !value;
            }
            task.runtimeInfo[property.name] = propertyValue;
        }
    });
};
 
TaskHealthHelper.prototype.stopHealthCheck = function () {
    var self = this;
 
    if (self.interval) {
        clearInterval(self.interval);
        self.interval = null;
    }
};
 
TaskHealthHelper.prototype.setupHealthCheck = function () {
    var self = this;
 
    self.stopHealthCheck();
    self.interval = setInterval(self.checkRunningInstances, self.options.interval * 1000);
};
 
module.exports = TaskHealthHelper;