Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kelektiv/node-cron
Browse files Browse the repository at this point in the history
  • Loading branch information
intcreator committed May 24, 2023
2 parents c44ab3f + 659510c commit 0dd0abc
Show file tree
Hide file tree
Showing 9 changed files with 3,367 additions and 6,186 deletions.
19 changes: 9 additions & 10 deletions .eslintrc
@@ -1,5 +1,12 @@
{
"extends": ["standard", "prettier", "prettier/standard"],
"extends": [
"standard",
"plugin:jest/recommended",
"plugin:import/recommended",
"plugin:n/recommended",
"plugin:prettier/recommended",
"plugin:promise/recommended"
],
"parserOptions": {
"ecmaVersion": 2018
},
Expand All @@ -12,15 +19,7 @@
"node": true,
"jest/globals": true
},
"plugins": ["prettier", "standard", "jest"],
"rules": {
"space-before-function-paren": 0,
"new-cap": 0,
"prettier/prettier": 2,
"jest/no-disabled-tests": 1,
"jest/no-focused-tests": 2,
"jest/no-identical-title": 2,
"jest/prefer-to-have-length": 1,
"jest/valid-expect": 2
"jest/no-done-callback": "off"
}
}
7 changes: 3 additions & 4 deletions README.md
Expand Up @@ -44,11 +44,10 @@ var job = new CronJob(
true,
'America/Los_Angeles'
);
// Use this if the 4th param is default value(false)
// job.start()
// job.start() - See note below when to use this
```

Note - You don't need to explicitly start a job in order to make it run since the 4th parameter is set to `true`. However, by default you need to call `job.start()` to start the cron job, which gives a little more control over running your jobs.
Note - In the example above, the 4th parameter of `CronJob()` automatically starts the job on initialization. If this parameter is falsy or not provided, the job needs to be explicitly started using `job.start()`.

There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/master/examples)

Expand Down Expand Up @@ -132,4 +131,4 @@ This is a community effort project. In the truest sense, this project started as

## License

MIT
MIT
4 changes: 2 additions & 2 deletions lib/cron.js
Expand Up @@ -7,8 +7,8 @@
root.Cron = factory(root.luxon);
}
})(this, function (luxon, childProcess) {
var exports = {};
var spawn = childProcess && childProcess.spawn;
const exports = {};
const spawn = childProcess && childProcess.spawn;
const CronTime = require('./time')(luxon);
const CronJob = require('./job')(CronTime, spawn);

Expand Down
37 changes: 19 additions & 18 deletions lib/job.js
@@ -1,7 +1,7 @@
function CronJob(CronTime, spawn) {
function fnWrap(cmd) {
var command;
var args;
let command;
let args;

switch (typeof cmd) {
case 'string':
Expand All @@ -14,7 +14,7 @@ function CronJob(CronTime, spawn) {
command = cmd && cmd.command;
if (command) {
args = cmd.args;
var options = cmd.options;
const options = cmd.options;

return spawn.bind(undefined, command, args, options);
}
Expand All @@ -35,9 +35,9 @@ function CronJob(CronTime, spawn) {
utcOffset,
unrefTimeout
) {
var _cronTime = cronTime;
var argCount = 0;
for (var i = 0; i < arguments.length; i++) {
let _cronTime = cronTime;
let argCount = 0;
for (let i = 0; i < arguments.length; i++) {
if (arguments[i] !== undefined) {
argCount++;
}
Expand Down Expand Up @@ -76,7 +76,7 @@ function CronJob(CronTime, spawn) {
return this;
}

var addCallback = function (callback) {
const addCallback = function (callback) {
if (typeof callback === 'function') {
this._callbacks.push(callback);
}
Expand All @@ -88,17 +88,18 @@ function CronJob(CronTime, spawn) {
// crontime is an object...
throw new Error('time must be an instance of CronTime.');
}
const wasRunning = this.running;
this.stop();
this.cronTime = time;
this.start();
if (wasRunning) this.start();
};

CJ.prototype.nextDate = function () {
return this.cronTime.sendAt();
};

var fireOnTick = function () {
for (var i = this._callbacks.length - 1; i >= 0; i--) {
const fireOnTick = function () {
for (let i = this._callbacks.length - 1; i >= 0; i--) {
this._callbacks[i].call(this.context, this.onComplete);
}
};
Expand All @@ -108,16 +109,16 @@ function CronJob(CronTime, spawn) {
return this.cronTime.sendAt(i);
};

var start = function () {
const start = function () {
if (this.running) {
return;
}

var MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
var self = this;
var timeout = this.cronTime.getTimeout();
var remaining = 0;
var startTime;
const MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
const self = this;
let timeout = this.cronTime.getTimeout();
let remaining = 0;
let startTime;

if (this.cronTime.realDate) {
this.runOnce = true;
Expand All @@ -134,10 +135,10 @@ function CronJob(CronTime, spawn) {
// The callback wrapper checks if it needs to sleep another period or not
// and does the real callback logic when it's time.
function callbackWrapper() {
var diff = startTime + timeout - Date.now();
const diff = startTime + timeout - Date.now();

if (diff > 0) {
var newTimeout = self.cronTime.getTimeout();
let newTimeout = self.cronTime.getTimeout();

if (newTimeout > diff) {
newTimeout = diff;
Expand Down
78 changes: 39 additions & 39 deletions lib/time.js
Expand Up @@ -70,7 +70,7 @@ function CronTime(luxon) {
this.source = source;

if (zone) {
const dt = luxon.DateTime.fromObject({}, { zone: zone });
const dt = luxon.DateTime.fromObject({}, { zone });
if (dt.invalid) {
throw new Error('Invalid timezone.');
}
Expand All @@ -82,8 +82,8 @@ function CronTime(luxon) {
this.utcOffset = utcOffset;
}

var that = this;
TIME_UNITS.map(timeUnit => {
const that = this;
TIME_UNITS.forEach(timeUnit => {
that[timeUnit] = {};
});

Expand All @@ -103,19 +103,19 @@ function CronTime(luxon) {
* Ensure that the syntax parsed correctly and correct the specified values if needed.
*/
_verifyParse: function () {
var months = Object.keys(this.month);
var dom = Object.keys(this.dayOfMonth);
var ok = false;
const months = Object.keys(this.month);
const dom = Object.keys(this.dayOfMonth);
let ok = false;

/* if a dayOfMonth is not found in all months, we only need to fix the last
wrong month to prevent infinite loop */
var lastWrongMonth = NaN;
for (var i = 0; i < months.length; i++) {
var m = months[i];
var con = MONTH_CONSTRAINTS[parseInt(m, 10)];
let lastWrongMonth = NaN;
for (let i = 0; i < months.length; i++) {
const m = months[i];
const con = MONTH_CONSTRAINTS[parseInt(m, 10)];

for (var j = 0; j < dom.length; j++) {
var day = dom[j];
for (let j = 0; j < dom.length; j++) {
const day = dom[j];
if (day <= con) {
ok = true;
}
Expand All @@ -130,12 +130,12 @@ function CronTime(luxon) {

// infinite loop detected (dayOfMonth is not found in all months)
if (!ok) {
var notOkCon = MONTH_CONSTRAINTS[parseInt(lastWrongMonth, 10)];
for (var k = 0; k < dom.length; k++) {
var notOkDay = dom[k];
const notOkCon = MONTH_CONSTRAINTS[parseInt(lastWrongMonth, 10)];
for (let k = 0; k < dom.length; k++) {
const notOkDay = dom[k];
if (notOkDay > notOkCon) {
delete this.dayOfMonth[notOkDay];
var fixedDay = Number(notOkDay) % notOkCon;
const fixedDay = Number(notOkDay) % notOkCon;
this.dayOfMonth[fixedDay] = true;
}
}
Expand All @@ -146,7 +146,7 @@ function CronTime(luxon) {
* Calculate the "next" scheduled time
*/
sendAt: function (i) {
var date = this.realDate ? this.source : luxon.DateTime.local();
let date = this.realDate ? this.source : luxon.DateTime.local();
if (this.zone) {
date = date.setZone(this.zone);
}
Expand Down Expand Up @@ -185,7 +185,7 @@ function CronTime(luxon) {
return this._getNextDateFrom(date);
} else {
// return the next schedule times
var dates = [];
const dates = [];
for (; i > 0; i--) {
date = this._getNextDateFrom(date);
dates.push(date);
Expand Down Expand Up @@ -213,7 +213,7 @@ function CronTime(luxon) {
* Json representation of the parsed cron syntax.
*/
toJSON: function () {
var self = this;
const self = this;
return TIME_UNITS.map(function (timeName) {
return self._wcOrAll(timeName);
});
Expand Down Expand Up @@ -244,8 +244,8 @@ function CronTime(luxon) {
if (start instanceof Date) {
start = luxon.DateTime.fromJSDate(start);
}
var date = start;
var firstDate = start.toMillis();
let date = start;
const firstDate = start.toMillis();
if (zone) {
date = date.setZone(zone);
}
Expand All @@ -262,10 +262,10 @@ function CronTime(luxon) {
// it shouldn't take more than 5 seconds to find the next execution time
// being very generous with this. Throw error if it takes too long to find the next time to protect from
// infinite loop.
var timeout = Date.now() + 5000;
const timeout = Date.now() + 5000;
// determine next date
while (true) {
var diff = date - start;
const diff = date - start;

// hard stop if the current date is after the expected execution
if (Date.now() > timeout) {
Expand Down Expand Up @@ -661,18 +661,18 @@ function CronTime(luxon) {
return '*';
}

var all = [];
for (var time in this[type]) {
const all = [];
for (const time in this[type]) {
all.push(time);
}

return all.join(',');
},

_hasAll: function (type) {
var constraints = CONSTRAINTS[TIME_UNITS.indexOf(type)];
const constraints = CONSTRAINTS[TIME_UNITS.indexOf(type)];

for (var i = constraints[0], n = constraints[1]; i < n; i++) {
for (let i = constraints[0], n = constraints[1]; i < n; i++) {
if (!(i in this[type])) {
return false;
}
Expand Down Expand Up @@ -707,7 +707,7 @@ function CronTime(luxon) {
throw new Error(`Unknown alias: ${alias}`);
});

var units = source.trim().split(/\s+/);
const units = source.trim().split(/\s+/);

// seconds are optional
if (units.length < TIME_UNITS_LEN - 1) {
Expand All @@ -718,12 +718,12 @@ function CronTime(luxon) {
throw new Error('Too many fields');
}

var unitsLen = units.length;
for (var i = 0; i < TIME_UNITS_LEN; i++) {
const unitsLen = units.length;
for (let i = 0; i < TIME_UNITS_LEN; i++) {
// If the split source string doesn't contain all digits,
// assume defaults for first n missing digits.
// This adds support for 5-digit standard cron syntax
var cur = units[i - (TIME_UNITS_LEN - unitsLen)] || PARSE_DEFAULTS[i];
const cur = units[i - (TIME_UNITS_LEN - unitsLen)] || PARSE_DEFAULTS[i];
this._parseField(cur, TIME_UNITS[i], CONSTRAINTS[i]);
}
},
Expand All @@ -739,14 +739,14 @@ function CronTime(luxon) {
* - Starting with the lower bounds of the range iterate by step up to the upper bounds and toggle the CronTime field value flag on.
*/
_parseField: function (value, type, constraints) {
var typeObj = this[type];
var pointer;
var low = constraints[0];
var high = constraints[1];
const typeObj = this[type];
let pointer;
const low = constraints[0];
const high = constraints[1];

var fields = value.split(',');
const fields = value.split(',');
fields.forEach(field => {
var wildcardIndex = field.indexOf('*');
const wildcardIndex = field.indexOf('*');
if (wildcardIndex !== -1 && wildcardIndex !== 0) {
throw new Error(
`Field (${field}) has an invalid wildcard expression`
Expand All @@ -758,9 +758,9 @@ function CronTime(luxon) {
value = value.replace(RE_WILDCARDS, `${low}-${high}`);

// commas separate information, so split based on those
var allRanges = value.split(',');
const allRanges = value.split(',');

for (var i = 0; i < allRanges.length; i++) {
for (let i = 0; i < allRanges.length; i++) {
if (allRanges[i].match(RE_RANGE)) {
allRanges[i].replace(RE_RANGE, ($0, lower, upper, step) => {
lower = parseInt(lower, 10);
Expand Down

0 comments on commit 0dd0abc

Please sign in to comment.