obliterate-2.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. --[[
  2. Completely obliterates a queue and all of its contents
  3. This command completely destroys a queue including all of its jobs, current or past
  4. leaving no trace of its existence. Since this script needs to iterate to find all the job
  5. keys, consider that this call may be slow for very large queues.
  6. The queue needs to be "paused" or it will return an error
  7. If the queue has currently active jobs then the script by default will return error,
  8. however this behaviour can be overrided using the 'force' option.
  9. Input:
  10. KEYS[1] meta
  11. KEYS[2] base
  12. ARGV[1] count
  13. ARGV[2] force
  14. ]]
  15. local maxCount = tonumber(ARGV[1])
  16. local baseKey = KEYS[2]
  17. local rcall = redis.call
  18. -- Includes
  19. --- @include "includes/removeJobs"
  20. --- @include "includes/removeListJobs"
  21. --- @include "includes/removeZSetJobs"
  22. local function removeLockKeys(keys)
  23. for i, key in ipairs(keys) do
  24. rcall("DEL", baseKey .. key .. ':lock')
  25. end
  26. end
  27. -- 1) Check if paused, if not return with error.
  28. if rcall("HEXISTS", KEYS[1], "paused") ~= 1 then
  29. return -1 -- Error, NotPaused
  30. end
  31. -- 2) Check if there are active jobs, if there are and not "force" return error.
  32. local activeKey = baseKey .. 'active'
  33. local activeJobs = getListItems(activeKey, maxCount)
  34. if (#activeJobs > 0) then
  35. if(ARGV[2] == "") then
  36. return -2 -- Error, ExistActiveJobs
  37. end
  38. end
  39. removeLockKeys(activeJobs)
  40. maxCount = removeJobs(activeJobs, true, baseKey, maxCount)
  41. rcall("LTRIM", activeKey, #activeJobs, -1)
  42. if(maxCount <= 0) then
  43. return 1
  44. end
  45. local delayedKey = baseKey .. 'delayed'
  46. maxCount = removeZSetJobs(delayedKey, true, baseKey, maxCount)
  47. if(maxCount <= 0) then
  48. return 1
  49. end
  50. local repeatKey = baseKey .. 'repeat'
  51. local repeatJobsIds = getZSetItems(repeatKey, maxCount)
  52. for i, key in ipairs(repeatJobsIds) do
  53. local jobKey = repeatKey .. ":" .. key
  54. rcall("DEL", jobKey)
  55. end
  56. if(#repeatJobsIds > 0) then
  57. for from, to in batches(#repeatJobsIds, 7000) do
  58. rcall("ZREM", repeatKey, unpack(repeatJobsIds, from, to))
  59. end
  60. end
  61. maxCount = maxCount - #repeatJobsIds
  62. if(maxCount <= 0) then
  63. return 1
  64. end
  65. local completedKey = baseKey .. 'completed'
  66. maxCount = removeZSetJobs(completedKey, true, baseKey, maxCount)
  67. if(maxCount <= 0) then
  68. return 1
  69. end
  70. local waitKey = baseKey .. 'paused'
  71. maxCount = removeListJobs(waitKey, true, baseKey, maxCount)
  72. if(maxCount <= 0) then
  73. return 1
  74. end
  75. local prioritizedKey = baseKey .. 'prioritized'
  76. maxCount = removeZSetJobs(prioritizedKey, true, baseKey, maxCount)
  77. if(maxCount <= 0) then
  78. return 1
  79. end
  80. local failedKey = baseKey .. 'failed'
  81. maxCount = removeZSetJobs(failedKey, true, baseKey, maxCount)
  82. if(maxCount <= 0) then
  83. return 1
  84. end
  85. if(maxCount > 0) then
  86. rcall("DEL",
  87. baseKey .. 'events',
  88. baseKey .. 'delay',
  89. baseKey .. 'stalled-check',
  90. baseKey .. 'stalled',
  91. baseKey .. 'id',
  92. baseKey .. 'pc',
  93. baseKey .. 'marker',
  94. baseKey .. 'meta',
  95. baseKey .. 'metrics:completed',
  96. baseKey .. 'metrics:completed:data',
  97. baseKey .. 'metrics:failed',
  98. baseKey .. 'metrics:failed:data')
  99. return 0
  100. else
  101. return 1
  102. end