Commit 7c8920c9 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Extract common logic to HashedStorage::BaseWorker

New class contains the ExclusiveLease specifics that is shared among
both the Migration and Rollback workers.
parent 4bae6100
......@@ -39,6 +39,8 @@ module Projects
if !from_exists && !to_exists
logger.warn "Can't find a repository on either source or target paths for #{project.full_path} (ID=#{project.id}) ..."
# We return true so we still reflect the change in the database.
# Next time the repository is (re)created it will be under the new storage layout
return true
elsif !from_exists
# Repository have been moved already.
......
# frozen_string_literal: true
module HashedStorage
class BaseWorker
include ExclusiveLeaseGuard
LEASE_TIMEOUT = 30.seconds.to_i
LEASE_KEY_SEGMENT = 'project_migrate_hashed_storage_worker'.freeze
protected
def lease_key
# we share the same lease key for both migration and rollback so they don't run simultaneously
"#{LEASE_KEY_SEGMENT}:#{project_id}"
end
def lease_timeout
LEASE_TIMEOUT
end
end
end
# frozen_string_literal: true
module HashedStorage
class ProjectMigrateWorker
class ProjectMigrateWorker < BaseWorker
include ApplicationWorker
LEASE_TIMEOUT = 30.seconds.to_i
LEASE_KEY_SEGMENT = 'project_migrate_hashed_storage_worker'.freeze
queue_namespace :hashed_storage
attr_reader :project_id
# rubocop: disable CodeReuse/ActiveRecord
def perform(project_id, old_disk_path = nil)
uuid = lease_for(project_id).try_obtain
@project_id = project_id # we need to set this in order to create the lease_key
if uuid
try_obtain_lease do
project = Project.without_deleted.find_by(id: project_id)
return unless project
break unless project
old_disk_path ||= project.disk_path
::Projects::HashedStorage::MigrationService.new(project, old_disk_path, logger: logger).execute
else
return false
end
ensure
cancel_lease_for(project_id, uuid) if uuid
end
# rubocop: enable CodeReuse/ActiveRecord
def lease_for(project_id)
Gitlab::ExclusiveLease.new(lease_key(project_id), timeout: LEASE_TIMEOUT)
end
private
def lease_key(project_id)
# we share the same lease key for both migration and rollback so they don't run simultaneously
"#{LEASE_KEY_SEGMENT}:#{project_id}"
end
def cancel_lease_for(project_id, uuid)
Gitlab::ExclusiveLease.cancel(lease_key(project_id), uuid)
end
end
end
# frozen_string_literal: true
module HashedStorage
class ProjectRollbackWorker
class ProjectRollbackWorker < BaseWorker
include ApplicationWorker
LEASE_TIMEOUT = 30.seconds.to_i
queue_namespace :hashed_storage
attr_reader :project_id
# rubocop: disable CodeReuse/ActiveRecord
def perform(project_id, old_disk_path = nil)
uuid = lease_for(project_id).try_obtain
@project_id = project_id # we need to set this in order to create the lease_key
if uuid
try_obtain_lease do
project = Project.without_deleted.find_by(id: project_id)
return unless project
break unless project
old_disk_path ||= project.disk_path
::Projects::HashedStorage::RollbackService.new(project, old_disk_path, logger: logger).execute
else
return false
end
ensure
cancel_lease_for(project_id, uuid) if uuid
end
# rubocop: enable CodeReuse/ActiveRecord
def lease_for(project_id)
Gitlab::ExclusiveLease.new(lease_key(project_id), timeout: LEASE_TIMEOUT)
end
private
def lease_key(project_id)
# we share the same lease key for both migration and rollback so they don't run simultaneously
"#{ProjectMigrateWorker::LEASE_KEY_SEGMENT}:#{project_id}"
end
def cancel_lease_for(project_id, uuid)
Gitlab::ExclusiveLease.cancel(lease_key(project_id), uuid)
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