From 94df8c368fcc2cabfb078fdc96546273b828708d Mon Sep 17 00:00:00 2001 From: Tomasz Rychlewicz Date: Mon, 27 Aug 2018 15:01:38 +0200 Subject: [PATCH] 1. Fix for call job.lockKey in scripts.js (it is function, but called as property) 2. Possibility to take lock without checking key existance --- lib/commands/takeLock-1.lua | 13 +++++++++++-- lib/job.js | 4 ++-- lib/scripts.js | 7 ++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/commands/takeLock-1.lua b/lib/commands/takeLock-1.lua index dca6f77a0..254b78350 100644 --- a/lib/commands/takeLock-1.lua +++ b/lib/commands/takeLock-1.lua @@ -3,13 +3,22 @@ Input: KEYS[1] 'lock', - + ARGV[1] token ARGV[2] lock duration in milliseconds - + ARGV[3] force + Output: "OK" if lock extented succesfully. ]] +if ARGV[3] == "1" then + if redis.call("SET", KEYS[1], ARGV[1], "PX", ARGV[2]) then + return 1 + else + return 0 + end +end + if redis.call("SET", KEYS[1], ARGV[1], "NX", "PX", ARGV[2]) then return 1 else diff --git a/lib/job.js b/lib/job.js index edb938fe7..208078e7e 100644 --- a/lib/job.js +++ b/lib/job.js @@ -161,8 +161,8 @@ Job.prototype.lockKey = function() { Takes a lock for this job so that no other queue worker can process it at the same time. */ -Job.prototype.takeLock = function() { - return scripts.takeLock(this.queue, this).then(function(lock) { +Job.prototype.takeLock = function(force = false) { + return scripts.takeLock(this.queue, this, force).then(function(lock) { return lock || false; }); }; diff --git a/lib/scripts.js b/lib/scripts.js index ba3c5c35d..1846a0d5b 100644 --- a/lib/scripts.js +++ b/lib/scripts.js @@ -311,11 +311,12 @@ var scripts = { ]); }, - takeLock: function(queue, job) { + takeLock: function(queue, job, force = false) { return queue.client.takeLock([ - job.lockKey, + job.lockKey(), queue.token, - queue.settings.lockDuration + queue.settings.lockDuration, + force ? 1 : 0 ]); },