Skip to content

Commit

Permalink
used custom redlock for fixing #426
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Mar 3, 2017
1 parent 14d69d3 commit 590dd04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
53 changes: 27 additions & 26 deletions lib/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ function execScript(client, hash, lua, numberOfKeys){

debuglog(lua, args);

client.defineCommand(hash, { numberOfKeys: numberOfKeys, lua: lua });
if(!client[hash]){
console.log('command %s not defined...', hash);
client.defineCommand(hash, { numberOfKeys: numberOfKeys, lua: lua });
}

return client[hash](args);
}
Expand All @@ -28,26 +31,20 @@ function isCommandDefined(client, hash){

var scripts = {
_isJobInList: function(keyVar, argVar, operator) {
keyVar = keyVar ? 'splitKey[1]..":"..splitKey[2]..":active"' : 'KEYS[1]';
argVar = argVar || 'ARGV[1]';
operator = operator || 'return';
return [
'local function item_in_list (list, item)',
' for _, v in pairs(list) do',
' if v == item then',
' return 1',
' end',
' end',
' return nil',
'end',
'local splitKey={}',
'local i=1',
'for str in string.gmatch(KEYS[1], "([^:]+)") do',
'splitKey[i] = str',
'i = i + 1',
'end',
[ 'local items = redis.call("LRANGE", ', keyVar, ' , 0, -1)' ].join(''),
[ operator, ' item_in_list(items, ', argVar, ')' ].join('')
keyVar = keyVar || 'KEYS[1]';
argVar = argVar || 'ARGV[1]';
operator = operator || 'return';
return [
'local function item_in_list (list, item)',
' for _, v in pairs(list) do',
' if v == item then',
' return 1',
' end',
' end',
' return nil',
'end',
['local items = redis.call("LRANGE",', keyVar, ' , 0, -1)'].join(''),
[operator, ' item_in_list(items, ', argVar, ')'].join('')
].join('\n');
},
isJobInList: function(client, listKey, jobId){
Expand Down Expand Up @@ -346,12 +343,14 @@ var scripts = {
return Promise.resolve(lock);
}

console.log("take lock", ensureActive)
//ensureActive = false;
var redlock;
if (ensureActive) {
var keyVar = ['"', job.queue.toKey('active'), '"'].join('');
var argVar = ['"', job.jobId, '"'].join('');
var isJobInList = this._isJobInList(keyVar, argVar, 'if');
var lockAcquired = 'and redis.call("HSET", splitKey[1]..":"..splitKey[2]..":"..splitKey[3], "lockAcquired", "1")';
// var keyVar = ['"', job.queue.toKey('active'), '"'].join('');
// var argVar = ['"', job.jobId, '"'].join('');
var isJobInList = this._isJobInList('KEYS[2]', 'ARGV[3]', 'if');
var lockAcquired = ['and redis.call("HSET", "KEYS[3]", "lockAcquired", "1")'].join('');
var success = 'then return 1 else return 0 end';
var opts = {
lockScript: function(lockScript) {
Expand All @@ -361,7 +360,9 @@ var scripts = {
lockAcquired,
success
].join('\n');
}
},
extraLockKeys: [job.queue.toKey('active'), queue.toKey(job.jobId)],
extraLockArgs: [job.jobId]
};
redlock = new Redlock(queue.clients, _.extend(opts, queue.redlock));
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"disturbed": "^1.0.6",
"ioredis": "^2.5.0",
"lodash": "^4.17.4",
"redlock": "^2.1.0",
"redlock": "[email protected]:OptimalBits/node-redlock.git",
"semver": "^5.3.0",
"uuid": "^3.0.1"
},
Expand Down

0 comments on commit 590dd04

Please sign in to comment.