Skip to content

Commit

Permalink
馃悰 Fixed post scheduling (#8976)
Browse files Browse the repository at this point in the history
closes #8975

- recursive logic was broken
- caused via bf47397
  • Loading branch information
kirrg001 authored and kevinansfield committed Sep 5, 2017
1 parent 69657a1 commit d460cf1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
42 changes: 23 additions & 19 deletions core/server/adapters/scheduling/SchedulingDefault.js
Expand Up @@ -54,35 +54,39 @@ SchedulingDefault.prototype.unschedule = function (object) {
*/
SchedulingDefault.prototype.run = function () {
var self = this,
timeout = null;
timeout = null,
recursiveRun;

if (this.isRunning) {
return;
}

this.isRunning = true;

timeout = setTimeout(function () {
var times = Object.keys(self.allJobs),
nextJobs = {};

times.every(function (time) {
if (moment(Number(time)).diff(moment(), 'minutes') <= self.offsetInMinutes) {
nextJobs[time] = self.allJobs[time];
delete self.allJobs[time];
return true;
}
recursiveRun = function recursiveRun() {
timeout = setTimeout(function () {
var times = Object.keys(self.allJobs),
nextJobs = {};

times.every(function (time) {
if (moment(Number(time)).diff(moment(), 'minutes') <= self.offsetInMinutes) {
nextJobs[time] = self.allJobs[time];
delete self.allJobs[time];
return true;
}

// break!
return false;
});

// break!
return false;
});
clearTimeout(timeout);
self._execute(nextJobs);

clearTimeout(timeout);
self._execute(nextJobs);
recursiveRun();
}, self.runTimeoutInMs);
};

// recursive!
self.run();
}, self.runTimeoutInMs);
recursiveRun();
};

/**
Expand Down
14 changes: 14 additions & 0 deletions core/test/unit/adapters/scheduling/SchedulingDefault_spec.js
Expand Up @@ -84,6 +84,20 @@ describe('Scheduling Default Adapter', function () {
scope.adapter.run();
});

it('ensure recursive run works', function (done) {
sandbox.spy(scope.adapter, '_execute');

scope.adapter.allJobs = {};
scope.adapter.runTimeoutInMs = 500;
scope.adapter.offsetInMinutes = 2;
scope.adapter.run();

setTimeout(function () {
scope.adapter._execute.callCount.should.be.greaterThan(1);
done();
}, 2000);
});

it('execute', function (done) {
var pinged = 0,
jobs = 3,
Expand Down

0 comments on commit d460cf1

Please sign in to comment.