Skip to content

Commit

Permalink
fix touch method
Browse files Browse the repository at this point in the history
  • Loading branch information
surjikal committed Oct 16, 2021
1 parent fd05e52 commit ce80942
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ const RedisScript = (scriptName) => {
}

module.exports.ParityDel = RedisScript('parityDel');
module.exports.ParityRelock = RedisScript('parityRelock');
30 changes: 16 additions & 14 deletions lib/warlock.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const UUID = require('uuid');
const { ParityDel } = require('./scripts');
const { ParityDel, ParityRelock } = require('./scripts');

module.exports = function (redis) {
const warlock = {};

const parityDel = ParityDel(redis);
const parityRelock = ParityRelock(redis);

warlock.makeKey = function (key) {
return `${key}:lock`;
Expand Down Expand Up @@ -87,23 +88,24 @@ module.exports = function (redis) {
tryLock();
};

warlock.touch = function(key, id, ttl, cb) {
cb = cb || function (){};

warlock.touch = async (key, id, ttl, cb) => {
if (typeof key !== 'string') {
return cb(new Error('lock key must be string'));
const e = new Error('lock key must be string');
e.id = id;
e.key = key;
e.ttl = ttl;
if (!cb) throw e;
return cb(e);
}

scripty.loadScriptFile(
'parityRelock',
__dirname + '/lua/parityRelock.lua',
function (err, parityRelock) {
if (err) { return cb(err); }
return parityRelock.run(3, warlock.makeKey(key), ttl, id, cb);
}
);
try {
const result = await parityRelock(3, warlock.makeKey(key), ttl, id);
return cb ? cb(null, result) : result;
} catch (e) {
if (!cb) throw e;
return cb(e);
}
};


return warlock;
};

0 comments on commit ce80942

Please sign in to comment.