storeDeduplicatedNextJob.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. --[[
  2. Function to store a deduplicated next job if the existing job is active
  3. and keepLastIfActive is set. When the active job finishes, the stored
  4. proto-job is used to create a real job in the queue.
  5. Returns true if the proto-job was stored, false otherwise.
  6. ]]
  7. --- @include "checkItemInList"
  8. local function storeDeduplicatedNextJob(deduplicationOpts, currentDebounceJobId, prefix,
  9. deduplicationId, jobName, jobData, fullOpts, eventsKey, maxEvents, jobId,
  10. parentKey, parentData, parentDependenciesKey, repeatJobKey)
  11. if deduplicationOpts['keepLastIfActive'] and currentDebounceJobId then
  12. local activeKey = prefix .. "active"
  13. local activeItems = rcall('LRANGE', activeKey, 0, -1)
  14. if checkItemInList(activeItems, currentDebounceJobId) then
  15. local deduplicationNextKey = prefix .. "dn:" .. deduplicationId
  16. local fields = {'name', jobName, 'data', jobData, 'opts', cjson.encode(fullOpts)}
  17. if parentKey then
  18. fields[#fields+1] = 'pk'
  19. fields[#fields+1] = parentKey
  20. end
  21. if parentData then
  22. fields[#fields+1] = 'pd'
  23. fields[#fields+1] = parentData
  24. end
  25. if parentDependenciesKey then
  26. fields[#fields+1] = 'pdk'
  27. fields[#fields+1] = parentDependenciesKey
  28. end
  29. if repeatJobKey then
  30. fields[#fields+1] = 'rjk'
  31. fields[#fields+1] = repeatJobKey
  32. end
  33. rcall('HSET', deduplicationNextKey, unpack(fields))
  34. -- Ensure the dedup key does not expire while the job is active,
  35. -- so subsequent adds always hit the dedup path and never bypass
  36. -- the active-check because of a TTL expiry.
  37. local deduplicationKey = prefix .. "de:" .. deduplicationId
  38. rcall('PERSIST', deduplicationKey)
  39. -- TODO remove debounced event in next breaking change
  40. rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "debounced", "jobId",
  41. currentDebounceJobId, "debounceId", deduplicationId)
  42. rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "deduplicated", "jobId",
  43. currentDebounceJobId, "deduplicationId", deduplicationId, "deduplicatedJobId", jobId)
  44. return true
  45. end
  46. end
  47. return false
  48. end