Commit cc8ad1bf authored by Gabriel Mazetto's avatar Gabriel Mazetto

Codestyle changes and Added Exclusive Lease to hashed storage migration

parent ff462603
...@@ -33,7 +33,7 @@ module Projects ...@@ -33,7 +33,7 @@ module Projects
end end
# Create hashed storage base path folder # Create hashed storage base path folder
FileUtils.mkdir_p(File.expand_path('..', new_path)) FileUtils.mkdir_p(File.dirname(new_path))
FileUtils.mv(old_path, new_path) FileUtils.mv(old_path, new_path)
logger.info("Migrated project attachments from '#{old_path}' to '#{new_path}' (PROJECT_ID=#{project.id})") logger.info("Migrated project attachments from '#{old_path}' to '#{new_path}' (PROJECT_ID=#{project.id})")
......
...@@ -2,10 +2,34 @@ class ProjectMigrateHashedStorageWorker ...@@ -2,10 +2,34 @@ class ProjectMigrateHashedStorageWorker
include Sidekiq::Worker include Sidekiq::Worker
include DedicatedSidekiqQueue include DedicatedSidekiqQueue
LEASE_TIMEOUT = 30.seconds.to_i
def perform(project_id) def perform(project_id)
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)
if uuid
::Projects::HashedStorageMigrationService.new(project, logger).execute ::Projects::HashedStorageMigrationService.new(project, logger).execute
else
false
end
rescue => ex
cancel_lease_for(project_id, uuid)
raise ex
end
private
def try_obtain_lease_for(project_id)
Gitlab::ExclusiveLease.new(lease_key(project_id), timeout: LEASE_TIMEOUT).try_obtain
end
def lease_key(project_id)
"project_migrate_hashed_storage_worker:#{project_id}"
end
def cancel_lease_for(project_id, uuid)
Gitlab::ExclusiveLease.cancel(lease_key(project_id), uuid)
end end
end end
...@@ -5,6 +5,11 @@ describe ProjectMigrateHashedStorageWorker do ...@@ -5,6 +5,11 @@ describe ProjectMigrateHashedStorageWorker 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
before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true)
end
it 'skips when project no longer exists' do it 'skips when project no longer exists' do
nonexistent_id = 999999999999 nonexistent_id = 999999999999
...@@ -26,4 +31,17 @@ describe ProjectMigrateHashedStorageWorker do ...@@ -26,4 +31,17 @@ describe ProjectMigrateHashedStorageWorker do
subject.perform(project.id) subject.perform(project.id)
end end
end end
context 'when dont have exclusive lease' do
before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false)
end
it 'skips when dont have lease' do
expect(::Projects::HashedStorageMigrationService).not_to receive(:new)
subject.perform(project.id)
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