removeOrphanedJobs-1.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.removeOrphanedJobs = void 0;
  4. const content = `--[[
  5. Removes orphaned job keys that exist in Redis but are not referenced
  6. in any queue state set. Checks each candidate atomically.
  7. Input:
  8. KEYS[1] base prefix key including trailing colon (e.g. bull:queueName:)
  9. ARGV[1] number of state key suffixes
  10. ARGV[2 .. 1+N] state key suffixes (e.g. active, wait, completed, ...)
  11. ARGV[2+N] number of job sub-key suffixes
  12. ARGV[3+N .. 2+N+M] job sub-key suffixes (e.g. logs, dependencies, ...)
  13. ARGV[3+N+M .. end] candidate job IDs to check
  14. Output:
  15. number of removed jobs
  16. ]]
  17. local rcall = redis.call
  18. local basePrefix = KEYS[1]
  19. -- Parse state key suffixes and cache their full key names + types.
  20. local stateKeyCount = tonumber(ARGV[1])
  21. local stateKeys = {}
  22. local stateKeyTypes = {}
  23. for i = 1, stateKeyCount do
  24. local fullKey = basePrefix .. ARGV[1 + i]
  25. stateKeys[i] = fullKey
  26. stateKeyTypes[i] = rcall('TYPE', fullKey)['ok']
  27. end
  28. -- Parse job sub-key suffixes.
  29. local subKeyCountIdx = 2 + stateKeyCount
  30. local subKeyCount = tonumber(ARGV[subKeyCountIdx])
  31. local subKeySuffixes = {}
  32. for i = 1, subKeyCount do
  33. subKeySuffixes[i] = ARGV[subKeyCountIdx + i]
  34. end
  35. -- Process candidate job IDs.
  36. local candidateStart = subKeyCountIdx + subKeyCount + 1
  37. local removedCount = 0
  38. for c = candidateStart, #ARGV do
  39. local jobId = ARGV[c]
  40. local found = false
  41. for i = 1, stateKeyCount do
  42. local kt = stateKeyTypes[i]
  43. if kt == 'list' then
  44. if rcall('LPOS', stateKeys[i], jobId) then
  45. found = true
  46. break
  47. end
  48. elseif kt == 'zset' then
  49. if rcall('ZSCORE', stateKeys[i], jobId) then
  50. found = true
  51. break
  52. end
  53. elseif kt == 'set' then
  54. if rcall('SISMEMBER', stateKeys[i], jobId) == 1 then
  55. found = true
  56. break
  57. end
  58. end
  59. end
  60. if not found then
  61. local jobKey = basePrefix .. jobId
  62. local keysToDelete = { jobKey }
  63. for _, suffix in ipairs(subKeySuffixes) do
  64. keysToDelete[#keysToDelete + 1] = jobKey .. ':' .. suffix
  65. end
  66. rcall('DEL', unpack(keysToDelete))
  67. removedCount = removedCount + 1
  68. end
  69. end
  70. return removedCount
  71. `;
  72. exports.removeOrphanedJobs = {
  73. name: 'removeOrphanedJobs',
  74. content,
  75. keys: 1,
  76. };
  77. //# sourceMappingURL=removeOrphanedJobs-1.js.map