Code coverage report for kardia/lib/worker.js

Statements: 40.21% (39 / 97)      Branches: 0% (0 / 47)      Functions: 18.18% (4 / 22)      Lines: 40.63% (39 / 96)      Ignored: none     

All files » kardia/lib/ » worker.js
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 1821   1     1 1 1 1   1 1 1 1 1 1 1   1 1   1   1   1                   1     1       1       1       1       1                                                                                 1                         1         1           1         1 1 1                       1               1                   1               1 1     1               1       1  
(function() {
	'use strict';
	var fluentd = require(__dirname + '/fluentd.js'),
		cluster = require('cluster');
 
	function Worker(worker, config) {
		this.pid = worker.process.pid;
		this.id = worker.id;
		this.startTime = new Date();
 
		this.values = {};
		this.counters = {};
		this.stacks = {};
		this.stackConfig = {};
		this.throughputs = {};
		this.throughputsBuffer = {};
		this.eventListeners = {};
 
		this.startMemory = process.memoryUsage();
		this.fallBehind = 0;
 
		this.config = config;
 
		this.measureFallBehind();
 
		this.workerMessageListener = worker.on('message', (function workerMessageListenerHandler(msg) {
			if (!msg['~kardia']) {
				return;
			}
 
			if (msg['~kardia'].cmd && msg['~kardia'].args && this[msg['~kardia'].cmd]) {
				this[msg['~kardia'].cmd].apply(this, msg['~kardia'].args);
			}
		}).bind(this));
 
		return this;
	}
 
	Worker.prototype.set = function(name, value) {
		this.values[name] = value;
	};
 
	Worker.prototype.unset = function(name, value) {
		delete this.values[name];
	};
 
	Worker.prototype.increment = function(name, value) {
		this.counters[name] = (this.counters[name] || 0) + (Number(value) || 1);
	};
 
	Worker.prototype.decrement = function(name, value) {
		this.increment(name, -(Number(value) || 1));
	};
 
	Worker.prototype.throughput = function(name) {
		if (cluster.isWorker && process.send) {
			process.send({ '~kardia': { cmd: 'throughput', args: [].slice.call(arguments) } });
			return;
		}
 
		if (!this.throughputsBuffer[name]) {
			this.throughputsBuffer[name] = 0;
		}
 
		this.throughputsBuffer[name]++;
 
		// the throughput calculation interval time in seconds (will likely be configurable in the future):
		if (!this.throughputCalculationStep) {
			this.throughputCalculationStep = 30;
		}
 
		if (!this.throughputInterval && Object.keys(this.throughputsBuffer).length > 0) {
 
			// set up the appropriate interval to calculate throughput rates per sec, minute, hour
			this.throughputInterval = setInterval((function() {
				Object.keys(this.throughputsBuffer).forEach((function(key) {
					if (!this.throughputs[key]) {
						this.throughputs[key] = {};
					}
					if (!this.throughputs[key][this.throughputCalculationStep]) {
						this.throughputs[key][this.throughputCalculationStep] = {};
					}
					var x = this.throughputs[key][this.throughputCalculationStep];
					x.sec = parseInt((this.throughputsBuffer[key] / this.throughputCalculationStep).toFixed(2), 10);
					x.min = parseInt((this.throughputsBuffer[key] * (60 / this.throughputCalculationStep)).toFixed(2), 10);
					x.hour = parseInt((this.throughputsBuffer[key] * (3600 / this.throughputCalculationStep)).toFixed(2), 10);
					this.throughputsBuffer[key] = 0;
 
				}).bind(this));
				this.emit('throughputIntervalRun');
				
			}).bind(this), 1000 * this.throughputCalculationStep);
		}
	};
 
	Worker.prototype.clearThroughput = function(name) {
		if (cluster.isWorker && process.send) {
			process.send({ '~kardia': { cmd: 'clearThroughput', args: [].slice.call(arguments) } });
			return;
		}
		delete this.throughputsBuffer[name];
		delete this.throughput[name];
 
		if (Object.keys(this.throughputInterval).length === 0 && this.throughputInterval) {
			clearInterval(this.throughputInterval);
		}
	};
 
	Worker.prototype.startStack = function(name, size) {
		this.stacks[name] = [];
		this.stackConfig[name] = { size: Number(size) || 15 };
	};
 
	Worker.prototype.stack = function(name, value) {
		if (!this.stacks[name]) this.startStack(name);
		this.stacks[name].unshift({ time: new Date().toISOString(), value: value });
		this.stacks[name] = this.stacks[name].splice(0, this.stackConfig[name].size);
	};
 
	Worker.prototype.stopStack = function(name) {
		delete this.stacks[name];
		delete this.stackConfig[name];
	};
 
	Worker.prototype.generateStatus = function() {
		var date = new Date();
		return {
			pid: this.pid,
			startTime: this.startTime,
			uptime: Math.floor((date.getTime() - this.startTime.getTime())/1000),
			values: this.values,
			stacks: this.stacks,
			throughput: this.throughputs,
			counters: this.counters,
			fallBehind: this.fallBehind
		};
	};
 
	Worker.prototype.emit = function(eventName, data) {
		if (this.eventListeners[eventName]) {
			this.eventListeners[eventName].forEach(function(listener) {
				listener(data);
			});
		}
	};
 
	Worker.prototype.on = function(eventName, handler) {
		if (!this.eventListeners[eventName]) {
			this.eventListeners[eventName] = [];
		}
 
		this.eventListeners[eventName].push(handler);
 
		return true;
	};
 
	Worker.prototype.removeListener = function(eventName, handler) {
		if (this.eventListeners[eventName]) {
			this.eventListeners[eventName].splice(this.eventListeners[eventName].indexOf(handler));
		}
 
		return true;
	};
 
	Worker.prototype.measureFallBehind = function() {
		var that = this,
			time = process.hrtime();
 
		setTimeout(function() {
			var diff = process.hrtime(time);
 
			that.fallBehind = ((diff[0] - 1) * 1e9 + diff[1]) / 1e6;
			that.measureFallBehind();
		}, 1000).unref();
	};
 
	Worker.prototype.sendCounterToFluentd = function(name, value) {
		fluentd.incrementCounter(this.config, name, value);
	};
 
	module.exports = Worker;
})();