Module: RubyReactor::SweeperJob::ClassMethods

Defined in:
lib/ruby_reactor/sweeper_job.rb

Instance Method Summary collapse

Instance Method Details

#schedule_nextObject

Enqueue the next tick for the upcoming time window, claiming that window so concurrent/duplicate/recovered ticks produce exactly one successor. Idempotent: also safe to call from start_sweeper! on every process boot.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_reactor/sweeper_job.rb', line 49

def schedule_next
  interval = RubyReactor.configuration.sweeper_interval
  window = (Time.now.to_i / interval) + 1

  lock = RubyReactor::Lock.new(
    "sweeper:window:#{window}",
    owner: SecureRandom.uuid,
    ttl: interval * 2, # outlive the window; expires on its own (never released)
    wait: 0,
    auto_extend: false
  )
  lock.acquire # raises AcquisitionError if this window is already claimed

  delay = (window * interval) - Time.now.to_i
  perform_in([delay, 1].max)
rescue RubyReactor::Lock::AcquisitionError
  # Another tick already scheduled this window — collapse the duplicate.
  nil
end