extendLocks-1.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. --[[
  2. Extend locks for multiple jobs and remove them from the stalled set if successful.
  3. Return the list of job IDs for which the operation failed.
  4. KEYS[1] = stalled key
  5. ARGV[1] = baseKey
  6. ARGV[2] = tokens
  7. ARGV[3] = jobIds
  8. ARGV[4] = lockDuration (ms)
  9. Output:
  10. An array of failed job IDs. If empty, all succeeded.
  11. ]]
  12. local rcall = redis.call
  13. local stalledKey = KEYS[1]
  14. local baseKey = ARGV[1]
  15. local tokens = cmsgpack.unpack(ARGV[2])
  16. local jobIds = cmsgpack.unpack(ARGV[3])
  17. local lockDuration = ARGV[4]
  18. local jobCount = #jobIds
  19. local failedJobs = {}
  20. for i = 1, jobCount, 1 do
  21. local lockKey = baseKey .. jobIds[i] .. ':lock'
  22. local jobId = jobIds[i]
  23. local token = tokens[i]
  24. local currentToken = rcall("GET", lockKey)
  25. if currentToken then
  26. if currentToken == token then
  27. local setResult = rcall("SET", lockKey, token, "PX", lockDuration)
  28. if setResult then
  29. rcall("SREM", stalledKey, jobId)
  30. else
  31. table.insert(failedJobs, jobId)
  32. end
  33. else
  34. table.insert(failedJobs, jobId)
  35. end
  36. else
  37. table.insert(failedJobs, jobId)
  38. end
  39. end
  40. return failedJobs