Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for takeLock & optionally forcing #1033

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/commands/takeLock-1.lua
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/job.js
Expand Up @@ -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;
});
};
Expand Down
7 changes: 4 additions & 3 deletions lib/scripts.js
Expand Up @@ -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
]);
},

Expand Down