Permalink
Browse files

Reset the time.

It may be nice to be able to reset the time on a cronjob. In order to do
this we need to assert that the time is a CronTime, assert that the
existing job has been stopped (assume that the stop function is
working), and then change the time. We leave it up to the user to
restart the job.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
1 parent 996c540 commit ef1be08d026b9cdb6fbad3deab6ee0660c4fddf5 @ncb000gt ncb000gt committed Aug 30, 2012
Showing with 41 additions and 0 deletions.
  1. +9 −0 lib/cron.js
  2. +32 −0 tests/test-cron.js
View
@@ -274,6 +274,15 @@ CronJob.prototype = {
}
},
+ /**
+ * Manually set the time of a job
+ */
+ setTime: function(time) {
+ if (!(time instanceof CronTime)) throw '\'time\' must be an instance of CronTime.';
+ this.stop();
+ this.cronTime = time;
+ },
+
/**
* Start the cronjob.
View
@@ -327,5 +327,37 @@ module.exports = testCase({
assert.ok(true);
assert.done();
}, 250);
+ },
+ 'test start, change time, start again': function(assert) {
+ assert.expect(3);
+ var c = new cron.CronJob('* * * * * *', function() {
+ assert.ok(true);
+ });
+ var time = cron.time('*/2 * * * * *');
+ c.start();
+ setTimeout(function() {
+ c.stop();
+ c.setTime(time);
+ c.start();
+ setTimeout(function() {
+ c.stop();
+ assert.done();
+ }, 4250);
+ }, 1250);
+ },
+ 'test start, change time, excpetion': function(assert) {
+ assert.expect(2);
+ var c = new cron.CronJob('* * * * * *', function() {
+ assert.ok(true);
+ });
+ var time = new Date();
+ c.start();
+ setTimeout(function() {
+ c.stop();
+ assert.throws(function() {
+ c.setTime(time);
+ });
+ assert.done();
+ }, 1250);
}
});

0 comments on commit ef1be08

Please sign in to comment.