moveJobFromActiveToWait-9.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. --[[
  2. Function to move job from active state to wait.
  3. Input:
  4. KEYS[1] active key
  5. KEYS[2] wait key
  6. KEYS[3] stalled key
  7. KEYS[4] paused key
  8. KEYS[5] meta key
  9. KEYS[6] limiter key
  10. KEYS[7] prioritized key
  11. KEYS[8] marker key
  12. KEYS[9] event key
  13. ARGV[1] job id
  14. ARGV[2] lock token
  15. ARGV[3] job id key
  16. ]]
  17. local rcall = redis.call
  18. -- Includes
  19. --- @include "includes/addJobInTargetList"
  20. --- @include "includes/pushBackJobWithPriority"
  21. --- @include "includes/getOrSetMaxEvents"
  22. --- @include "includes/getTargetQueueList"
  23. --- @include "includes/removeLock"
  24. local jobId = ARGV[1]
  25. local token = ARGV[2]
  26. local jobKey = ARGV[3]
  27. if rcall("EXISTS", jobKey) == 0 then
  28. return -1
  29. end
  30. local errorCode = removeLock(jobKey, KEYS[3], token, jobId)
  31. if errorCode < 0 then
  32. return errorCode
  33. end
  34. local metaKey = KEYS[5]
  35. local removed = rcall("LREM", KEYS[1], 1, jobId)
  36. if removed > 0 then
  37. local target, isPausedOrMaxed = getTargetQueueList(metaKey, KEYS[1], KEYS[2], KEYS[4])
  38. local priority = tonumber(rcall("HGET", ARGV[3], "priority")) or 0
  39. if priority > 0 then
  40. pushBackJobWithPriority(KEYS[7], priority, jobId)
  41. else
  42. addJobInTargetList(target, KEYS[8], "RPUSH", isPausedOrMaxed, jobId)
  43. end
  44. local maxEvents = getOrSetMaxEvents(metaKey)
  45. -- Emit waiting event
  46. rcall("XADD", KEYS[9], "MAXLEN", "~", maxEvents, "*", "event", "waiting",
  47. "jobId", jobId, "prev", "active")
  48. end
  49. local pttl = rcall("PTTL", KEYS[6])
  50. if pttl > 0 then
  51. return pttl
  52. else
  53. return 0
  54. end