removeChildDependency-1.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const content = `--[[
  2. Break parent-child dependency by removing
  3. child reference from parent
  4. Input:
  5. KEYS[1] 'key' prefix,
  6. ARGV[1] job key
  7. ARGV[2] parent key
  8. Output:
  9. 0 - OK
  10. 1 - There is not relationship.
  11. -1 - Missing job key
  12. -5 - Missing parent key
  13. ]]
  14. local rcall = redis.call
  15. local jobKey = ARGV[1]
  16. local parentKey = ARGV[2]
  17. -- Includes
  18. --[[
  19. Check if this job has a parent. If so we will just remove it from
  20. the parent child list, but if it is the last child we should move the parent to "wait/paused"
  21. which requires code from "moveToFinished"
  22. ]]
  23. -- Includes
  24. --[[
  25. Function to add job in target list and add marker if needed.
  26. ]]
  27. -- Includes
  28. --[[
  29. Add marker if needed when a job is available.
  30. ]]
  31. local function addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
  32. if not isPausedOrMaxed then
  33. rcall("ZADD", markerKey, 0, "0")
  34. end
  35. end
  36. local function addJobInTargetList(targetKey, markerKey, pushCmd, isPausedOrMaxed, jobId)
  37. rcall(pushCmd, targetKey, jobId)
  38. addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
  39. end
  40. --[[
  41. Functions to destructure job key.
  42. Just a bit of warning, these functions may be a bit slow and affect performance significantly.
  43. ]]
  44. local getJobIdFromKey = function (jobKey)
  45. return string.match(jobKey, ".*:(.*)")
  46. end
  47. local getJobKeyPrefix = function (jobKey, jobId)
  48. return string.sub(jobKey, 0, #jobKey - #jobId)
  49. end
  50. --[[
  51. Function to check for the meta.paused key to decide if we are paused or not
  52. (since an empty list and !EXISTS are not really the same).
  53. ]]
  54. local function getTargetQueueList(queueMetaKey, activeKey, waitKey, pausedKey)
  55. local queueAttributes = rcall("HMGET", queueMetaKey, "paused", "concurrency", "max", "duration")
  56. if queueAttributes[1] then
  57. return pausedKey, true, queueAttributes[3], queueAttributes[4]
  58. else
  59. if queueAttributes[2] then
  60. local activeCount = rcall("LLEN", activeKey)
  61. if activeCount >= tonumber(queueAttributes[2]) then
  62. return waitKey, true, queueAttributes[3], queueAttributes[4]
  63. else
  64. return waitKey, false, queueAttributes[3], queueAttributes[4]
  65. end
  66. end
  67. end
  68. return waitKey, false, queueAttributes[3], queueAttributes[4]
  69. end
  70. --[[
  71. Function to remove job keys.
  72. ]]
  73. local function removeJobKeys(jobKey)
  74. return rcall("DEL", jobKey, jobKey .. ':logs', jobKey .. ':dependencies',
  75. jobKey .. ':processed', jobKey .. ':failed', jobKey .. ':unsuccessful')
  76. end
  77. local function _moveParentToWait(parentPrefix, parentId, emitEvent)
  78. local parentTarget, isPausedOrMaxed = getTargetQueueList(parentPrefix .. "meta", parentPrefix .. "active",
  79. parentPrefix .. "wait", parentPrefix .. "paused")
  80. addJobInTargetList(parentTarget, parentPrefix .. "marker", "RPUSH", isPausedOrMaxed, parentId)
  81. if emitEvent then
  82. local parentEventStream = parentPrefix .. "events"
  83. rcall("XADD", parentEventStream, "*", "event", "waiting", "jobId", parentId, "prev", "waiting-children")
  84. end
  85. end
  86. local function removeParentDependencyKey(jobKey, hard, parentKey, baseKey, debounceId)
  87. if parentKey then
  88. local parentDependenciesKey = parentKey .. ":dependencies"
  89. local result = rcall("SREM", parentDependenciesKey, jobKey)
  90. if result > 0 then
  91. local pendingDependencies = rcall("SCARD", parentDependenciesKey)
  92. if pendingDependencies == 0 then
  93. local parentId = getJobIdFromKey(parentKey)
  94. local parentPrefix = getJobKeyPrefix(parentKey, parentId)
  95. local numRemovedElements = rcall("ZREM", parentPrefix .. "waiting-children", parentId)
  96. if numRemovedElements == 1 then
  97. if hard then -- remove parent in same queue
  98. if parentPrefix == baseKey then
  99. removeParentDependencyKey(parentKey, hard, nil, baseKey, nil)
  100. removeJobKeys(parentKey)
  101. if debounceId then
  102. rcall("DEL", parentPrefix .. "de:" .. debounceId)
  103. end
  104. else
  105. _moveParentToWait(parentPrefix, parentId)
  106. end
  107. else
  108. _moveParentToWait(parentPrefix, parentId, true)
  109. end
  110. end
  111. end
  112. return true
  113. end
  114. else
  115. local parentAttributes = rcall("HMGET", jobKey, "parentKey", "deid")
  116. local missedParentKey = parentAttributes[1]
  117. if( (type(missedParentKey) == "string") and missedParentKey ~= ""
  118. and (rcall("EXISTS", missedParentKey) == 1)) then
  119. local parentDependenciesKey = missedParentKey .. ":dependencies"
  120. local result = rcall("SREM", parentDependenciesKey, jobKey)
  121. if result > 0 then
  122. local pendingDependencies = rcall("SCARD", parentDependenciesKey)
  123. if pendingDependencies == 0 then
  124. local parentId = getJobIdFromKey(missedParentKey)
  125. local parentPrefix = getJobKeyPrefix(missedParentKey, parentId)
  126. local numRemovedElements = rcall("ZREM", parentPrefix .. "waiting-children", parentId)
  127. if numRemovedElements == 1 then
  128. if hard then
  129. if parentPrefix == baseKey then
  130. removeParentDependencyKey(missedParentKey, hard, nil, baseKey, nil)
  131. removeJobKeys(missedParentKey)
  132. if parentAttributes[2] then
  133. rcall("DEL", parentPrefix .. "de:" .. parentAttributes[2])
  134. end
  135. else
  136. _moveParentToWait(parentPrefix, parentId)
  137. end
  138. else
  139. _moveParentToWait(parentPrefix, parentId, true)
  140. end
  141. end
  142. end
  143. return true
  144. end
  145. end
  146. end
  147. return false
  148. end
  149. if rcall("EXISTS", jobKey) ~= 1 then return -1 end
  150. if rcall("EXISTS", parentKey) ~= 1 then return -5 end
  151. if removeParentDependencyKey(jobKey, false, parentKey, KEYS[1], nil) then
  152. rcall("HDEL", jobKey, "parentKey", "parent")
  153. return 0
  154. else
  155. return 1
  156. end`;
  157. export const removeChildDependency = {
  158. name: 'removeChildDependency',
  159. content,
  160. keys: 1,
  161. };
  162. //# sourceMappingURL=removeChildDependency-1.js.map