pause-7.js 1.8 KB

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