diff options
Diffstat (limited to 'app/workers/gitlab_usage_ping_worker.rb')
-rw-r--r-- | app/workers/gitlab_usage_ping_worker.rb | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/app/workers/gitlab_usage_ping_worker.rb b/app/workers/gitlab_usage_ping_worker.rb index 9f0cf1728dd..a696c6e746a 100644 --- a/app/workers/gitlab_usage_ping_worker.rb +++ b/app/workers/gitlab_usage_ping_worker.rb @@ -1,32 +1,24 @@ # frozen_string_literal: true class GitlabUsagePingWorker # rubocop:disable Scalability/IdempotentWorker + LEASE_KEY = 'gitlab_usage_ping_worker:ping' LEASE_TIMEOUT = 86400 include ApplicationWorker - # rubocop:disable Scalability/CronWorkerContext - # This worker does not perform work scoped to a context - include CronjobQueue - # rubocop:enable Scalability/CronWorkerContext + include CronjobQueue # rubocop:disable Scalability/CronWorkerContext + include Gitlab::ExclusiveLeaseHelpers feature_category :collection - - # Retry for up to approximately three hours then give up. - sidekiq_options retry: 10, dead: false + sidekiq_options retry: 3, dead: false + sidekiq_retry_in { |count| (count + 1) * 8.hours.to_i } def perform # Multiple Sidekiq workers could run this. We should only do this at most once a day. - return unless try_obtain_lease - - # Splay the request over a minute to avoid thundering herd problems. - sleep(rand(0.0..60.0).round(3)) - - SubmitUsagePingService.new.execute - end - - private + in_lock(LEASE_KEY, ttl: LEASE_TIMEOUT) do + # Splay the request over a minute to avoid thundering herd problems. + sleep(rand(0.0..60.0).round(3)) - def try_obtain_lease - Gitlab::ExclusiveLease.new('gitlab_usage_ping_worker:ping', timeout: LEASE_TIMEOUT).try_obtain + SubmitUsagePingService.new.execute + end end end |