|
| 1 | +--[[ |
| 2 | + Remove a job from all the queues it may be in as well as all its data. |
| 3 | + In order to be able to remove a job, it cannot be active. |
| 4 | +
|
| 5 | + Input: |
| 6 | + KEYS[1] jobId |
| 7 | + ARGV[1] jobId |
| 8 | +
|
| 9 | + Events: |
| 10 | + 'removed' |
| 11 | +]] |
| 12 | + |
| 13 | +local rcall = redis.call |
| 14 | + |
| 15 | +local getJobIdFromKey = function (jobKey) |
| 16 | + return string.match(jobKey, ".*:(.*)") |
| 17 | +end |
| 18 | + |
| 19 | +local getJobKeyPrefix = function (jobKey, jobId) |
| 20 | + return string.sub(jobKey, 0, #jobKey - #jobId) |
| 21 | +end |
| 22 | + |
| 23 | +-- recursively check if there are no locks on the |
| 24 | +-- jobs to be removed. |
| 25 | +local function isLocked( prefix, jobId) |
| 26 | + local jobKey = prefix .. jobId; |
| 27 | + |
| 28 | + -- Check if this job is locked |
| 29 | + local lockKey = jobKey .. ':lock' |
| 30 | + local lock = rcall("GET", lockKey) |
| 31 | + if not lock then |
| 32 | + local dependencies = rcall("SMEMBERS", jobKey .. ":dependencies") |
| 33 | + if (#dependencies > 0) then |
| 34 | + for i, childJobKey in ipairs(dependencies) do |
| 35 | + -- We need to get the jobId for this job. |
| 36 | + local childJobId = getJobIdFromKey(childJobKey) |
| 37 | + local childJobPrefix = getJobKeyPrefix(childJobKey, childJobId) |
| 38 | + local result = isLocked( childJobPrefix, childJobId ) |
| 39 | + if result then |
| 40 | + return true |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + return false |
| 45 | + end |
| 46 | + return true |
| 47 | +end |
| 48 | + |
| 49 | +local function removeJob( prefix, jobId) |
| 50 | + local jobKey = prefix .. jobId; |
| 51 | + |
| 52 | + -- Check if this job has a parent. If so we will just remove it from |
| 53 | + -- the parent child list, but if it is the last child we should move the parent to "wait/paused" |
| 54 | + -- which requires code from "moveToFinished" |
| 55 | + local parentKey = rcall("HGET", jobKey, "parentKey") |
| 56 | + if( (type(parentKey) == "string") and (rcall("EXISTS", parentKey) == 1)) then |
| 57 | + local parentDependenciesKey = parentKey .. ":dependencies" |
| 58 | + local result = rcall("SREM", parentDependenciesKey, jobKey) |
| 59 | + if rcall("SCARD", parentDependenciesKey) == 0 then |
| 60 | + local parentId = getJobIdFromKey(parentKey) |
| 61 | + local parentPrefix = getJobKeyPrefix(parentKey, parentId) |
| 62 | + |
| 63 | + rcall("ZREM", parentPrefix .. "waiting-children", parentId) |
| 64 | + |
| 65 | + if rcall("HEXISTS", parentPrefix .. "meta", "paused") ~= 1 then |
| 66 | + rcall("RPUSH", parentPrefix .. "wait", parentId) |
| 67 | + else |
| 68 | + rcall("RPUSH", parentPrefix .. "parentPrefixpaused", parentId) |
| 69 | + end |
| 70 | + |
| 71 | + local parentEventStream = parentPrefix .. "events" |
| 72 | + rcall("XADD", parentEventStream, "*", "event", "active", "jobId", parentId, "prev", "waiting-children") |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + rcall("LREM", prefix .. "active", 0, jobId) |
| 77 | + rcall("LREM", prefix .. "wait", 0, jobId) |
| 78 | + rcall("ZREM", prefix .. "delayed", jobId) |
| 79 | + rcall("LREM", prefix .. "paused", 0, jobId) |
| 80 | + rcall("ZREM", prefix .. "completed", jobId) |
| 81 | + rcall("ZREM", prefix .. "failed", jobId) |
| 82 | + rcall("ZREM", prefix .. "priority", jobId) |
| 83 | + rcall("ZREM", prefix .. "waiting-children", jobId) |
| 84 | + rcall("DEL", jobKey) |
| 85 | + rcall("DEL", jobKey .. ":logs") |
| 86 | + |
| 87 | + -- Check if this job has children |
| 88 | + -- If so, we are going to try to remove the children recursively in deep first way because |
| 89 | + -- if some job is locked we must exit with and error. |
| 90 | + local dependencies = rcall("SMEMBERS", jobKey .. ":dependencies") |
| 91 | + if (#dependencies > 0) then |
| 92 | + for i, childJobKey in ipairs(dependencies) do |
| 93 | + -- We need to get the jobId for this job. |
| 94 | + local childJobId = getJobIdFromKey(childJobKey) |
| 95 | + local childJobPrefix = getJobKeyPrefix(childJobKey, childJobId) |
| 96 | + removeJob( childJobPrefix, childJobId ) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + rcall("DEL", jobKey .. ":dependencies") |
| 101 | + |
| 102 | + -- -- delete keys related to rate limiter |
| 103 | + -- local limiterIndexTable = KEYS[10] .. ":index" |
| 104 | + -- local limitedSetKey = rcall("HGET", limiterIndexTable, jobId) |
| 105 | + -- if limitedSetKey then |
| 106 | + -- rcall("SREM", limitedSetKey, jobId) |
| 107 | + -- rcall("HDEL", limiterIndexTable, jobId) |
| 108 | + -- end |
| 109 | + |
| 110 | + rcall("XADD", prefix .. "events", "*", "event", "removed", "jobId", jobId, "prev", "unknown"); |
| 111 | +end |
| 112 | + |
| 113 | +local prefix = getJobKeyPrefix(KEYS[1], ARGV[1]) |
| 114 | + |
| 115 | +if not isLocked(prefix, ARGV[1]) then |
| 116 | + removeJob(prefix, ARGV[1]) |
| 117 | + return 1 |
| 118 | +end |
| 119 | +return 0 |
0 commit comments