fix exclusive lease specs fro hashed storage migration

This commit is contained in:
Gabriel Mazetto 2017-11-22 16:29:03 +01:00
parent 10c2ba7dbb
commit 05876a49c6
2 changed files with 15 additions and 9 deletions

View file

@ -8,23 +8,23 @@ class ProjectMigrateHashedStorageWorker
project = Project.find_by(id: project_id) project = Project.find_by(id: project_id)
return if project.nil? || project.pending_delete? return if project.nil? || project.pending_delete?
uuid = try_obtain_lease_for(project_id) uuid = lease_for(project_id).try_obtain
if uuid if uuid
::Projects::HashedStorageMigrationService.new(project, logger).execute ::Projects::HashedStorageMigrationService.new(project, logger).execute
else else
false false
end end
rescue => ex rescue => ex
cancel_lease_for(project_id, uuid) cancel_lease_for(project_id, uuid) if uuid
raise ex raise ex
end end
private def lease_for(project_id)
Gitlab::ExclusiveLease.new(lease_key(project_id), timeout: LEASE_TIMEOUT)
def try_obtain_lease_for(project_id)
Gitlab::ExclusiveLease.new(lease_key(project_id), timeout: LEASE_TIMEOUT).try_obtain
end end
private
def lease_key(project_id) def lease_key(project_id)
"project_migrate_hashed_storage_worker:#{project_id}" "project_migrate_hashed_storage_worker:#{project_id}"
end end

View file

@ -1,13 +1,16 @@
require 'spec_helper' require 'spec_helper'
describe ProjectMigrateHashedStorageWorker do describe ProjectMigrateHashedStorageWorker, :clean_gitlab_redis_shared_state do
describe '#perform' do describe '#perform' do
let(:project) { create(:project, :empty_repo) } let(:project) { create(:project, :empty_repo) }
let(:pending_delete_project) { create(:project, :empty_repo, pending_delete: true) } let(:pending_delete_project) { create(:project, :empty_repo, pending_delete: true) }
context 'when have exclusive lease' do context 'when have exclusive lease' do
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true) lease = subject.lease_for(project.id)
allow(Gitlab::ExclusiveLease).to receive(:new).and_return(lease)
allow(lease).to receive(:try_obtain).and_return(true)
end end
it 'skips when project no longer exists' do it 'skips when project no longer exists' do
@ -34,7 +37,10 @@ describe ProjectMigrateHashedStorageWorker do
context 'when dont have exclusive lease' do context 'when dont have exclusive lease' do
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false) lease = subject.lease_for(project.id)
allow(Gitlab::ExclusiveLease).to receive(:new).and_return(lease)
allow(lease).to receive(:try_obtain).and_return(false)
end end
it 'skips when dont have lease' do it 'skips when dont have lease' do