Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace cron with cron parser #16

Merged
merged 3 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10,732 changes: 10,564 additions & 168 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
"url": "https://github.com/hokify/agenda/issues"
},
"dependencies": {
"cron": "~1.8.2",
"cron-parser": "^2.18.0",
"date.js": "~0.3.3",
"debug": "~4.2.0",
"debug": "~4.3.1",
"human-interval": "~2.0.0",
"moment-timezone": "~0.5.31",
"mongodb": "~3.6.2"
"moment-timezone": "~0.5.32",
"mongodb": "~3.6.3"
},
"devDependencies": {
"@hokify/eslint-config": "^0.5.10",
Expand All @@ -67,24 +67,24 @@
"@types/mocha": "^8.0.3",
"@types/mongodb": "^3.5.29",
"@types/node": "^14.14.0",
"@types/sinon": "^9.0.8",
"@types/sinon": "^9.0.9",
"better-docs": "^2.3.2",
"blanket": "1.2.3",
"chai": "^4.2.0",
"coveralls": "3.1.0",
"delay": "4.4.0",
"eslint": "7.11.0",
"better-docs": "^2.3.2",
"eslint": "7.15.0",
"jsdoc": "3.6.6",
"mocha": "8.2.0",
"mocha-lcov-reporter": "1.3.0",
"mongodb-memory-server": "^6.9.2",
"nyc": "^15.1.0",
"typedoc": "^0.19.2",
"prettier": "^2.1.2",
"q": "1.5.1",
"sinon": "9.2.0",
"standard-version": "^9.0.0",
"ts-node": "^9.0.0",
"typedoc": "^0.19.2",
"typescript": "^4.0.3"
}
}
2 changes: 1 addition & 1 deletion src/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export class Job<DATA = unknown | void> {

if (definition.fn.length === 2) {
log('[%s:%s] process function being called', this.attrs.name, this.attrs._id);
await new Promise((resolve, reject) => {
await new Promise<void>((resolve, reject) => {
try {
const result = definition.fn(this, error => {
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/JobProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ export class JobProcessor {
// check if the job is still alive
const checkIfJobIsStillAlive = () => {
// check every "this.agenda.definitions[job.attrs.name].lockLifetime / 2"" (or at mininum every processEvery)
return new Promise((resolve, reject) =>
return new Promise<void>((resolve, reject) =>
setTimeout(async () => {
// when job is not running anymore, just finish
if (!(await job.isRunning())) {
Expand Down
4 changes: 4 additions & 0 deletions src/types/ArgumentsType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export type ArgumentsType<T extends (...args: any[]) => any> = T extends (...args: infer A) => any
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
? A
: never;
24 changes: 17 additions & 7 deletions src/utils/nextRunAt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CronTime } from 'cron';
import * as moment from 'moment-timezone';
import * as humanInterval from 'human-interval';
import * as date from 'date.js';
import * as debug from 'debug';
import type { IJobParameters } from '../types/JobParameters';
import { parseExpression } from 'cron-parser';
import { isValidDate } from './isValidDate';
import type { IJobParameters } from '../types/JobParameters';
import type { ArgumentsType } from '../types/ArgumentsType';

const log = debug('agenda:nextRunAt');

Expand All @@ -28,21 +29,30 @@ export function isValidHumanInterval(value: unknown): value is string {
export const computeFromInterval = (attrs: IJobParameters): Date => {
const previousNextRunAt = attrs.nextRunAt || new Date();
log('[%s:%s] computing next run via interval [%s]', attrs.name, attrs._id, attrs.repeatInterval);

const lastRun = dateForTimezone(attrs.lastRunAt || new Date(), attrs.repeatTimezone);

const cronOptions: ArgumentsType<typeof parseExpression>[1] = {
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
currentDate: lastRun.toDate()
};

if (attrs.repeatTimezone) {
simllll marked this conversation as resolved.
Show resolved Hide resolved
cronOptions.tz = attrs.repeatTimezone;
}
let nextRunAt: Date | null = null;

if (typeof attrs.repeatInterval === 'string') {
try {
const cronTime = new CronTime(attrs.repeatInterval);
let nextDate: Date = cronTime._getNextDateFrom(lastRun);
let cronTime = parseExpression(attrs.repeatInterval, cronOptions);
let nextDate = cronTime.next().toDate();
if (
nextDate.valueOf() === lastRun.valueOf() ||
nextDate.valueOf() <= previousNextRunAt.valueOf()
) {
// Handle cronTime giving back the same date for the next run time
nextDate = cronTime._getNextDateFrom(
dateForTimezone(new Date(lastRun.valueOf() + 1000), attrs.repeatTimezone)
);
cronOptions.currentDate = new Date(lastRun.valueOf() + 1000);
cronTime = parseExpression(attrs.repeatInterval, cronOptions);
nextDate = cronTime.next().toDate();
}

nextRunAt = nextDate;
Expand Down
29 changes: 24 additions & 5 deletions test/job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Db } from 'mongodb';
import * as Q from 'q';
import * as delay from 'delay';
import * as sinon from 'sinon';
import { fail } from 'assert';
import { Job } from '../src/Job';
import { Agenda } from '../src';
import { mockMongo } from './helpers/mock-mongodb';
Expand All @@ -28,7 +29,7 @@ const clearJobs = async () => {
// Slow timeouts for Travis
const jobTimeout = 500;
const jobType = 'do work';
const jobProcessor = () => {};
const jobProcessor = () => { };

describe('Job', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -286,6 +287,24 @@ describe('Job', () => {
expect(job.attrs.nextRunAt.valueOf()).to.equal(expectedDate.valueOf());
});

it('cron job with month starting at 1', async () => {
job.repeatEvery('0 0 * 1 *', {
timezone: 'GMT'
});
if (job.attrs.nextRunAt) {
expect(job.attrs.nextRunAt.getMonth()).to.equal(0);
} else {
fail();
}
});

it('repeating job with cron', async () => {
job.repeatEvery('0 0 * 1 *', {
timezone: 'GMT'
});
expect(job.attrs.nextRunAt).to.not.eql(null);
});

describe('when repeat at time is invalid', () => {
beforeEach(() => {
job.attrs.repeatAt = 'foo';
Expand Down Expand Up @@ -664,7 +683,7 @@ describe('Job', () => {
).to.eq('processed');

await agenda.stop();
const processedStopped = new Promise(resolve => {
const processedStopped = new Promise<void>(resolve => {
agenda.define('jobQueueTest', async _job => {
resolve();
});
Expand Down Expand Up @@ -944,7 +963,7 @@ describe('Job', () => {
});

it('does not on-the-fly lock more than definition.lockLimit jobs', async () => {
agenda.define('lock job', (job, cb) => {}, { lockLimit: 1 }); // eslint-disable-line no-unused-vars
agenda.define('lock job', (job, cb) => { }, { lockLimit: 1 }); // eslint-disable-line no-unused-vars

await agenda.start();

Expand All @@ -958,7 +977,7 @@ describe('Job', () => {
agenda.lockLimit(1);
agenda.processEvery(200);

agenda.define('lock job', (job, cb) => {}); // eslint-disable-line no-unused-vars
agenda.define('lock job', (job, cb) => { }); // eslint-disable-line no-unused-vars

await agenda.start();

Expand All @@ -976,7 +995,7 @@ describe('Job', () => {
it('does not lock more than definition.lockLimit jobs during processing interval', async () => {
agenda.processEvery(200);

agenda.define('lock job', (job, cb) => {}, { lockLimit: 1 }); // eslint-disable-line no-unused-vars
agenda.define('lock job', (job, cb) => { }, { lockLimit: 1 }); // eslint-disable-line no-unused-vars

await agenda.start();

Expand Down
4 changes: 2 additions & 2 deletions test/jobprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('JobProcessor', () => {

await agenda.start();

const promiseResult = await new Promise(resolve => {
const promiseResult = await new Promise<Error | void>(resolve => {
agenda.on('error', err => {
resolve(err);
});
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('JobProcessor', () => {
// queue up long ones
agenda.now('test long');

const promiseResult = await new Promise(resolve => {
const promiseResult = await new Promise<Error | void>(resolve => {
agenda.on('error', err => {
resolve(err);
});
Expand Down