kue-unique
Advanced tools
Comparing version 1.0.0 to 1.0.1
275
index.js
@@ -17,3 +17,3 @@ 'use strict'; | ||
var async = require('async'); | ||
var noop = function() {}; | ||
var noop = function () {}; | ||
@@ -27,4 +27,4 @@ | ||
*/ | ||
Job.getUniqueJobsKey = function() { | ||
return Job.client.getKey('unique:jobs'); | ||
Job.getUniqueJobsKey = function () { | ||
return Job.client.getKey('unique:jobs'); | ||
}; | ||
@@ -40,17 +40,25 @@ | ||
*/ | ||
Job.getUniqueJobsData = function(done) { | ||
Job.getUniqueJobsData = function (done) { | ||
var key = this.getUniqueJobsKey(); | ||
Job | ||
.client | ||
.hgetall(key, function(error, data) { | ||
var key = this.getUniqueJobsKey(); | ||
Job | ||
.client | ||
.hgetall(key, function (error, data) { | ||
//correct null | ||
if (_.isNull(data)) { | ||
data = {}; | ||
} | ||
//correct null | ||
if (_.isNull(data)) { | ||
data = {}; | ||
} else { | ||
//deserialize string to number | ||
//once fetched from redis | ||
_.forEach(data, function (value, key) { | ||
if (!isNaN(value)) { | ||
data[key] = parseInt(value); | ||
} | ||
}); | ||
} | ||
done(error, data); | ||
done(error, data); | ||
}); | ||
}); | ||
}; | ||
@@ -67,22 +75,22 @@ | ||
*/ | ||
Job.getUniqueJobData = function(unique, done) { | ||
Job.getUniqueJobData = function (unique, done) { | ||
var key = this.getUniqueJobsKey(); | ||
var key = this.getUniqueJobsKey(); | ||
Job.client.hget(key, unique, function(error, data) { | ||
//pick unique job data | ||
var uniqueJobData = {}; | ||
Job.client.hget(key, unique, function (error, data) { | ||
//pick unique job data | ||
var uniqueJobData = {}; | ||
if (data) { | ||
if (data) { | ||
if (!isNaN(data)) { | ||
data = parseInt(data); | ||
} | ||
if (!isNaN(data)) { | ||
data = parseInt(data); | ||
} | ||
uniqueJobData[unique] = data; | ||
} | ||
uniqueJobData[unique] = data; | ||
} | ||
done(null, uniqueJobData); | ||
done(null, uniqueJobData); | ||
}); | ||
}); | ||
@@ -100,33 +108,32 @@ }; | ||
*/ | ||
Job.removeUniqueJobData = function(id, done) { | ||
Job.removeUniqueJobData = function (id, done) { | ||
var key = Job.getUniqueJobsKey(); | ||
var key = Job.getUniqueJobsKey(); | ||
async.waterfall([ | ||
async.waterfall([ | ||
function loadUniqueJobsData(next) { | ||
Job.getUniqueJobsData(next); | ||
}, | ||
function loadUniqueJobsData(next) { | ||
Job.getUniqueJobsData(next); | ||
}, | ||
function dosave(uniqueJobsData, next) { | ||
//remove given job from unique job data | ||
//we have an id, lets find the key name. | ||
// | ||
var unique = Object.keys(uniqueJobsData).filter(function(key) { | ||
return uniqueJobsData[key] === id; | ||
})[0]; | ||
function dosave(uniqueJobsData, next) { | ||
//remove given job from unique job data | ||
//we have an id, lets find the key name. | ||
// | ||
var unique = Object.keys(uniqueJobsData).filter(function (key) { | ||
return uniqueJobsData[key] === id; | ||
})[0]; | ||
Job | ||
.client | ||
.hdel(key, unique, | ||
function(error /*, response*/ ) { | ||
next(error); | ||
}); | ||
}, | ||
Job | ||
.client | ||
.hdel(key, unique, function (error /*, response*/ ) { | ||
next(error); | ||
}); | ||
}, | ||
function reloadUniqueJobsData(next) { | ||
Job.getUniqueJobsData(next); | ||
} | ||
function reloadUniqueJobsData(next) { | ||
Job.getUniqueJobsData(next); | ||
} | ||
], done); | ||
], done); | ||
@@ -144,23 +151,23 @@ }; | ||
*/ | ||
Job.saveUniqueJobsData = function(uniqueJobData, done) { | ||
Job.saveUniqueJobsData = function (uniqueJobData, done) { | ||
var key = Job.getUniqueJobsKey(); | ||
var key = Job.getUniqueJobsKey(); | ||
var field; | ||
if (Object.keys(uniqueJobData).length > 0) { | ||
field = Object.keys(uniqueJobData)[0]; | ||
} | ||
var field; | ||
if (Object.keys(uniqueJobData).length > 0) { | ||
field = Object.keys(uniqueJobData)[0]; | ||
} | ||
Job | ||
.client | ||
.hset(key, field, uniqueJobData[field], function(error /*,response*/ ) { | ||
Job | ||
.client | ||
.hset(key, field, uniqueJobData[field], function (error /*,response*/ ) { | ||
if (error) { | ||
return done(error, null); | ||
} else { | ||
if (error) { | ||
return done(error, null); | ||
} else { | ||
Job.getUniqueJobsData(done); | ||
} | ||
Job.getUniqueJobsData(done); | ||
} | ||
}); | ||
}); | ||
@@ -176,10 +183,10 @@ }; | ||
*/ | ||
Job.prototype.unique = function(unique) { | ||
Job.prototype.unique = function (unique) { | ||
//extend job data with unique key | ||
_.merge(this.data || {}, { | ||
unique: unique | ||
}); | ||
//extend job data with unique key | ||
_.merge(this.data || {}, { | ||
unique: unique | ||
}); | ||
return this; | ||
return this; | ||
@@ -191,68 +198,68 @@ }; | ||
var previousSave = Job.prototype.save; | ||
Job.prototype.save = function(done) { | ||
//correct callback | ||
done = done || noop; | ||
Job.prototype.save = function (done) { | ||
//correct callback | ||
done = done || noop; | ||
//if job is unique | ||
var isUniqueJob = this.data && this.data.unique; | ||
if (isUniqueJob) { | ||
//check if it already exist | ||
async.waterfall([ | ||
//if job is unique | ||
var isUniqueJob = this.data && this.data.unique; | ||
if (isUniqueJob) { | ||
//check if it already exist | ||
async.waterfall([ | ||
function tryGetExistingJobData(next) { | ||
Job.getUniqueJobData(this.data.unique, next); | ||
}.bind(this), | ||
function tryGetExistingJobData(next) { | ||
Job.getUniqueJobData(this.data.unique, next); | ||
}.bind(this), | ||
function tryGetExistingOrSaveJob(uniqueJobData, next) { | ||
//try get existing job | ||
var exists = _.size(_.keys(uniqueJobData)) > 0; | ||
function tryGetExistingOrSaveJob(uniqueJobData, next) { | ||
//try get existing job | ||
var exists = _.size(_.keys(uniqueJobData)) > 0; | ||
if (exists) { | ||
//get existing job | ||
var id = _.first(_.values(uniqueJobData)); | ||
Job.get(id, function(error, job) { | ||
if (exists) { | ||
//get existing job | ||
var id = _.first(_.values(uniqueJobData)); | ||
Job.get(id, function (error, job) { | ||
//flag job as already exist | ||
if (job) { | ||
job.alreadyExist = true; | ||
} | ||
//flag job as already exist | ||
if (job) { | ||
job.alreadyExist = true; | ||
} | ||
next(error, job); | ||
next(error, job); | ||
}); | ||
} | ||
}); | ||
} | ||
//save a new job | ||
else { | ||
//save a new job | ||
else { | ||
previousSave.call(this, function(error) { | ||
next(error, this); | ||
}.bind(this)); | ||
previousSave.call(this, function (error) { | ||
next(error, this); | ||
}.bind(this)); | ||
} | ||
} | ||
}.bind(this), | ||
}.bind(this), | ||
function _saveUniqueJobsData(job, next) { | ||
//save job unique data | ||
var uniqueJobData = {}; | ||
function _saveUniqueJobsData(job, next) { | ||
//save job unique data | ||
var uniqueJobData = {}; | ||
uniqueJobData[job.data.unique] = job.id; | ||
uniqueJobData[job.data.unique] = job.id; | ||
Job.saveUniqueJobsData(uniqueJobData, function(error /*,uniqueJobsData*/ ) { | ||
next(error, job); | ||
}); | ||
} | ||
], function(error, job) { | ||
return done(error, job); | ||
Job.saveUniqueJobsData(uniqueJobData, function (error /*,uniqueJobsData*/ ) { | ||
next(error, job); | ||
}); | ||
} | ||
} | ||
], function (error, job) { | ||
return done(error, job); | ||
}); | ||
//otherwise save a job | ||
else { | ||
return previousSave.call(this, done); | ||
} | ||
} | ||
//otherwise save a job | ||
else { | ||
return previousSave.call(this, done); | ||
} | ||
}; | ||
@@ -263,21 +270,21 @@ | ||
var previousRemove = Job.prototype.remove; | ||
Job.prototype.remove = function(done) { | ||
//correct callback | ||
done = done || noop; | ||
Job.prototype.remove = function (done) { | ||
//correct callback | ||
done = done || noop; | ||
async.parallel({ | ||
async.parallel({ | ||
removeJob: function(next) { | ||
previousRemove.call(this, next); | ||
}.bind(this), | ||
removeJob: function (next) { | ||
previousRemove.call(this, next); | ||
}.bind(this), | ||
removeUniqueData: function(next) { | ||
Job.removeUniqueJobData(this.id, next); | ||
}.bind(this) | ||
removeUniqueData: function (next) { | ||
Job.removeUniqueJobData(this.id, next); | ||
}.bind(this) | ||
}, function finalize(error /*, results*/ ) { | ||
done(error, this); | ||
}.bind(this)); | ||
}, function finalize(error /*, results*/ ) { | ||
done(error, this); | ||
}.bind(this)); | ||
return this; | ||
return this; | ||
}; | ||
@@ -284,0 +291,0 @@ |
{ | ||
"name": "kue-unique", | ||
"version": "1.0.0", | ||
"description": "Unique job utility for kue", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lykmapipo/kue-unique.git" | ||
}, | ||
"keywords": [ | ||
"kue", | ||
"queue", | ||
"que", | ||
"q", | ||
"unique", | ||
"once", | ||
"single", | ||
"one", | ||
"only", | ||
"jobs", | ||
"redis", | ||
"job", | ||
"worker" | ||
], | ||
"author": "lykmapipo", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lykmapipo/kue-unique/issues" | ||
}, | ||
"homepage": "https://github.com/lykmapipo/kue-unique", | ||
"contributors": [{ | ||
"name": "lykmapipo", | ||
"github": "https://github.com/lykmapipo" | ||
}], | ||
"devDependencies": { | ||
"async": "^1.4.2", | ||
"chai": "^3.3.0", | ||
"faker": "^3.0.1", | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jshint": "^0.11.3", | ||
"grunt-mocha-test": "^0.12.7", | ||
"jshint-stylish": "^2.0.1", | ||
"kue": "^0.9.6", | ||
"lodash": "^3.10.1", | ||
"mocha": "^2.3.3" | ||
} | ||
"name": "kue-unique", | ||
"version": "1.0.1", | ||
"description": "Unique job utility for kue", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lykmapipo/kue-unique.git" | ||
}, | ||
"keywords": [ | ||
"kue", | ||
"queue", | ||
"que", | ||
"q", | ||
"unique", | ||
"once", | ||
"single", | ||
"one", | ||
"only", | ||
"jobs", | ||
"redis", | ||
"job", | ||
"worker" | ||
], | ||
"author": "lykmapipo", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lykmapipo/kue-unique/issues" | ||
}, | ||
"homepage": "https://github.com/lykmapipo/kue-unique", | ||
"contributors": [{ | ||
"name": "lykmapipo", | ||
"github": "https://github.com/lykmapipo" | ||
}], | ||
"devDependencies": { | ||
"async": "^1.4.2", | ||
"chai": "^3.3.0", | ||
"faker": "^3.0.1", | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jshint": "^0.11.3", | ||
"grunt-mocha-test": "^0.12.7", | ||
"jshint-stylish": "^2.0.1", | ||
"kue": "^0.9.6", | ||
"lodash": "^3.10.1", | ||
"mocha": "^2.3.3" | ||
} | ||
} |
6
212
10098