dont run intervals stacked up

master
Bel LaPointe 2021-12-13 13:03:17 -07:00
parent dbf92ea0c6
commit 1be78e4cd3
1 changed files with 64 additions and 30 deletions

View File

@ -1338,19 +1338,70 @@
this.databases.push(arguments[i]);
}
this.deltas = new Deltas(this.cache());
this.intervals = [];
this.scheduleSync();
this.syncCacheWithPrimary();
this.syncIntervalCacheWithPrimary();
}
syncCacheWithPrimary()
redundantSyncInterval(key)
{
let last_run = this.intervals_last_run[key];
let now = Date.now();
let redundant = last_run != undefined && now - last_run < 1000;
console.log(`redundant ${key}? last=${last_run}, now=${now}, redundant=${redundant}`);
if (!redundant)
this.intervals_last_run[key] = now;
return redundant;
}
syncIntervalDeltas()
{
if (!this.redundantSyncInterval("syncIntervalDeltas"))
return;
this._syncIntervalDeltas();
}
_syncIntervalDeltas()
{
var n = this.pendingSyncCount();
try
{
this.deltas.popEach((key) => {
console.log("syncing: " + key);
this.replicate(key);
});
}
catch (e)
{
console.log("failed to sync: " + e);
}
finally
{
this.writePendingSyncCount();
}
}
syncIntervalCacheWithPrimary()
{
if (!this.redundantSyncInterval("syncIntervalCacheWithPrimary"))
return;
this._syncIntervalCacheWithPrimary();
}
_syncIntervalCacheWithPrimary()
{
if (this.pendingSyncCount())
return;
this.syncAWithB(this.cache(), this.primary());
}
syncReplicasWithCache()
syncIntervalReplicasWithCache()
{
if (!this.redundantSyncInterval("syncIntervalReplicasWithCache"))
return;
this._syncIntervalReplicasWithCache();
}
_syncIntervalReplicasWithCache()
{
this.replicas().slice(1).forEach((replica) => {
this.syncAWithB(replica, this.cache());
@ -1383,43 +1434,26 @@
scheduleSync()
{
this.close();
this.intervals.push(
setInterval(() => { this.syncDeltas(); }, 10 * 1000)
setInterval(() => { this.syncIntervalDeltas(); }, 10 * 1000)
);
this.intervals.push(
setInterval(() => { this.syncCacheWithPrimary(); }, 120 * 1000)
setInterval(() => { this.syncIntervalCacheWithPrimary(); }, 120 * 1000)
);
this.intervals.push(
setInterval(() => { this.syncReplicasWithCache(); }, 123 * 1000)
setInterval(() => { this.syncIntervalReplicasWithCache(); }, 123 * 1000)
);
}
close()
{
this.intervals.forEach((i) => {
clearInterval(i);
});
this.intervals = [];
}
syncDeltas()
{
var n = this.pendingSyncCount();
try
{
this.deltas.popEach((key) => {
console.log("syncing: " + key);
this.replicate(key);
if (this.intervals)
this.intervals.forEach((i) => {
clearInterval(i);
});
}
catch (e)
{
console.log("failed to sync: " + e);
}
finally
{
this.writePendingSyncCount();
}
this.intervals = [];
this.intervals_last_run = {};
}
pendingSyncCount()