Class: GitlabServicePingWorker

Inherits:
Object
  • Object
show all
Includes:
ApplicationWorker, CronjobQueue, Gitlab::ExclusiveLeaseHelpers
Defined in:
app/workers/gitlab_service_ping_worker.rb

Overview

rubocop:disable Scalability/IdempotentWorker

Constant Summary collapse

LEASE_KEY =
'gitlab_service_ping_worker:ping'
LEASE_TIMEOUT =
86400

Constants included from Gitlab::ExclusiveLeaseHelpers

Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError

Constants included from ApplicationWorker

ApplicationWorker::LOGGING_EXTRA_KEY, ApplicationWorker::SAFE_PUSH_BULK_LIMIT

Constants included from Gitlab::Loggable

Gitlab::Loggable::ANONYMOUS

Constants included from WorkerAttributes

WorkerAttributes::DEFAULT_DATA_CONSISTENCY, WorkerAttributes::DEFAULT_DEFER_DELAY, WorkerAttributes::NAMESPACE_WEIGHTS, WorkerAttributes::VALID_DATA_CONSISTENCIES, WorkerAttributes::VALID_RESOURCE_BOUNDARIES, WorkerAttributes::VALID_URGENCIES

Instance Method Summary collapse

Methods included from Gitlab::ExclusiveLeaseHelpers

#in_lock

Methods included from Gitlab::Loggable

#build_structured_payload

Methods included from Gitlab::SidekiqVersioning::Worker

#job_version

Methods included from WorkerContext

#with_context

Instance Method Details

#perform(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/workers/gitlab_service_ping_worker.rb', line 18

def perform(options = {})
  # Sidekiq does not support keyword arguments, so the args need to be
  # passed the old pre-Ruby 2.0 way.
  #
  # See https://github.com/mperham/sidekiq/issues/2372
  triggered_from_cron = options.fetch('triggered_from_cron', true)
  skip_db_write = options.fetch('skip_db_write', false)

  # Disable service ping for GitLab.com unless called manually
  # See https://gitlab.com/gitlab-org/gitlab/-/issues/292929 for details
  return if Gitlab.com? && triggered_from_cron

  # Multiple Sidekiq workers could run this. We should only do this at most once a day.
  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))

    ServicePing::SubmitService.new(payload: usage_data, skip_db_write: skip_db_write).execute
  end
end

#usage_dataObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/workers/gitlab_service_ping_worker.rb', line 39

def usage_data
  ServicePing::BuildPayload.new.execute.tap do |payload|
    record = {
      recorded_at: payload[:recorded_at],
      payload: payload,
      created_at: Time.current,
      updated_at: Time.current
    }

    RawUsageData.upsert(record, unique_by: :recorded_at)
  end
rescue StandardError => err
  Gitlab::ErrorTracking.track_and_raise_for_dev_exception(err)
  nil
end