summaryrefslogtreecommitdiff
path: root/app/workers/gitlab_usage_ping_worker.rb
blob: a5e22f88a3b7948c3891fea0f0fb50c8899dc741 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true

class GitlabUsagePingWorker
  LEASE_TIMEOUT = 86400

  include ApplicationWorker
  include CronjobQueue

  # Retry for up to approximately three hours then give up.
  sidekiq_options retry: 10, dead: false

  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

  def try_obtain_lease
    Gitlab::ExclusiveLease.new('gitlab_usage_ping_worker:ping', timeout: LEASE_TIMEOUT).try_obtain
  end
end