deduplicateJobWithoutReplace.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. --[[
  2. Function to deduplicate a job.
  3. ]]
  4. --- @include "setDeduplicationKey"
  5. --- @include "storeDeduplicatedNextJob"
  6. local function deduplicateJobWithoutReplace(deduplicationId, deduplicationOpts, jobId, deduplicationKey,
  7. eventsKey, maxEvents, prefix, jobName, jobData, fullOpts,
  8. parentKey, parentData, parentDependenciesKey, repeatJobKey)
  9. local ttl = deduplicationOpts['ttl']
  10. local deduplicationKeyExists
  11. if ttl and ttl > 0 then
  12. if deduplicationOpts['extend'] then
  13. local currentDebounceJobId = rcall('GET', deduplicationKey)
  14. if currentDebounceJobId then
  15. if storeDeduplicatedNextJob(deduplicationOpts, currentDebounceJobId, prefix,
  16. deduplicationId, jobName, jobData, fullOpts, eventsKey, maxEvents, jobId,
  17. parentKey, parentData, parentDependenciesKey, repeatJobKey) then
  18. return currentDebounceJobId
  19. end
  20. if deduplicationOpts['keepLastIfActive'] then
  21. rcall('SET', deduplicationKey, currentDebounceJobId)
  22. else
  23. setDeduplicationKey(deduplicationKey, currentDebounceJobId, deduplicationOpts)
  24. end
  25. rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "debounced",
  26. "jobId", currentDebounceJobId, "debounceId", deduplicationId)
  27. rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "deduplicated", "jobId",
  28. currentDebounceJobId, "deduplicationId", deduplicationId, "deduplicatedJobId", jobId)
  29. return currentDebounceJobId
  30. else
  31. if deduplicationOpts['keepLastIfActive'] then
  32. rcall('SET', deduplicationKey, jobId)
  33. else
  34. setDeduplicationKey(deduplicationKey, jobId, deduplicationOpts)
  35. end
  36. return
  37. end
  38. else
  39. if deduplicationOpts['keepLastIfActive'] then
  40. deduplicationKeyExists = not rcall('SET', deduplicationKey, jobId, 'NX')
  41. else
  42. deduplicationKeyExists = not rcall('SET', deduplicationKey, jobId, 'PX', ttl, 'NX')
  43. end
  44. end
  45. else
  46. deduplicationKeyExists = not rcall('SET', deduplicationKey, jobId, 'NX')
  47. end
  48. if deduplicationKeyExists then
  49. local currentDebounceJobId = rcall('GET', deduplicationKey)
  50. if storeDeduplicatedNextJob(deduplicationOpts, currentDebounceJobId, prefix,
  51. deduplicationId, jobName, jobData, fullOpts, eventsKey, maxEvents, jobId,
  52. parentKey, parentData, parentDependenciesKey, repeatJobKey) then
  53. return currentDebounceJobId
  54. end
  55. -- TODO remove debounced event in next breaking change
  56. rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "debounced", "jobId",
  57. currentDebounceJobId, "debounceId", deduplicationId)
  58. rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "deduplicated", "jobId",
  59. currentDebounceJobId, "deduplicationId", deduplicationId, "deduplicatedJobId", jobId)
  60. return currentDebounceJobId
  61. end
  62. end