Skip to content

Commit

Permalink
making sync interval happen AFTER sync is complete via timeout instea…
Browse files Browse the repository at this point in the history
…d of interval
  • Loading branch information
jeffsu committed Oct 18, 2012
1 parent 392bc0a commit 2b95fe3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
6 changes: 0 additions & 6 deletions lib/client.ms
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ export class Client {
return redis.createClient(config);
}

function inc(name, n) {
n = n || 1;
var data = this.getData(name);
data.inc(name, n);
}

function getStats(name) {
return this.stats[name] || (this.stats[name] = new Stats());
}
Expand Down
21 changes: 16 additions & 5 deletions lib/server.ms
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,24 @@ export class Server {
return services;
}

function sync(redis, time) {
function sync(redis, time, cb) {
var count = 0;
function fin() {
if (--count == 0) return cb ? cb() : null;
}

for (var serviceName in this.services) {
var service = this.services[serviceName];
for (var sensorName in service.sensors) {
var sensor = service.sensors[sensorName];
sensor.sync(time, redis, "upbeat:services:" + serviceName + ":" + sensorName);
count++;
sensor.sync(time, redis, "upbeat:services:" + serviceName + ":" + sensorName, fin);
}
}

for (var k in this.stats) {
this.stats[k].syncTime(time, redis, k);
count++;
this.stats[k].syncTime(time, redis, k, fin);
}
}

Expand Down Expand Up @@ -102,11 +109,15 @@ export class Server {
function setSyncInterval(time, interval, redis) {
function run() {
if (self.status != 'running') return;
if (self.redisConnected) self.sync(redis, time);
if (self.redisConnected) self.sync(redis, time, beat);
else beat();
}

function beat() {
setTimeout(run, interval);
}

self.toRun.push(run);
setInterval(run, interval);
}

function buildService(name, config) {
Expand Down

0 comments on commit 2b95fe3

Please sign in to comment.