Skip to content

Commit

Permalink
adding save and read job data from redis
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Apr 14, 2015
1 parent 4213ebc commit f6954ec
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 6 deletions.
86 changes: 80 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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();
}

/**
Expand All @@ -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
Expand All @@ -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...
});
};

/**
Expand Down
43 changes: 43 additions & 0 deletions test/capability.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

});

});

0 comments on commit f6954ec

Please sign in to comment.