removeChildDependency-1.js 6.0 KB

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