removeJobsByMaxAge.lua 690 B

123456789101112131415161718192021222324
  1. --[[
  2. Functions to remove jobs by max age.
  3. ]]
  4. -- Includes
  5. --- @include "batches"
  6. --- @include "removeJob"
  7. local function removeJobsByMaxAge(timestamp, maxAge, targetSet, prefix, maxLimit)
  8. local start = timestamp - maxAge * 1000
  9. local jobIds = rcall("ZREVRANGEBYSCORE", targetSet, start, "-inf", "LIMIT", 0, maxLimit)
  10. for i, jobId in ipairs(jobIds) do
  11. removeJob(jobId, false, prefix, false --[[remove debounce key]])
  12. end
  13. if #jobIds > 0 then
  14. if #jobIds < maxLimit then
  15. rcall("ZREMRANGEBYSCORE", targetSet, "-inf", start)
  16. else
  17. for from, to in batches(#jobIds, 7000) do
  18. rcall("ZREM", targetSet, unpack(jobIds, from, to))
  19. end
  20. end
  21. end
  22. end