|
1 | 1 | --[[
|
2 |
| - Completely obliterates a queue and all of its content |
| 2 | + Completely obliterates a queue and all of its contents |
3 | 3 | Input:
|
4 | 4 |
|
5 | 5 | KEYS[1] meta
|
6 |
| - KEYS[2] active |
| 6 | + KEYS[2] base |
7 | 7 |
|
8 |
| - ARGV[1] cursor |
9 |
| - ARGV[2] pattern |
10 |
| - ARGV[3] count |
11 |
| - ARGV[4] force |
| 8 | + ARGV[1] count |
| 9 | + ARGV[2] force |
12 | 10 | ]]
|
13 | 11 |
|
14 | 12 | -- This command completely destroys a queue including all of its jobs, current or past
|
|
18 | 16 | -- The queue needs to be "paused" or it will return an error
|
19 | 17 | -- If the queue has currently active jobs then the script by default will return error,
|
20 | 18 | -- however this behaviour can be overrided using the `force` option.
|
| 19 | +local maxCount = tonumber(ARGV[1]) |
| 20 | +local count = 0 |
| 21 | +local baseKey = KEYS[2] |
21 | 22 |
|
22 | 23 | local rcall = redis.call
|
| 24 | +local function getListItems(keyName) |
| 25 | + return rcall('LRANGE', keyName, 0, -1) |
| 26 | +end |
| 27 | + |
| 28 | +local function getZSetItems(keyName) |
| 29 | + return rcall('ZRANGE', keyName, 0, -1) |
| 30 | +end |
| 31 | + |
| 32 | +local function getSetItems(keyName) |
| 33 | + return rcall('SMEMBERS', keyName, 0, -1) |
| 34 | +end |
| 35 | + |
| 36 | +local function removeKeys(parentKey, keys) |
| 37 | + for i, key in ipairs(keys) do |
| 38 | + if(count > maxCount) then |
| 39 | + return true |
| 40 | + end |
| 41 | + rcall("DEL", baseKey .. key) |
| 42 | + count = count + 1 |
| 43 | + end |
| 44 | + rcall("DEL", parentKey) |
| 45 | + return false |
| 46 | +end |
| 47 | + |
| 48 | +local function removeLockKeys(keys) |
| 49 | + for i, key in ipairs(keys) do |
| 50 | + if(count > maxCount) then |
| 51 | + return true |
| 52 | + end |
| 53 | + rcall("DEL", baseKey .. key .. ':lock') |
| 54 | + count = count + 1 |
| 55 | + end |
| 56 | + return false |
| 57 | +end |
23 | 58 |
|
24 | 59 | -- 1) Check if paused, if not return with error.
|
25 | 60 | if rcall("HEXISTS", KEYS[1], "paused") ~= 1 then
|
26 | 61 | return -1 -- Error, NotPaused
|
27 | 62 | end
|
28 | 63 |
|
29 | 64 | -- 2) Check if there are active jobs, if there are and not "force" return error.
|
30 |
| -local activeKey = KEYS[2] |
31 |
| -local active = rcall('LRANGE', activeKey, 0, -1) |
32 |
| -if (#active > 0) then |
33 |
| - if(ARGV[4] == "") then |
| 65 | +local activeKey = baseKey .. 'active' |
| 66 | +local activeKeys = getListItems(activeKey) |
| 67 | +if (#activeKeys > 0) then |
| 68 | + if(ARGV[2] == "") then |
34 | 69 | return -2 -- Error, ExistsActiveJobs
|
35 | 70 | end
|
36 | 71 | end
|
37 | 72 |
|
38 |
| -local result = rcall("scan", ARGV[1], "MATCH", ARGV[2], "COUNT", ARGV[3]) |
39 |
| -local cursor = result[1] |
40 |
| -local keys = result[2] |
41 |
| -for i, key in ipairs(keys) do |
42 |
| - rcall("DEL", key) |
| 73 | +if(removeLockKeys(activeKeys)) then |
| 74 | + return 1 |
43 | 75 | end
|
| 76 | + |
| 77 | +if(removeKeys(activeKey, activeKeys)) then |
| 78 | + return 1 |
| 79 | +end |
| 80 | + |
| 81 | +local waitKey = baseKey .. 'paused' |
| 82 | +if(removeKeys(waitKey, getListItems(waitKey))) then |
| 83 | + return 1 |
| 84 | +end |
| 85 | + |
| 86 | +local delayedKey = baseKey .. 'delayed' |
| 87 | +if(removeKeys(delayedKey, getZSetItems(delayedKey))) then |
| 88 | + return 1 |
| 89 | +end |
| 90 | + |
| 91 | +local completedKey = baseKey .. 'completed' |
| 92 | +if(removeKeys(completedKey, getZSetItems(completedKey))) then |
| 93 | + return 1 |
| 94 | +end |
| 95 | + |
| 96 | +local failedKey = baseKey .. 'failed' |
| 97 | +if(removeKeys(failedKey, getZSetItems(failedKey))) then |
| 98 | + return 1 |
| 99 | +end |
| 100 | + |
| 101 | +local waitKey = baseKey .. 'wait' |
| 102 | +if(removeKeys(waitKey, getListItems(waitKey))) then |
| 103 | + return 1 |
| 104 | +end |
| 105 | + |
| 106 | +local waitKey = baseKey .. 'wait' |
| 107 | +if(removeKeys(waitKey, getListItems(waitKey))) then |
| 108 | + return 1 |
| 109 | +end |
| 110 | + |
| 111 | +rcall("DEL", baseKey .. 'priority') |
| 112 | +rcall("DEL", baseKey .. 'events') |
| 113 | +rcall("DEL", baseKey .. 'delay') |
| 114 | +rcall("DEL", baseKey .. 'stalled-check') |
| 115 | +rcall("DEL", baseKey .. 'stalled') |
| 116 | +rcall("DEL", baseKey .. 'id') |
| 117 | +rcall("DEL", baseKey .. 'meta') |
| 118 | + |
| 119 | +return 0 |
0 commit comments