Commit a180306d authored by Matija Čupić's avatar Matija Čupić

Use token from redis in gcp project billing worker

parent 2885dc06
require 'securerandom'
class CheckGcpProjectBillingWorker class CheckGcpProjectBillingWorker
include ApplicationWorker include ApplicationWorker
include ClusterQueue include ClusterQueue
LEASE_TIMEOUT = 15.seconds.to_i LEASE_TIMEOUT = 15.seconds.to_i
def self.generate_redis_token_key
SecureRandom.uuid
end
def self.redis_shared_state_key_for(token) def self.redis_shared_state_key_for(token)
"gitlab:gcp:#{token.hash}:billing_enabled" "gitlab:gcp:#{token.hash}:billing_enabled"
end end
def perform(token) def perform(token_key)
return unless token_key
token = get_token(token_key)
return unless token return unless token
return unless try_obtain_lease_for(token) return unless try_obtain_lease_for(token)
...@@ -20,6 +29,10 @@ class CheckGcpProjectBillingWorker ...@@ -20,6 +29,10 @@ class CheckGcpProjectBillingWorker
private private
def get_token(token_key)
Gitlab::Redis::SharedState.with { |redis| redis.get(token_key) }
end
def try_obtain_lease_for(token) def try_obtain_lease_for(token)
Gitlab::ExclusiveLease Gitlab::ExclusiveLease
.new("check_gcp_project_billing_worker:#{token.hash}", timeout: LEASE_TIMEOUT) .new("check_gcp_project_billing_worker:#{token.hash}", timeout: LEASE_TIMEOUT)
......
...@@ -3,33 +3,51 @@ require 'spec_helper' ...@@ -3,33 +3,51 @@ require 'spec_helper'
describe CheckGcpProjectBillingWorker do describe CheckGcpProjectBillingWorker do
describe '.perform' do describe '.perform' do
let(:token) { 'bogustoken' } let(:token) { 'bogustoken' }
subject { described_class.new.perform(token) } subject { described_class.new.perform('token_key') }
context 'when there is no lease' do context 'when there is a token in redis' do
before do before do
allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return('randomuuid') allow_any_instance_of(described_class).to receive(:get_token).and_return(token)
end end
it 'calls the service' do context 'when there is no lease' do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double]) before do
allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return('randomuuid')
end
subject it 'calls the service' do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
subject
end
it 'stores billing status in redis' do
redis_double = double
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
expect(redis_double).to receive(:set).with(described_class.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(described_class).to receive(:try_obtain_lease_for).and_return(false)
end
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double]) 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(described_class.redis_shared_state_key_for(token), anything)
subject subject
end
end end
end end
context 'when there is a lease' do context 'when there is no token in redis' do
before do before do
allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return(false) allow_any_instance_of(described_class).to receive(:get_token).and_return(nil)
end end
it 'does not call the service' do it 'does not call the service' do
......
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