Commit 8cfb4550 authored by Alan Szlosek's avatar Alan Szlosek

use regex pattern to determine digest trigger

parent a53f7c98
......@@ -28,10 +28,15 @@ Valid intervals, for now:
// Think I want to let devs specify their own method for rolling up messages
// Maybe our default will be a line with the message id, followed by a count of those rolled up
match is a regex against a ISO-8601 string
secondsGranularity - instead of comparing pattern every minute, true sets interval to every second
*/
var Digest = function(interval, cacheSize, deliveryCallback) {
var Digest = function(secondsGranularity, pattern, cacheSize, deliveryCallback) {
var self = this;
this.pattern = pattern;
// Maybe cacheSize can let us specify how many messages to keep a copy of
// We don't want to cache all messages added to the digest, as we'd eath through memory
this.cacheSize = cacheSize || 10;
......@@ -43,38 +48,49 @@ var Digest = function(interval, cacheSize, deliveryCallback) {
this.deliveryCallback = deliveryCallback;
// Schedule delivery
this.schedule(interval);
};
Digest.prototype.schedule = function(interval) {
var self = this;
// SCHEDULING STUFF
var callback = function() {
// Schedule next timeout ASAP
// Also give deliveryCallback chance to cancel the timeout
clearTimeout(self.timeoutHandle);
reschedule();
var _intervalCallback = function() {
var now = new Date();
if (!self.pattern) {
self.deliveryCallback(self.counts, self.cache);
self.counts = {};
self.cache = {};
return;
}
self.deliveryCallback(self.counts, self.cache);
self.counts = {};
self.cache = {};
};
var reschedule = function() {
var seconds = 2 * 1000;
self.timeoutHandle = setTimeout(
callback,
seconds
);
};
reschedule();
if (now.toISOString().match(self.pattern)) {
self.deliveryCallback(self.counts, self.cache);
self.counts = {};
self.cache = {};
}
};
// Schedule delivery
if (secondsGranularity) {
this.intervalHandle = setInterval(
_intervalCallback,
// Every second
1000
);
} else {
var now = new Date();
var diff = 60 - (now.getSeconds() % 60);
setTimeout(
function() {
_intervalCallback();
this.intervalHandle = setInterval(
_intervalCallback,
// Every minute
60 * 1000
);
},
diff * 1000
);
}
};
Digest.prototype.add = function(id, message) {
if (!(id in this.cache)) {
this.counts[id] = 0;
......@@ -87,8 +103,8 @@ Digest.prototype.add = function(id, message) {
Digest.prototype.stop = function() {
console.log('digest.stop');
if (this.timeoutHandle) {
clearTimeout(this.timeoutHandle);
if (this.intervalHandle) {
clearInterval(this.intervalHandle);
}
};
......
......@@ -3,7 +3,7 @@ var moment = require('moment');
var Harbinger = require('../index');
tap.test('digest', function(test) {
tap.test('digest.second', function(test) {
test.plan(2);
var start = moment().unix();
......@@ -11,15 +11,37 @@ tap.test('digest', function(test) {
var end = moment().unix();
// Make sure callback took 2 seconds to fire
test.equal(end - start, 2);
test.equal(end - start, 1);
// Make sure we've seen two instances of message id 123
test.equal(counts[123], 2);
digest.stop();
test.end();
};
var digest = new Harbinger.Digest('10 second', 10, callback);
var digest = new Harbinger.Digest(true, null, 10, callback);
digest.add(123, {message: 'Test'});
digest.add(123, {message: 'Another'});
});
tap.test('digest.five-second', function(test) {
test.plan(2);
var start = moment().unix();
var callback = function(counts, cache) {
var now = new Date();
test.ok(now.getSeconds() == 0 || now.getSeconds() % 5 == 0);
// Make sure we've seen two instances of message id 123
test.equal(counts[456], 2);
digest.stop();
test.end();
};
var digest = new Harbinger.Digest(true, /(\d0|\d5)\.\d{1,3}Z/, 10, callback);
digest.add(456, {message: 'Test'});
digest.add(456, {message: 'Another'});
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment