diff --git a/index.js b/index.js index f7b59cf..b4085f7 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ var redis = kue.redis; var _ = require('lodash'); var async = require('async'); var datejs = require('date.js'); -// var uuid = require('node-uuid'); +var uuid = require('node-uuid'); /** * @constructor * @description A job scheduling utility for kue @@ -18,7 +18,7 @@ function KueScheduler(options) { //extend default configurations //with custom provided configurations //and reference them for later use - this.options = _.extend({ + this.options = _.merge({ redis: { port: 6379, host: '127.0.0.1' @@ -35,8 +35,10 @@ function KueScheduler(options) { //a redis client to listen for key expiry this.listener = redis.createClientFactory(this.options); - //subscribe to key expiration events - this.listener.subscribe('__keyevent@0__:expired'); + + //listen for job key expiry + //and schedule kue jobs to run + this._subscribe(); } /** @@ -48,6 +50,15 @@ KueScheduler.prototype._getJobExpiryKey = function(uuid) { return 'kue:scheduler:' + uuid; }; +/** + * @function + * @description generate job uuid from job expiry key + * @private + */ +KueScheduler.prototype._getJobUUID = function(jobExpiryKey) { + return jobExpiryKey.split(':')[2]; +}; + /** * @function * @description generate a storage key for the scheduled job data @@ -57,9 +68,72 @@ KueScheduler.prototype._getJobDataKey = function(uuid) { return 'kue:scheduler:data:' + uuid; }; +/** + * @function + * @description save job data into redis backend + * @private + */ +KueScheduler.prototype._saveJobData = function(jobDataKey, jobData, done) { + this.scheduler.hmset(jobDataKey, jobData, function(error, response) { + done(error, jobData, response); + }); +}; + +/** + * @function + * @description retrieved saved job data from redis backend + * @private + */ +KueScheduler.prototype._readJobData = function(jobDataKey, done) { + this.scheduler.hgetall(jobDataKey, function(error, data) { + done(error, data); + }); +}; + +KueScheduler.prototype._subscribe = function() { + var scheduler = this; + + //listen for job key expiry + this.listener.on('message', function(channel, jobExpiryKey) { + //get job uuid + scheduler._getJobUUID(jobExpiryKey); + + //get saved job data + + }); + + //subscribe to key expiration events + this.listener.subscribe('__keyevent@0__:expired'); + +}; + + +KueScheduler.prototype.every = function(interval, jobDefinition) { + var scheduler = this; + + //extend job definition with + //scheduling data + jobDefinition = _.merge(jobDefinition, { + data: { + schedule: 'RECCUR', + reccurInterval: interval + } + }); + + //generate job uuid + var jobUUID = uuid.v1(); + + async + .parallel({ + jobExpiryKey: function(next) { + next(null, scheduler._getJobExpiryKey(jobUUID)); + }, + jobDataKey: function(next) { + next(null, scheduler._getJobDataKey(jobUUID)); + } + }, function finish( /*error, results*/ ) { -KueScheduler.prototype.every = function( /*interval, jobDefinition*/ ) { - // body... + }); }; /** diff --git a/test/capability.spec.js b/test/capability.spec.js index d247434..ceb0206 100644 --- a/test/capability.spec.js +++ b/test/capability.spec.js @@ -52,4 +52,47 @@ describe('KueScheduler#Capability', function() { done(); }); + it('should be able to generate job uuid from job expriration key', function(done) { + var jobuuid = uuid.v1(); + var jobEpiryKey = kueScheduler._getJobExpiryKey(jobuuid); + + expect(kueScheduler._getJobUUID(jobEpiryKey)) + .to.be.equal(jobuuid); + + done(); + }); + + describe('KueScheduler#Capability#CRUD', function() { + var jobuuid; + var jobDataKey; + var jobData; + + before(function(done) { + jobuuid = uuid.v1(); + jobDataKey = kueScheduler._getJobDataKey(jobuuid); + jobData = { + uuid: jobuuid + }; + + done(); + }); + + it('should be able to save job data', function(done) { + kueScheduler + ._saveJobData(jobDataKey, jobData, function(error, _jobData) { + expect(_jobData.uuid).to.equal(jobData.uuid); + done(error, _jobData); + }); + }); + + it('should be able to read job data', function(done) { + kueScheduler + ._readJobData(jobDataKey, function(error, _jobData) { + expect(_jobData.uuid).to.equal(jobData.uuid); + done(error, _jobData); + }); + }); + + }); + }); \ No newline at end of file