Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-schedule

Package Overview
Dependencies
Maintainers
4
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-schedule - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

50

lib/schedule.js

@@ -28,2 +28,5 @@

//setup a private number of invocations variable
var triggeredJobs = 0;
// Set scope vars

@@ -77,2 +80,8 @@ var jobName = name && typeof name === 'string' ? name : '<Anonymous Job ' + (++anonJobCounter) + '>';

};
this.triggeredJobs = function() {
return triggeredJobs;
};
this.setTriggeredJobs = function(triggeredJob) {
triggeredJobs = triggeredJob;
};
this.cancel = function(reschedule) {

@@ -105,3 +114,3 @@ reschedule = (typeof reschedule == 'boolean') ? reschedule : false;

if (this.name) {
scheduledJobs[this.name] = null;
delete scheduledJobs[this.name];
}

@@ -133,2 +142,22 @@ }

};
this.reschedule = function(spec) {
var inv;
var cInvs = pendingInvocations.slice();
for (var j = 0; j < cInvs.length; j++) {
inv = cInvs[j];
cancelInvocation(inv);
}
pendingInvocations = [];
if (this.schedule(spec)) {
this.setTriggeredJobs(0);
return true;
} else {
pendingInvocations = cInvs;
return false;
}
};
this.nextInvocation = function() {

@@ -149,2 +178,3 @@ if (!pendingInvocations.length) {

if (typeof this.job == 'function') {
this.setTriggeredJobs(this.triggeredJobs() + 1);
this.job();

@@ -375,3 +405,3 @@ } else {

if (then < now) {
process.nextTick(job);
setImmediate(job);
return null;

@@ -487,2 +517,17 @@ }

function rescheduleJob(job, spec) {
if (job instanceof Job) {
if (job.reschedule(spec)) {
return job;
}
} else if (typeof job == 'string' || job instanceof String) {
if (job in scheduledJobs && scheduledJobs.hasOwnProperty(job)) {
if (scheduledJobs[job].reschedule(spec)) {
return scheduledJobs[job];
}
}
}
return null;
}
function cancelJob(job) {

@@ -507,3 +552,4 @@ var success = false;

module.exports.scheduleJob = scheduleJob;
module.exports.rescheduleJob = rescheduleJob;
module.exports.scheduledJobs = scheduledJobs;
module.exports.cancelJob = cancelJob;

2

package.json
{
"name": "node-schedule",
"version": "1.0.0",
"version": "1.1.0",
"description": "A cron-like and not-cron-like job scheduler for Node.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -167,6 +167,6 @@ # Node Schedule

It's worth noting that the default value of a component of a recurrence rule is
`null` (except for seconds, which is 0 for familiarity with cron). If we did not
> **Note**: It's worth noting that the default value of a component of a recurrence rule is
`null` (except for seconds, which is 0 for familiarity with cron). *If we did not
explicitly set `minute` to 0 above, the message would have instead been logged at
5:00pm, 5:01pm, 5:02pm, ..., 5:59pm. Probably not what you want.
5:00pm, 5:01pm, 5:02pm, ..., 5:59pm.* Probably not what you want.

@@ -173,0 +173,0 @@ #### Object Literal Syntax

@@ -202,2 +202,358 @@

},
".rescheduleJob(job, {...})": {
"Reschedule jobs from object based to object based": function(test) {
test.expect(3);
var job = new schedule.scheduleJob({
second: null
}, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job, {
minute: null
});
}, 3250);
setTimeout(function() {
job.cancel();
test.done();
}, 5000);
clock.tick(5000);
},
"Reschedule jobs from every minutes to every second": function(test) {
test.expect(3);
var timeout = 60 * 1000;
var job = new schedule.scheduleJob({
minute: null
}, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job, {
second: null
});
}, timeout);
setTimeout(function() {
job.cancel();
test.done();
}, timeout + 2250);
clock.tick(timeout + 2250);
}
},
".rescheduleJob(job, Date)": {
"Reschedule jobs from Date to Date": function(test) {
test.expect(1);
var job = new schedule.scheduleJob(new Date(Date.now() + 3000), function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job, new Date(Date.now() + 5000));
}, 1000);
setTimeout(function() {
test.done();
}, 6150);
clock.tick(6150);
},
"Reschedule jobs that has been executed": function(test) {
test.expect(2);
var job = new schedule.scheduleJob(new Date(Date.now() + 1000), function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job, new Date(Date.now() + 2000));
}, 2000);
setTimeout(function() {
test.done();
}, 5150);
clock.tick(5150);
}
},
".rescheduleJob(job, RecurrenceRule)": {
"Reschedule jobs from RecurrenceRule to RecurrenceRule": function(test) {
test.expect(3);
var timeout = 60 * 1000;
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
var newRule = new schedule.RecurrenceRule();
newRule.minute = null;
setTimeout(function() {
schedule.rescheduleJob(job, newRule);
}, 2250);
setTimeout(function() {
job.cancel();
test.done();
}, timeout + 2250);
clock.tick(timeout + 2250);
},
"Reschedule jobs from RecurrenceRule to Date": function(test) {
test.expect(3);
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job, new Date(Date.now() + 2000));
}, 2150);
setTimeout(function() {
test.done();
}, 4250);
clock.tick(4250);
},
"Reschedule jobs from RecurrenceRule to {...}": function(test) {
test.expect(3);
var timeout = 60 * 1000;
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job, {
minute: null
});
}, 2150);
setTimeout(function() {
job.cancel();
test.done();
}, timeout + 2150);
clock.tick(timeout + 2150);
},
"Reschedule jobs that is not available": function(test) {
test.expect(4);
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(null, new Date(Date.now() + 2000));
}, 2150);
setTimeout(function() {
job.cancel();
test.done();
}, 4250);
clock.tick(4250);
}
},
'.rescheduleJob("job name", {...})': {
"Reschedule jobs from object based to object based": function(test) {
test.expect(3);
var job = new schedule.scheduleJob({
second: null
}, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job.name, {
minute: null
});
}, 3250);
setTimeout(function() {
job.cancel();
test.done();
}, 5000);
clock.tick(5000);
},
"Reschedule jobs from every minutes to every second": function(test) {
test.expect(3);
var timeout = 60 * 1000;
var job = new schedule.scheduleJob({
minute: null
}, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job.name, {
second: null
});
}, timeout);
setTimeout(function() {
job.cancel();
test.done();
}, timeout + 2250);
clock.tick(timeout + 2250);
}
},
'.rescheduleJob("job name", Date)': {
"Reschedule jobs from Date to Date": function(test) {
test.expect(1);
var job = new schedule.scheduleJob(new Date(Date.now() + 3000), function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job.name, new Date(Date.now() + 5000));
}, 1000);
setTimeout(function() {
test.done();
}, 6150);
clock.tick(6150);
},
"Reschedule jobs that has been executed": function(test) {
test.expect(2);
var job = new schedule.scheduleJob(new Date(Date.now() + 1000), function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job.name, new Date(Date.now() + 2000));
}, 2000);
setTimeout(function() {
test.done();
}, 5150);
clock.tick(5150);
}
},
'.rescheduleJob("job name", RecurrenceRule)': {
"Reschedule jobs from RecurrenceRule to RecurrenceRule": function(test) {
test.expect(3);
var timeout = 60 * 1000;
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
var newRule = new schedule.RecurrenceRule();
newRule.minute = null;
setTimeout(function() {
schedule.rescheduleJob(job.name, newRule);
}, 2250);
setTimeout(function() {
job.cancel();
test.done();
}, timeout + 2250);
clock.tick(timeout + 2250);
},
"Reschedule jobs from RecurrenceRule to Date": function(test) {
test.expect(3);
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job.name, new Date(Date.now() + 2000));
}, 2150);
setTimeout(function() {
test.done();
}, 4250);
clock.tick(4250);
},
"Reschedule jobs from RecurrenceRule to {...}": function(test) {
test.expect(3);
var timeout = 60 * 1000;
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob(job.name, {
minute: null
});
}, 2150);
setTimeout(function() {
job.cancel();
test.done();
}, timeout + 2150);
clock.tick(timeout + 2150);
},
"Reschedule jobs that is not available": function(test) {
test.expect(4);
var rule = new schedule.RecurrenceRule();
rule.second = null; // fire every second
var job = schedule.scheduleJob(rule, function() {
test.ok(true);
});
setTimeout(function() {
schedule.rescheduleJob("Blah", new Date(Date.now() + 2000));
}, 2150);
setTimeout(function() {
job.cancel();
test.done();
}, 4250);
clock.tick(4250);
}
},
".cancelJob(Job)": {

@@ -204,0 +560,0 @@ "Prevents all future invocations of Job passed in": function(test) {

@@ -420,9 +420,9 @@

test.equal(schedule.scheduledJobs.cancelJob, job1);
test.equal(schedule.scheduledJobs.second, job2);
test.strictEqual(schedule.scheduledJobs.cancelJob, job1);
test.strictEqual(schedule.scheduledJobs.second, job2);
setTimeout(function() {
job1.cancel();
job2.cancel();
test.equal(schedule.scheduledJobs.cancelJob, null);
test.equal(schedule.scheduledJobs.second, null);
test.strictEqual(schedule.scheduledJobs.cancelJob, undefined);
test.strictEqual(schedule.scheduledJobs.second, undefined);
test.done();

@@ -434,2 +434,25 @@ }, 1250);

},
"#reschedule": {
"When rescheduled counter will be reset to zero": function(test) {
var job = new schedule.scheduleJob({
second: null
}, function() {});
setTimeout(function() {
test.equal(job.triggeredJobs(), 3);
schedule.rescheduleJob(job, {
minute: null
});
}, 3250);
setTimeout(function() {
job.cancel();
test.equal(job.triggeredJobs(), 0);
test.done();
}, 5000);
clock.tick(5000);
}
},
"When invoked": {

@@ -452,2 +475,17 @@ "Job emits 'run' event": function(test) {

clock.tick(3250);
},
"Job counter increase properly": function(test) {
var job = new schedule.Job(function() {});
job.schedule({
second: null // fire every second
});
setTimeout(function() {
job.cancel();
test.equal(job.triggeredJobs(), 2);
test.done();
}, 2250);
clock.tick(2250);
}

@@ -454,0 +492,0 @@ },

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc