Skip to content

Commit

Permalink
Temporary fix #5
Browse files Browse the repository at this point in the history
Need more hack…
  • Loading branch information
soplwang committed Oct 17, 2013
1 parent 1220c42 commit 3582c0b
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/tasks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* tasks.js */

// BUG: CHECK epoch, if 0, may means not-defined...

var events = require('events');
var util = require('util');
var crypto = require('crypto');
Expand Down Expand Up @@ -130,14 +132,19 @@ function Task(task) {
if (!this.hasOwnProperty('timeout')) this.timeout = 15;
if (!this.hasOwnProperty('retry')) this.retry = 1;

if (typeof this.cron === 'number' && this.cron < 1000000)
this.cron = (this.cron || 1) + _Now();
if (typeof this.cron === 'number')
this.cron = _Epoch(this.cron || 1);
if (typeof this.cron === 'number' && !this.hasOwnProperty('drop'))
this.drop = this.cron + 86400; // 1d
if (typeof this.drop === 'number' && this.drop < 1000000)
this.drop += _Now();
if (typeof this.patch === 'string')
this.patch = [this.patch];
if (this.hasOwnProperty('drop'))
this.drop = _Epoch(this.drop);
if (this.hasOwnProperty('mask')) {
if (!Array.isArray(this.mask)) this.mask = [this.mask];
this.mask[0] = _Epoch(this.mask[0]);
this.mask[1] = _Epoch(this.mask[1]);
}

if (typeof this.patch === 'string') this.patch = [this.patch];

if (!this.created) this.created = _Now();
if (!this.updated) this.updated = this.created;
Expand Down Expand Up @@ -166,6 +173,10 @@ Task.prototype.sched = function () {
var cron = once ? new Date(this.cron*1000) : this.cron;

this._cronJob = new CronJob(cron, function () {
if (Array.isArray(self.mask)) {
if (self.mask[0] && _Now() < self.mask[0]) return;
if (self.mask[1] && _Now() >= self.mask[1]) return;
}
if (!once)
self.run();
else
Expand Down Expand Up @@ -344,9 +355,9 @@ Task.prototype.toJSON = function () {
var self = this;
var o = {};

['name', 'url', 'callback', 'active', 'cron', 'drop',
['name', 'url', 'callback', 'active', 'cron', 'mask', 'drop',
'timeout', 'retry', 'urlTemplate', 'callbackTemplate',
'patches', 'hash', 'created', 'updated',
'patch', 'hash', 'created', 'updated',
'_running', '_counts', '_last'].forEach(function (k) {
o[k] = self[k];
});
Expand Down Expand Up @@ -417,3 +428,8 @@ function _Merge(obj, add) {
function _Now() {
return ~~(Date.now() / 1000);
}

function _Epoch(epo) {
if (typeof epo !== 'number') return;
return (epo < 1000000) ? (epo + _Now()) : epo;
}

0 comments on commit 3582c0b

Please sign in to comment.