BigW Consortium Gitlab

Add lease to CheckGcpProjectBillingWorker

parent 78f85f3f
class CheckGcpProjectBillingWorker class CheckGcpProjectBillingWorker
include ApplicationWorker include ApplicationWorker
LEASE_TIMEOUT = 1.minute.to_i
def self.redis_shared_state_key_for(token) def self.redis_shared_state_key_for(token)
"gitlab:gcp:#{token}:billing_enabled" "gitlab:gcp:#{token}:billing_enabled"
end end
def perform(token) def perform(token)
return unless token return unless token
return unless try_obtain_lease_for(token)
billing_enabled = CheckGcpProjectBillingService.new.execute(token) billing_enabled = CheckGcpProjectBillingService.new.execute(token)
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
redis.set(self.class.redis_shared_state_key_for(token), billing_enabled) redis.set(self.class.redis_shared_state_key_for(token), billing_enabled)
end end
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 end
...@@ -5,20 +5,38 @@ describe CheckGcpProjectBillingWorker do ...@@ -5,20 +5,38 @@ describe CheckGcpProjectBillingWorker do
let(:token) { 'bogustoken' } let(:token) { 'bogustoken' }
subject { described_class.new.perform(token) } subject { described_class.new.perform(token) }
it 'calls the service' do context 'when there is no lease' do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute) before do
allow_any_instance_of(CheckGcpProjectBillingWorker).to receive(:try_obtain_lease_for).and_return('randomuuid')
end
subject it 'calls the service' do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute)
subject
end
it 'stores billing status in redis' do
redis_double = double
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return(true)
expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
expect(redis_double).to receive(:set).with(CheckGcpProjectBillingWorker.redis_shared_state_key_for(token), anything)
subject
end
end end
it 'stores billing status in redis' do context 'when there is a lease' do
redis_double = double before do
allow_any_instance_of(CheckGcpProjectBillingWorker).to receive(:try_obtain_lease_for).and_return(false)
end
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return(true) it 'does not call the service' do
expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double) expect(CheckGcpProjectBillingService).not_to receive(:new)
expect(redis_double).to receive(:set).with(CheckGcpProjectBillingWorker.redis_shared_state_key_for(token), anything)
subject subject
end
end end
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