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

Refactor scheduler and test files to handle missed executions #410

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "node-cron",
"version": "3.0.3",
"version": "3.0.4",
"description": "A simple cron-like task scheduler for Node.js",
"author": "Lucas Merencia",
"license": "ISC",
Expand Down
13 changes: 7 additions & 6 deletions src/scheduler.js
Expand Up @@ -15,21 +15,22 @@ class Scheduler extends EventEmitter{
this.stop();

let lastCheck = process.hrtime();
let lastExecution = this.timeMatcher.apply(new Date());
let lastExecution = this.timeMatcher.apply(new Date()).getTime();

const matchTime = () => {
const delay = 1000;
const now = new Date().getTime();
const elapsedTime = process.hrtime(lastCheck);
const elapsedMs = (elapsedTime[0] * 1e9 + elapsedTime[1]) / 1e6;
const missedExecutions = Math.floor(elapsedMs / 1000);

for(let i = missedExecutions; i >= 0; i--){
const date = new Date(new Date().getTime() - i * 1000);
const date = new Date(now - i * 1000);
let date_tmp = this.timeMatcher.apply(date);
if(lastExecution.getTime() < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)){
date_tmp.setMilliseconds(0);
if(lastExecution < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)){
lastExecution = date_tmp.getTime();
this.emit('scheduled-time-matched', date_tmp);
date_tmp.setMilliseconds(0);
lastExecution = date_tmp;
}
}
lastCheck = process.hrtime();
Expand Down
52 changes: 29 additions & 23 deletions test/node-cron-test.js
@@ -1,28 +1,29 @@
const { assert } = require('chai');
const sinon = require('sinon');
const cron = require('../src/node-cron');
const Scheduler = require('../src/scheduler')

describe('node-cron', () => {
beforeEach(() => {
this.clock = sinon.useFakeTimers(new Date(2018, 0, 1, 0, 0, 0, 0));
});

afterEach(() => {
this.clock.restore();
});

describe('schedule', () => {
it('should schedule a task', () => {
let executed = 0;
cron.schedule('* * * * * *', () => {
executed += 1;
});

this.clock.tick(2000);

assert.equal(2, executed);
});

it('should schedule a task with America/Sao_Paulo timezone', (done) => {
let startDate = new Date('Thu, 20 Sep 2018 00:00:00.000Z');
this.clock.restore();
Expand All @@ -40,7 +41,7 @@ describe('node-cron', () => {
});
this.clock.tick(1000);
});

it('should schedule a task with Europe/Rome timezone', (done) => {
let startDate = new Date('Thu, 20 Sep 2018 00:00:00.000Z');
this.clock.restore();
Expand All @@ -58,52 +59,57 @@ describe('node-cron', () => {
});
this.clock.tick(1000);
});

it('should schedule a task stoped', () => {
let executed = 0;
cron.schedule('* * * * * *', () => {
executed += 1;
}, { scheduled: false });

this.clock.tick(2000);

assert.equal(0, executed);
});

it('should start a stoped task', () => {
let executed = 0;
let scheduledTask = cron.schedule('* * * * * *', () => {
executed += 1;
}, { scheduled: false });

this.clock.tick(2000);
assert.equal(0, executed);
scheduledTask.start();
this.clock.tick(2000);
assert.equal(2, executed);
});

it('should recover missed executions', (done) => {
let executed = 0;
this.clock.restore();
let scheduledTask = cron.schedule('* * * * * *', () => {
executed += 1;
}, { recoverMissedExecutions: true });

let wait = true;

let startedAt = new Date();

let nextSec = new Date(startedAt.getTime() + 1000);
nextSec.setMilliseconds(0); // reset to the beginning of the next second
let waitingTime = nextSec.getTime() - (new Date().getTime());
let wait = true;
// waiting time until next second + 1 second
// So it will pass the ignored current second and miss exactly one execution of the next second

while(wait){
if((new Date().getTime() - startedAt.getTime()) > 1000){
if((new Date().getTime() - startedAt.getTime()) > waitingTime + 1000){
wait = false;
}
}

setTimeout(() => {
scheduledTask.stop();
assert.equal(2, executed);
done();
}, 1000);
}, 10);
}).timeout(4000);

it('should schedule a background task', () => {
Expand All @@ -114,14 +120,14 @@ describe('node-cron', () => {
task.stop();
});
});

describe('validate', () => {
it('should validate a pattern', () => {
assert.isTrue(cron.validate('* * * * * *'));
assert.isTrue(cron.validate('* * * * * *'));
});

it('should fail with a invalid pattern', () => {
assert.isFalse(cron.validate('62 * * * * *'));
assert.isFalse(cron.validate('62 * * * * *'));
});
});

Expand All @@ -135,4 +141,4 @@ describe('node-cron', () => {
assert.lengthOf(cron.getTasks(), 1);
});
});
});
});
19 changes: 12 additions & 7 deletions test/scheduler-test.js
Expand Up @@ -49,11 +49,16 @@ describe('Scheduler', () => {
emited += 1;
});
scheduler.start();
let wait = true;
let startedAt = new Date();

let nextSec = new Date(startedAt.getTime() + 1000);
nextSec.setMilliseconds(0); // reset to the beginning of the next second
let waitingTime = nextSec.getTime() - (new Date().getTime());
let wait = true;
// waiting time until next second + 1 second
// So it will pass the ignored current second and miss exactly one execution of the next second

while(wait){
if((new Date().getTime() - startedAt.getTime()) > 1000){
if((new Date().getTime() - startedAt.getTime()) > waitingTime + 1000){
wait = false;
}
}
Expand All @@ -62,8 +67,8 @@ describe('Scheduler', () => {
scheduler.stop();
assert.equal(2, emited);
done();
}, 1000);
}).timeout(3000);
}, 10);
}).timeout(4000);

it('should ignore missed executions', (done) => {
this.clock.restore();
Expand All @@ -75,7 +80,7 @@ describe('Scheduler', () => {
scheduler.start();
let wait = true;
let startedAt = new Date();

while(wait){
if((new Date().getTime() - startedAt.getTime()) > 1000){
wait = false;
Expand All @@ -88,4 +93,4 @@ describe('Scheduler', () => {
done();
}, 1000);
}).timeout(3000);
});
});