UNPKG

librato-node

Version:

A node.js client for Librato Metrics (http://metrics.librato.com/)

69 lines (58 loc) 1.54 kB
// Generated by CoffeeScript 1.12.7 (function() { var Worker; Worker = (function() { Worker.prototype.period = 60000; function Worker(arg) { var period, ref; ref = arg != null ? arg : {}, this.job = ref.job, period = ref.period; if (this.job == null) { throw new Error('must provide job'); } if (period != null) { this.period = period; } this.stopped = true; } Worker.prototype.start = function() { var nextRun, workFn; this.stopped = false; nextRun = Worker.startTime(this.period); workFn = (function(_this) { return function() { var now; while (true) { now = Date.now(); if (now >= nextRun) { _this.job(); while (nextRun <= now) { nextRun += _this.period; } } else { _this.timerId = setTimeout(workFn, Math.min(nextRun - now, _this.period)); return; } } }; })(this); return workFn(); }; Worker.prototype.stop = function() { if (this.stopped) { return; } this.stopped = true; if (this.timerId != null) { return clearTimeout(this.timerId); } }; Worker.startTime = function(period) { var now; now = Date.now(); return now + (period - (now % period)); }; return Worker; })(); module.exports = Worker; }).call(this); //# sourceMappingURL=worker.js.map