From 3582c0bcca1039c3f8220651c99da55e73528066 Mon Sep 17 00:00:00 2001 From: Sopl'Wang Date: Thu, 17 Oct 2013 18:49:41 +0800 Subject: [PATCH] Temporary fix #5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Need more hackā€¦ --- lib/tasks.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/tasks.js b/lib/tasks.js index 6726647..780c398 100644 --- a/lib/tasks.js +++ b/lib/tasks.js @@ -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'); @@ -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; @@ -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 @@ -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]; }); @@ -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; +}