summaryrefslogtreecommitdiff
path: root/app/workers/check_gcp_project_billing_worker.rb
blob: 254b0959063c2dac994ee7e8d0ff8baf1c292149 (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
class CheckGcpProjectBillingWorker
  include ApplicationWorker

  LEASE_TIMEOUT = 1.minute.to_i

  def self.redis_shared_state_key_for(token)
    "gitlab:gcp:#{token}:billing_enabled"
  end

  def perform(token)
    return unless token
    return unless try_obtain_lease_for(token)

    billing_enabled = CheckGcpProjectBillingService.new.execute(token)
    Gitlab::Redis::SharedState.with do |redis|
      redis.set(self.class.redis_shared_state_key_for(token), billing_enabled)
    end
  end

  private

  def try_obtain_lease_for(token)
    Gitlab::ExclusiveLease
      .new("check_gcp_project_billing_worker:#{token}", timeout: LEASE_TIMEOUT)
      .try_obtain
  end
end