moveToWaitingChildren-7.js 3.0 KB

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