moveToWaitingChildren-7.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const content = `--[[
  2. Moves job from active to waiting children set.
  3. Input:
  4. KEYS[1] active key
  5. KEYS[2] wait-children key
  6. KEYS[3] job key
  7. KEYS[4] job dependencies key
  8. KEYS[5] job unsuccessful key
  9. KEYS[6] stalled key
  10. KEYS[7] events key
  11. ARGV[1] token
  12. ARGV[2] child key
  13. ARGV[3] timestamp
  14. ARGV[4] jobId
  15. ARGV[5] prefix
  16. Output:
  17. 0 - OK
  18. 1 - There are not pending dependencies.
  19. -1 - Missing job.
  20. -2 - Missing lock
  21. -3 - Job not in active set
  22. -9 - Job has failed children
  23. ]]
  24. local rcall = redis.call
  25. local activeKey = KEYS[1]
  26. local waitingChildrenKey = KEYS[2]
  27. local jobKey = KEYS[3]
  28. local jobDependenciesKey = KEYS[4]
  29. local jobUnsuccessfulKey = KEYS[5]
  30. local stalledKey = KEYS[6]
  31. local eventStreamKey = KEYS[7]
  32. local token = ARGV[1]
  33. local timestamp = ARGV[3]
  34. local jobId = ARGV[4]
  35. --- Includes
  36. local function removeLock(jobKey, stalledKey, token, jobId)
  37. if token ~= "0" then
  38. local lockKey = jobKey .. ':lock'
  39. local lockToken = rcall("GET", lockKey)
  40. if lockToken == token then
  41. rcall("DEL", lockKey)
  42. rcall("SREM", stalledKey, jobId)
  43. else
  44. if lockToken then
  45. -- Lock exists but token does not match
  46. return -6
  47. else
  48. -- Lock is missing completely
  49. return -2
  50. end
  51. end
  52. end
  53. return 0
  54. end
  55. local function removeJobFromActive(activeKey, stalledKey, jobKey, jobId,
  56. token)
  57. local errorCode = removeLock(jobKey, stalledKey, token, jobId)
  58. if errorCode < 0 then
  59. return errorCode
  60. end
  61. local numRemovedElements = rcall("LREM", activeKey, -1, jobId)
  62. if numRemovedElements < 1 then
  63. return -3
  64. end
  65. return 0
  66. end
  67. local function moveToWaitingChildren(activeKey, waitingChildrenKey, stalledKey, eventStreamKey,
  68. jobKey, jobId, timestamp, token)
  69. local errorCode = removeJobFromActive(activeKey, stalledKey, jobKey, jobId, token)
  70. if errorCode < 0 then
  71. return errorCode
  72. end
  73. local score = tonumber(timestamp)
  74. rcall("ZADD", waitingChildrenKey, score, jobId)
  75. rcall("XADD", eventStreamKey, "*", "event", "waiting-children", "jobId", jobId, 'prev', 'active')
  76. return 0
  77. end
  78. if rcall("EXISTS", jobKey) == 1 then
  79. if rcall("ZCARD", jobUnsuccessfulKey) ~= 0 then
  80. return -9
  81. else
  82. if ARGV[2] ~= "" then
  83. if rcall("SISMEMBER", jobDependenciesKey, ARGV[2]) ~= 0 then
  84. return moveToWaitingChildren(activeKey, waitingChildrenKey, stalledKey, eventStreamKey,
  85. jobKey, jobId, timestamp, token)
  86. end
  87. return 1
  88. else
  89. if rcall("SCARD", jobDependenciesKey) ~= 0 then
  90. return moveToWaitingChildren(activeKey, waitingChildrenKey, stalledKey, eventStreamKey,
  91. jobKey, jobId, timestamp, token)
  92. end
  93. return 1
  94. end
  95. end
  96. end
  97. return -1
  98. `;
  99. export const moveToWaitingChildren = {
  100. name: 'moveToWaitingChildren',
  101. content,
  102. keys: 7,
  103. };
  104. //# sourceMappingURL=moveToWaitingChildren-7.js.map