-
Notifications
You must be signed in to change notification settings - Fork 243
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
Changes from 3 commits
d586abe
9080420
58a49f0
bcc1d21
678c2cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ){ | ||
|
@@ -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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'self' used out of scope. |
||
} | ||
} | ||
}; | ||
|
||
return Task; | ||
}()); |
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'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing semicolon.