Skip to content

Commit

Permalink
Merge 762b51d into d8e1da3
Browse files Browse the repository at this point in the history
  • Loading branch information
emhagman committed Jul 19, 2017
2 parents d8e1da3 + 762b51d commit 0b28e8a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
20 changes: 7 additions & 13 deletions lib/job.js
Expand Up @@ -3,7 +3,7 @@
* None
*/

var humanInterval = require('human-interval'), CronTime = require('cron').CronTime, date = require('date.js'),
var humanInterval = require('human-interval'), cronParser = require('cron-parser'), date = require('date.js'),
moment = require('moment-timezone');

var Job = module.exports = function Job(args) {
Expand Down Expand Up @@ -74,27 +74,21 @@ Job.prototype.computeNextRunAt = function() {
}
return this;

function dateForTimezone(d) {
d = moment(d);
if (timezone) {
d.tz(timezone);
}
return d;
}

/**
* Internal method that computes the interval
* @returns {undefined}
*/
function computeFromInterval() {
var lastRun = this.attrs.lastRunAt || new Date();
lastRun = dateForTimezone(lastRun);
var cronOptions = { currentDate: lastRun };
if (timezone) cronOptions.tz = timezone;
try {
var cronTime = new CronTime(interval);
var nextDate = cronTime._getNextDateFrom(lastRun);
var cronTime = cronParser.parseExpression(interval, cronOptions);
var nextDate = cronTime.next()._date.toDate();
if (nextDate.valueOf() === lastRun.valueOf()) {
// Handle cronTime giving back the same date for the next run time
nextDate = cronTime._getNextDateFrom(dateForTimezone(new Date(lastRun.valueOf() + 1000)));
cronOptions.currentDate = new Date(lastRun.valueOf() + 1000);
nextDate = cronParser.parseExpression(interval, cronOptions);
}
this.attrs.nextRunAt = nextDate;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/agenda/agenda/issues"
},
"dependencies": {
"cron": "~1.1.0",
"cron-parser": "^2.4.1",
"date.js": "~0.3.1",
"human-interval": "~0.1.3",
"moment-timezone": "^0.5.0",
Expand Down
11 changes: 11 additions & 0 deletions test/agenda.js
Expand Up @@ -582,6 +582,17 @@ describe("agenda", function() {
expect(job.attrs.nextRunAt.valueOf()).to.be(now.valueOf() + 60000);
});

it('understands cron intervals, specifically months (OBO)', function() {
// https://github.com/agenda/agenda/issues/358
// ensure CRON months are properly parsed
var now = new Date('2017-08-04 12:00:00');
job.attrs.lastRunAt = now;
job.repeatEvery('0 12 4 8 *');
job.computeNextRunAt();
expect(moment(job.attrs.nextRunAt).toDate().getMonth()).to.be(now.getMonth());
expect(moment(job.attrs.nextRunAt).toDate().getFullYear()).to.be(now.getFullYear() + 1);
});

it('understands cron intervals with a timezone', function () {
var date = new Date('2015-01-01T06:01:00-00:00');
job.attrs.lastRunAt = date;
Expand Down
46 changes: 24 additions & 22 deletions yarn.lock
Expand Up @@ -262,11 +262,12 @@ coveralls@^2.13.1:
minimist "1.2.0"
request "2.79.0"

cron@~1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/cron/-/cron-1.1.1.tgz#02719d4ef480dfc8ee24d81a3603460ba39013ce"
cron-parser@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/cron-parser/-/cron-parser-2.4.1.tgz#022befce1af293e4d3144ff04c2cbd2edb491271"
dependencies:
moment-timezone "~0.5.5"
is-nan "^1.2.1"
moment-timezone "^0.5.0"

cryptiles@2.x.x:
version "2.0.5"
Expand Down Expand Up @@ -313,6 +314,13 @@ deep-is@~0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"

define-properties@^1.1.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.2.tgz#83a73f2fea569898fb737193c8f873caf6d45c94"
dependencies:
foreach "^2.0.5"
object-keys "^1.0.8"

del@^2.0.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/del/-/del-2.2.2.tgz#c12c981d067846c84bcaf862cff930d907ffd1a8"
Expand Down Expand Up @@ -646,7 +654,7 @@ inflight@^1.0.4:
once "^1.3.0"
wrappy "1"

inherits@2, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3:
inherits@2, inherits@^2.0.3, inherits@~2.0.1:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

Expand Down Expand Up @@ -682,6 +690,12 @@ is-my-json-valid@^2.12.4:
jsonpointer "^4.0.0"
xtend "^4.0.0"

is-nan@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/is-nan/-/is-nan-1.2.1.tgz#9faf65b6fb6db24b7f5c0628475ea71f988401e2"
dependencies:
define-properties "^1.1.1"

is-path-cwd@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-path-cwd/-/is-path-cwd-1.0.0.tgz#d225ec23132e89edd38fda767472e62e65f1106d"
Expand Down Expand Up @@ -941,7 +955,7 @@ mocha@^3.4.2:
mkdirp "0.5.1"
supports-color "3.1.2"

moment-timezone@^0.5.0, moment-timezone@~0.5.5:
moment-timezone@^0.5.0:
version "0.5.13"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.13.tgz#99ce5c7d827262eb0f1f702044177f60745d7b90"
dependencies:
Expand Down Expand Up @@ -990,7 +1004,7 @@ object-assign@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"

object-keys@^1.0.6:
object-keys@^1.0.6, object-keys@^1.0.8:
version "1.0.11"
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d"

Expand Down Expand Up @@ -1071,7 +1085,7 @@ qs@~6.3.0:
version "6.3.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.3.2.tgz#e75bd5f6e268122a2a0e0bda630b2550c166502c"

readable-stream@2.2.7:
readable-stream@2.2.7, readable-stream@^2.2.2:
version "2.2.7"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.7.tgz#07057acbe2467b22042d36f98c5ad507054e95b1"
dependencies:
Expand All @@ -1083,18 +1097,6 @@ readable-stream@2.2.7:
string_decoder "~1.0.0"
util-deprecate "~1.0.1"

readable-stream@^2.2.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.3"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
safe-buffer "~5.1.1"
string_decoder "~1.0.3"
util-deprecate "~1.0.1"

request@2.79.0:
version "2.79.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.79.0.tgz#4dfe5bf6be8b8cdc37fcf93e04b65577722710de"
Expand Down Expand Up @@ -1171,7 +1173,7 @@ rx-lite@*, rx-lite@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-4.0.8.tgz#0b1e11af8bc44836f04a6407e92da42467b79444"

safe-buffer@~5.1.0, safe-buffer@~5.1.1:
safe-buffer@~5.1.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"

Expand Down Expand Up @@ -1218,7 +1220,7 @@ string-width@^2.0.0, string-width@^2.1.0:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"

string_decoder@~1.0.0, string_decoder@~1.0.3:
string_decoder@~1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab"
dependencies:
Expand Down

0 comments on commit 0b28e8a

Please sign in to comment.