Skip to content

Commit

Permalink
change the internal 'info' naming to 'job'
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Feb 28, 2019
1 parent b51325d commit 71c1843
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
32 changes: 16 additions & 16 deletions cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ Cron.prototype.scheduleCall = function scheduleCall( func, at, repeat ) {
if (typeof options.at !== 'number') throw new Error('at not a number');
if (!Array.isArray(options.args)) throw new Error('args not an array');

var info = {
var job = {
func: options.func, args: options.args, _at: options.at, _repeat: options.repeat,
_cron: this, _self: options.self || null, _timer: null, _start: null,
pause: function() { clearTimeout(this._timer); this._timer = null },
resume: function() { if (!this._timer) startTimer(this) }
};

startTimer(info);
this.scheduled.push(info);
return info;
startTimer(job);
this.scheduled.push(job);
return job;

function startTimer(info) {
function startTimer(job) {
// use setTimeout to be in control of our schedule
var now = new Date();
info._start = info._cron._findNextRuntime(now, info._at, info._repeat);
info._timer = setTimeout(runCall, info._start - +now, info);
info._timer.unref && info._timer.unref();
job._start = job._cron._findNextRuntime(now, job._at, job._repeat);
job._timer = setTimeout(runCall, job._start - +now, job);
job._timer.unref && job._timer.unref();
}

function runCall(info) {
function runCall(job) {
// run func first to not overlap next run
invoke(info.func, info._self, info.args);
if (info._repeat > 0) startTimer(info); else info._cron.cancelCall(info);
invoke(job.func, job._self, job.args);
if (job._repeat > 0) startTimer(job); else job._cron.cancelCall(job);
}
}

Expand All @@ -61,12 +61,12 @@ Cron.prototype.cancelCall = function cancelCall( funcOrInfo ) {
this.scheduled = new Array();
var removed = new Array();
for (var i = 0; i < jobs.length; i++) {
var info = jobs[i];
if (info.func === funcOrInfo || info === funcOrInfo || funcOrInfo === '_all') {
info._timer = (clearTimeout(info._timer), null);
removed.push(info);
var job = jobs[i];
if (job.func === funcOrInfo || job === funcOrInfo || funcOrInfo === '_all') {
job.pause();
removed.push(job);
} else {
this.scheduled.push(info);
this.scheduled.push(job);
}
}
return removed;
Expand Down
42 changes: 21 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ module.exports = {
},

'should invoke one-shot without repeat': function(t) {
var info = cron.scheduleCall(noop, offsetNow(10));
t.ok(info._start > Date.now());
var job = cron.scheduleCall(noop, offsetNow(10));
t.ok(job._start > Date.now());
t.done();
},

'should accept human-legible time spec': function(t) {
var info;
info = cron.scheduleCall(noop, 0, '1000');
t.equal(info._repeat, 1000);
var job;
job = cron.scheduleCall(noop, 0, '1000');
t.equal(job._repeat, 1000);
t.equal(cron.scheduleCall(noop, 0, '2h')._repeat, 7200000);
t.equal(cron.scheduleCall(noop, 0, '3m')._repeat, 180000);
t.equal(cron.scheduleCall(noop, 0, '4s')._repeat, 4000);
Expand All @@ -59,8 +59,8 @@ module.exports = {

'should cancel a call': function(t) {
var called = false;
var info = cron.scheduleCall(function() { called = true }, 0, 10);
cron.cancelCall(info);
var job = cron.scheduleCall(function() { called = true }, 0, 10);
cron.cancelCall(job);
setTimeout(function() {
t.ok(!called);
t.done();
Expand All @@ -82,45 +82,45 @@ module.exports = {

'should run a call at the specified time and interval': function(t) {
var callCount = 0;
var info = cron.scheduleCall(function testCall() {
t.equal(info.func, testCall);
t.deepEqual(info.args, []);
var job = cron.scheduleCall(function testCall() {
t.equal(job.func, testCall);
t.deepEqual(job.args, []);
var now = Date.now();
// allow for a possible off-by-one with nodejs setTimeout
now += 1;
t.ok(info._start <= now);
t.ok(job._start <= now);
t.ok(now % 30 < 5);
if (++callCount === 5) {
cron.cancelCall(info);
cron.cancelCall(job);
t.done();
}
}, 0, 30);
// _start is set to the call scheduled time
t.ok(info._start > Date.now());
t.ok(job._start > Date.now());
},

'should run a call only once': function(t) {
var callCount = 0;
var info = cron.scheduleCall(function() { callCount += 1 }, offsetNow(5));
var job = cron.scheduleCall(function() { callCount += 1 }, offsetNow(5));
setTimeout(function() {
t.equal(callCount, 1);
t.deepEqual(cron.cancelCall(info), []);
t.deepEqual(cron.cancelCall(job), []);
t.done();
}, 17);
},

'should schedule a call for two days out': function(t) {
var info = cron.scheduleCall(noop, 3 * 24 * 3600 * 1000 + 3600000, 100);
t.ok(info._start > Date.now() + 48 * 3600 * 1000);
t.equal(info._repeat, 100);
var job = cron.scheduleCall(noop, 3 * 24 * 3600 * 1000 + 3600000, 100);
t.ok(job._start > Date.now() + 48 * 3600 * 1000);
t.equal(job._repeat, 100);
t.done();
},

'should schedule a one-shot call for tomorrow if hour already passed': function(t) {
var now = Date.now();
var info = cron.scheduleCall(noop, offsetNow(-10));
t.ok(info._start < Date.now() + Cron.msPerDay);
t.ok(Date.now() + Cron.msPerDay < info._start + 20);
var job = cron.scheduleCall(noop, offsetNow(-10));
t.ok(job._start < Date.now() + Cron.msPerDay);
t.ok(Date.now() + Cron.msPerDay < job._start + 20);
t.done();
},

Expand Down

0 comments on commit 71c1843

Please sign in to comment.