Module: Reenqueuer
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/workers/concerns/reenqueuer.rb
Overview
A concern that helps run exactly one instance of a worker, over and over, until it returns false or raises.
To ensure the worker is always up, you can schedule it every minute with sidekiq-cron. Excess jobs will immediately exit due to an exclusive lease.
The worker must define:
- `#perform`
- `#lease_timeout`
The worker spec should include ‘it_behaves_like ’reenqueuer’‘ and `it_behaves_like ’#perform is rate limited to 1 call per’‘.
Optionally override ‘#minimum_duration` to adjust the rate limit.
When ‘#perform` returns false, the job will not be reenqueued. Instead, we will wait for the next one scheduled by sidekiq-cron.
#lease_timeout should be longer than the longest possible ‘#perform`. The lease is normally released in an ensure block, but it is possible to orphan the lease by killing Sidekiq, so it should also be as short as possible. Consider that long-running jobs are generally not recommended. Ideally, every job finishes within 25 seconds because that is the default wait time for graceful termination.
Timing: It runs as often as Sidekiq allows. We rate limit with sleep for now: gitlab.com/gitlab-org/gitlab/issues/121697
Defined Under Namespace
Modules: ReenqueuerSleeper
Instance Method Summary collapse
Instance Method Details
#perform(*args) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/workers/concerns/reenqueuer.rb', line 43 def perform(*args) set_custom_lease_key(*args) if self.respond_to?(:set_custom_lease_key) try_obtain_lease do reenqueue(*args) do ensure_minimum_duration(minimum_duration) do super end end end end |