pause-7.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const content = `--[[
  2. Pauses or resumes a queue globably.
  3. Input:
  4. KEYS[1] 'wait' or 'paused''
  5. KEYS[2] 'paused' or 'wait'
  6. KEYS[3] 'meta'
  7. KEYS[4] 'prioritized'
  8. KEYS[5] events stream key
  9. KEYS[6] 'delayed'
  10. KEYS|7] 'marker'
  11. ARGV[1] 'paused' or 'resumed'
  12. Event:
  13. publish paused or resumed event.
  14. ]]
  15. local rcall = redis.call
  16. -- Includes
  17. --[[
  18. Add delay marker if needed.
  19. ]]
  20. -- Includes
  21. --[[
  22. Function to return the next delayed job timestamp.
  23. ]]
  24. local function getNextDelayedTimestamp(delayedKey)
  25. local result = rcall("ZRANGE", delayedKey, 0, 0, "WITHSCORES")
  26. if #result then
  27. local nextTimestamp = tonumber(result[2])
  28. if nextTimestamp ~= nil then
  29. return nextTimestamp / 0x1000
  30. end
  31. end
  32. end
  33. local function addDelayMarkerIfNeeded(markerKey, delayedKey)
  34. local nextTimestamp = getNextDelayedTimestamp(delayedKey)
  35. if nextTimestamp ~= nil then
  36. -- Replace the score of the marker with the newest known
  37. -- next timestamp.
  38. rcall("ZADD", markerKey, nextTimestamp, "1")
  39. end
  40. end
  41. local markerKey = KEYS[7]
  42. local hasJobs = rcall("EXISTS", KEYS[1]) == 1
  43. --TODO: check this logic to be reused when changing a delay
  44. if hasJobs then rcall("RENAME", KEYS[1], KEYS[2]) end
  45. if ARGV[1] == "paused" then
  46. rcall("HSET", KEYS[3], "paused", 1)
  47. rcall("DEL", markerKey)
  48. else
  49. rcall("HDEL", KEYS[3], "paused")
  50. if hasJobs or rcall("ZCARD", KEYS[4]) > 0 then
  51. -- Add marker if there are waiting or priority jobs
  52. rcall("ZADD", markerKey, 0, "0")
  53. else
  54. addDelayMarkerIfNeeded(markerKey, KEYS[6])
  55. end
  56. end
  57. rcall("XADD", KEYS[5], "*", "event", ARGV[1]);
  58. `;
  59. export const pause = {
  60. name: 'pause',
  61. content,
  62. keys: 7,
  63. };
  64. //# sourceMappingURL=pause-7.js.map