-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #78 : Add UnitTests for modules
Making adjustments to scheduler module to enable unit testing
- Loading branch information
Showing
2 changed files
with
48 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,52 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------------------ Dependencies | ||
|
||
var _ = require('lodash'); | ||
var nconf = require('nconf'); | ||
var schedule = require('node-schedule'); | ||
|
||
module.exports = (function() { | ||
// ------------------------------------------------------------------------------------------ Module definition | ||
|
||
var self = {}; | ||
var schedules = []; | ||
module.exports = new Scheduler(); | ||
|
||
self.add = function(name, cron, job) { | ||
if(!schedules[name]) { | ||
schedules[name] = schedule.scheduleJob(cron, job); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
function Scheduler() { | ||
this.schedules = []; | ||
}; | ||
|
||
Scheduler.prototype.get = function(name) { | ||
return this.schedules[name]; | ||
}; | ||
|
||
self.clear = function(name) { | ||
if(name) { | ||
schedules[name].cancel(); | ||
schedules = _.remove(schedules, schedules[name]); | ||
} else { | ||
_.each(schedules, function(schedule) { | ||
schedule.cancel(); | ||
}); | ||
schedules = Array(); | ||
} | ||
Scheduler.prototype.add = function(name, cron, job) { | ||
if(!this.schedules[name]) { | ||
this.schedules[name] = schedule.scheduleJob(name, cron, job); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
self.reschedule = function(name, cron, job) { | ||
if(schedules[name]) { | ||
self.clear(name); | ||
} | ||
return self.add(name, cron, job); | ||
Scheduler.prototype.remove = function(name) { | ||
this.clear(name); | ||
}; | ||
|
||
Scheduler.prototype.clear = function(name) { | ||
if(name) { | ||
this.schedules[name].cancel(); | ||
this.schedules = _.remove(this.schedules, this.schedules[name]); | ||
} else { | ||
_.each(this.schedules, function(schedule) { | ||
schedule.cancel(); | ||
}); | ||
this.schedules = Array(); | ||
} | ||
return true; | ||
}; | ||
|
||
return self; | ||
})() | ||
Scheduler.prototype.reschedule = function(name, cron, job) { | ||
if(this.schedules[name]) { | ||
this.clear(name); | ||
} | ||
return this.add(name, cron, job); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters