cleanSet.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. --[[
  2. Function to clean job set.
  3. Returns jobIds and deleted count number.
  4. ]]
  5. -- Includes
  6. --- @include "batches"
  7. --- @include "getJobsInZset"
  8. --- @include "getTimestamp"
  9. --- @include "isJobSchedulerJob"
  10. --- @include "removeJob"
  11. local function cleanSet(
  12. setKey,
  13. jobKeyPrefix,
  14. rangeEnd,
  15. timestamp,
  16. limit,
  17. attributes,
  18. isFinished,
  19. jobSchedulersKey)
  20. local jobs = getJobsInZset(setKey, rangeEnd, limit)
  21. local deleted = {}
  22. local deletedCount = 0
  23. local jobTS
  24. for i, job in ipairs(jobs) do
  25. if limit > 0 and deletedCount >= limit then
  26. break
  27. end
  28. local jobKey = jobKeyPrefix .. job
  29. -- Extract a Job Scheduler Id from jobId ("repeat:job-scheduler-id:millis")
  30. -- and check if it is in the scheduled jobs
  31. if not (jobSchedulersKey and isJobSchedulerJob(job, jobKey, jobSchedulersKey)) then
  32. if isFinished then
  33. removeJob(job, true, jobKeyPrefix, true --[[remove debounce key]] )
  34. deletedCount = deletedCount + 1
  35. table.insert(deleted, job)
  36. else
  37. -- * finishedOn says when the job was completed, but it isn't set unless the job has actually completed
  38. jobTS = getTimestamp(jobKey, attributes)
  39. if (not jobTS or jobTS <= timestamp) then
  40. removeJob(job, true, jobKeyPrefix, true --[[remove debounce key]] )
  41. deletedCount = deletedCount + 1
  42. table.insert(deleted, job)
  43. end
  44. end
  45. end
  46. end
  47. if (#deleted > 0) then
  48. for from, to in batches(#deleted, 7000) do
  49. rcall("ZREM", setKey, unpack(deleted, from, to))
  50. end
  51. end
  52. return {deleted, deletedCount}
  53. end