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

Added getStatus to ScheduledTask #103

Merged
merged 5 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 24 additions & 1 deletion src/scheduled-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ module.exports = (function() {
* @param {*} options - task options.
*/
function ScheduledTask(task, options) {
var self = this;

var timezone = options.timezone;

task.on('started', function() {
self.status = 'running';
});

task.on('done', function() {
self.status = 'waiting';
});

task.on('failed', function() {
self.status = 'failed';
});

this.task = function () {
var date = new Date();
if(timezone){
Expand All @@ -35,6 +50,7 @@ module.exports = (function() {
* @returns {ScheduledTask} instance of this task.
*/
ScheduledTask.prototype.start = function() {
this.status = 'scheduled';
if (this.task && !this.tick) {
this.tick = setTimeout(this.task.bind(this), 1000);
}
Expand All @@ -48,6 +64,7 @@ module.exports = (function() {
* @returns {ScheduledTask} instance of this task.
*/
ScheduledTask.prototype.stop = function() {
this.status = 'stoped';
if (this.tick) {
clearTimeout(this.tick);
this.tick = null;
Expand All @@ -56,11 +73,17 @@ module.exports = (function() {
return this;
};


ScheduledTask.prototype.getStatus = function() {
return this.status;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.


/**
* Destoys the scheduled task.
* Destroys the scheduled task.
*/
ScheduledTask.prototype.destroy = function() {
this.stop();
this.status = 'destroyed';

this.task = null;
};
Expand Down
41 changes: 30 additions & 11 deletions src/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var convertExpression = require('./convert-expression');
var validatePattern = require('./pattern-validation');

var events = require('events');

module.exports = (function(){
function matchPattern(pattern, value){
if( pattern.indexOf(',') !== -1 ){
Expand All @@ -11,46 +13,63 @@ module.exports = (function(){
}
return pattern === value.toString();
}

function mustRun(task, date){
var runInSecond = matchPattern(task.expressions[0], date.getSeconds());
var runOnMinute = matchPattern(task.expressions[1], date.getMinutes());
var runOnHour = matchPattern(task.expressions[2], date.getHours());
var runOnDayOfMonth = matchPattern(task.expressions[3], date.getDate());
var runOnMonth = matchPattern(task.expressions[4], date.getMonth() + 1);
var runOnDayOfWeek = matchPattern(task.expressions[5], date.getDay());

var runOnDay = false;
var delta = task.initialPattern.length === 6 ? 0 : -1;

if (task.initialPattern[3 + delta] === '*') {
runOnDay = runOnDayOfWeek;
} else if (task.initialPattern[5 + delta] === '*') {
runOnDay = runOnDayOfMonth;
} else {
runOnDay = runOnDayOfMonth || runOnDayOfWeek;
}

return runInSecond && runOnMinute && runOnHour && runOnDay && runOnMonth;
}

function Task(pattern, execution){
validatePattern(pattern);
this.initialPattern = pattern.split(' ');
this.pattern = convertExpression(pattern);
this.execution = execution;
this.expressions = this.pattern.split(' ');
}

events.EventEmitter.call(this);
}

Task.prototype = events.EventEmitter.prototype;

Task.prototype.update = function(date){
if(mustRun(this, date)){
try {
this.execution();
} catch(err) {
console.error(err);
}
var self = this;
var execution = new Promise(function(resolve, reject){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Promise' is not defined.

self.emit('started', self);
var ex = self.execution();
if( execution instanceof Promise){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Promise' is not defined.

ex.then(resolve).catch(reject);
}
}).then(function(){
self.emit('done', self);
}).catch(function(error){
console.error(error);
self.emit('failed', error);
});
} catch (error) {
console.error(error);
self.emit('failed', error);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'self' used out of scope.

}
}
};

return Task;
}());
48 changes: 48 additions & 0 deletions test/scheduled-taks-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var expect = require('expect.js');
var sinon = require('sinon');
var ScheduledTask = require('../src/scheduled-task');
var Task = require('../src/task');

describe('ScheduledTask', function() {
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});

afterEach(function(){
this.clock.restore();
});

it('should return scheduled status', function() {
var task = new Task('* * * * *');
var scheduledTask = new ScheduledTask(task, {});
expect(scheduledTask.getStatus()).to.equal('scheduled');
});

it('should return running status', function(done) {
var task = new Task('* * * * * *', function(){});
var scheduledTask = new ScheduledTask(task, {});

task.on('started', function() {
expect(scheduledTask.getStatus()).to.equal('running');
done();
});

this.clock.tick(1100);
});

it('should return stoped status', function() {
var task = new Task('* * * * *');
var scheduledTask = new ScheduledTask(task, {});
scheduledTask.stop();
expect(scheduledTask.getStatus()).to.equal('stoped');
});

it('should return destroyed status', function() {
var task = new Task('* * * * *');
var scheduledTask = new ScheduledTask(task, {});
scheduledTask.destroy();
expect(scheduledTask.getStatus()).to.equal('destroyed');
});
});