extendLocks-1.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.extendLocks = void 0;
  4. const content = `--[[
  5. Extend locks for multiple jobs and remove them from the stalled set if successful.
  6. Return the list of job IDs for which the operation failed.
  7. KEYS[1] = stalled key
  8. ARGV[1] = baseKey
  9. ARGV[2] = tokens
  10. ARGV[3] = jobIds
  11. ARGV[4] = lockDuration (ms)
  12. Output:
  13. An array of failed job IDs. If empty, all succeeded.
  14. ]]
  15. local rcall = redis.call
  16. local stalledKey = KEYS[1]
  17. local baseKey = ARGV[1]
  18. local tokens = cmsgpack.unpack(ARGV[2])
  19. local jobIds = cmsgpack.unpack(ARGV[3])
  20. local lockDuration = ARGV[4]
  21. local jobCount = #jobIds
  22. local failedJobs = {}
  23. for i = 1, jobCount, 1 do
  24. local lockKey = baseKey .. jobIds[i] .. ':lock'
  25. local jobId = jobIds[i]
  26. local token = tokens[i]
  27. local currentToken = rcall("GET", lockKey)
  28. if currentToken then
  29. if currentToken == token then
  30. local setResult = rcall("SET", lockKey, token, "PX", lockDuration)
  31. if setResult then
  32. rcall("SREM", stalledKey, jobId)
  33. else
  34. table.insert(failedJobs, jobId)
  35. end
  36. else
  37. table.insert(failedJobs, jobId)
  38. end
  39. else
  40. table.insert(failedJobs, jobId)
  41. end
  42. end
  43. return failedJobs
  44. `;
  45. exports.extendLocks = {
  46. name: 'extendLocks',
  47. content,
  48. keys: 1,
  49. };
  50. //# sourceMappingURL=extendLocks-1.js.map