BigW Consortium Gitlab

Add lease to CheckGcpProjectBillingWorker

parent 78f85f3f
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
......@@ -5,6 +5,11 @@ describe CheckGcpProjectBillingWorker do
let(:token) { 'bogustoken' }
subject { described_class.new.perform(token) }
context 'when there is no lease' do
before do
allow_any_instance_of(CheckGcpProjectBillingWorker).to receive(:try_obtain_lease_for).and_return('randomuuid')
end
it 'calls the service' do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute)
......@@ -21,4 +26,17 @@ describe CheckGcpProjectBillingWorker do
subject
end
end
context 'when there is a lease' do
before do
allow_any_instance_of(CheckGcpProjectBillingWorker).to receive(:try_obtain_lease_for).and_return(false)
end
it 'does not call the service' do
expect(CheckGcpProjectBillingService).not_to receive(:new)
subject
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment