From c750cbb508275e041a782c954a5664d7a2fafa8b Mon Sep 17 00:00:00 2001 From: Manuel Astudillo Date: Fri, 3 Mar 2017 23:58:35 +0100 Subject: [PATCH] used custom redlock for fixing #426 --- lib/scripts.js | 48 ++++++++++++++++++++++-------------------------- package.json | 2 +- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/scripts.js b/lib/scripts.js index 5f20b48a2..c37f0c4a3 100644 --- a/lib/scripts.js +++ b/lib/scripts.js @@ -17,7 +17,9 @@ function execScript(client, hash, lua, numberOfKeys){ debuglog(lua, args); - client.defineCommand(hash, { numberOfKeys: numberOfKeys, lua: lua }); + if(!client[hash]){ + client.defineCommand(hash, { numberOfKeys: numberOfKeys, lua: lua }); + } return client[hash](args); } @@ -28,26 +30,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){ @@ -348,10 +344,8 @@ var scripts = { 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 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) { @@ -361,7 +355,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 { diff --git a/package.json b/package.json index 6a6b32e64..67046e80f 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "disturbed": "^1.0.6", "ioredis": "^2.5.0", "lodash": "^4.17.4", - "redlock": "^2.1.0", + "redlock": "git@github.com:OptimalBits/node-redlock.git", "semver": "^5.3.0", "uuid": "^3.0.1" },