|
| 1 | +var EventEmitter = require('events').EventEmitter; |
| 2 | +var util = require('util'); |
| 3 | +var DefaultDispatcher = require('./lib/dispatcher'); |
| 4 | +var async = require('async'); |
| 5 | + |
| 6 | +var delay = require('./lib/loop-delay'); |
| 7 | +var memory = require('./lib/memory'); |
| 8 | +var GC = require('./lib/gc'); |
| 9 | +var cpu = require('./lib/cpu'); |
| 10 | + |
| 11 | +function Indy(options) { |
| 12 | + EventEmitter.call(this); |
| 13 | + |
| 14 | + this.options = options || {}; |
| 15 | + this.dispatcher = this.options.dispatcher || new DefaultDispatcher(); |
| 16 | + this.interval = this.options.interval || 10000; // in ms |
| 17 | + this.intervalRef = undefined; |
| 18 | + this.gc = new GC(); |
| 19 | +} |
| 20 | + |
| 21 | +util.inherits(Indy, EventEmitter); |
| 22 | + |
| 23 | +// Start watching for any events and publish them to our dispatcher. |
| 24 | + |
| 25 | +Indy.prototype.whip = function () { |
| 26 | + this.gc.start(); |
| 27 | + var self = this; |
| 28 | + var collect = function () { |
| 29 | + var metrics = |
| 30 | + { delay: delay, |
| 31 | + gcCount: self.gc.delta.bind(self.gc), |
| 32 | + memRss: memory.rss, |
| 33 | + memFree: memory.free, |
| 34 | + memTotal: memory.total, |
| 35 | + memHeapTotal: memory.heapTotal, |
| 36 | + memHeapUsed: memory.heapUsed, |
| 37 | + cpus: cpu.cpus, |
| 38 | + loadavg5: cpu.loadavg5, |
| 39 | + loadavg10: cpu.loadavg10, |
| 40 | + loadavg15: cpu.loadavg15 |
| 41 | + }; |
| 42 | + |
| 43 | + async.parallelLimit(metrics, 2, function (err, data) { |
| 44 | + if (err) { |
| 45 | + return; |
| 46 | + } |
| 47 | + self.dispatcher.dispatch(data); |
| 48 | + }); |
| 49 | + }; |
| 50 | + |
| 51 | + self.intervalRef = setInterval(collect, self.interval); |
| 52 | +}; |
| 53 | + |
| 54 | +Indy.prototype.pause = function (done) { |
| 55 | + var self = this; |
| 56 | + if (!self.intervalRef) { |
| 57 | + return; |
| 58 | + } |
| 59 | + clearInterval(self.intervalRef); |
| 60 | + done(); |
| 61 | +}; |
| 62 | + |
| 63 | +Indy.prototype.resume = function () { |
| 64 | + if (this.intervalRef) { |
| 65 | + return; |
| 66 | + } |
| 67 | + this.whip(); |
| 68 | +}; |
| 69 | + |
| 70 | +module.exports = Indy; |
0 commit comments