cleanList.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. --[[
  2. Function to clean job list.
  3. Returns jobIds and deleted count number.
  4. ]]
  5. -- Includes
  6. --- @include "getTimestamp"
  7. --- @include "isJobSchedulerJob"
  8. --- @include "removeJob"
  9. local function cleanList(listKey, jobKeyPrefix, rangeStart, rangeEnd,
  10. timestamp, isWaiting, jobSchedulersKey)
  11. local jobs = rcall("LRANGE", listKey, rangeStart, rangeEnd)
  12. local deleted = {}
  13. local deletedCount = 0
  14. local jobTS
  15. local deletionMarker = ''
  16. local jobIdsLen = #jobs
  17. for i, job in ipairs(jobs) do
  18. if limit > 0 and deletedCount >= limit then
  19. break
  20. end
  21. local jobKey = jobKeyPrefix .. job
  22. if (isWaiting or rcall("EXISTS", jobKey .. ":lock") == 0) and
  23. not isJobSchedulerJob(job, jobKey, jobSchedulersKey) then
  24. -- Find the right timestamp of the job to compare to maxTimestamp:
  25. -- * finishedOn says when the job was completed, but it isn't set unless the job has actually completed
  26. -- * processedOn represents when the job was last attempted, but it doesn't get populated until
  27. -- the job is first tried
  28. -- * timestamp is the original job submission time
  29. -- Fetch all three of these (in that order) and use the first one that is set so that we'll leave jobs
  30. -- that have been active within the grace period:
  31. jobTS = getTimestamp(jobKey, {"finishedOn", "processedOn", "timestamp"})
  32. if (not jobTS or jobTS <= timestamp) then
  33. -- replace the entry with a deletion marker; the actual deletion will
  34. -- occur at the end of the script
  35. rcall("LSET", listKey, rangeEnd - jobIdsLen + i, deletionMarker)
  36. removeJob(job, true, jobKeyPrefix, true --[[remove debounce key]])
  37. deletedCount = deletedCount + 1
  38. table.insert(deleted, job)
  39. end
  40. end
  41. end
  42. rcall("LREM", listKey, 0, deletionMarker)
  43. return {deleted, deletedCount}
  44. end