Refactoring Geo::RepositoryBackfillService

parent cbbb506a
module EE
# Repository EE mixin
#
# This module is intended to encapsulate EE-specific model logic
# and be prepended in the `Repository` model
module Repository
extend ActiveSupport::Concern
# Runs code after a repository has been synced.
def after_sync
expire_all_method_caches
expire_branch_cache
expire_content_cache
end
end
end
...@@ -6,6 +6,7 @@ class Repository ...@@ -6,6 +6,7 @@ class Repository
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
include Elastic::RepositoriesSearch include Elastic::RepositoriesSearch
include RepositoryMirroring include RepositoryMirroring
prepend EE::Repository
attr_accessor :path_with_namespace, :project attr_accessor :path_with_namespace, :project
...@@ -426,13 +427,6 @@ class Repository ...@@ -426,13 +427,6 @@ class Repository
expire_branches_cache expire_branches_cache
end end
# Runs code after a repository has been synced.
def after_sync
expire_all_method_caches
expire_branch_cache
expire_content_cache
end
# Runs code after a new commit has been pushed. # Runs code after a new commit has been pushed.
def after_push_commit(branch_name) def after_push_commit(branch_name)
expire_statistics_caches expire_statistics_caches
......
...@@ -13,16 +13,14 @@ module Geo ...@@ -13,16 +13,14 @@ module Geo
def execute def execute
try_obtain_lease do try_obtain_lease do
log('Started repository sync') log('Started repository sync')
started_at, finished_at = fetch_repositories
fetch_repositories do |started_at, finished_at| update_registry(started_at, finished_at)
update_registry(started_at, finished_at)
end
log('Finished repository sync') log('Finished repository sync')
end end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
logger.error("Couldn't find project with ID=#{project_id}, skipping syncing") logger.error("Couldn't find project with ID=#{project_id}, skipping syncing")
ensure ensure
log('Releasing leases to sync repositories')
Gitlab::ExclusiveLease.cancel(LEASE_KEY_PREFIX, backfill_lease) Gitlab::ExclusiveLease.cancel(LEASE_KEY_PREFIX, backfill_lease)
end end
...@@ -46,7 +44,7 @@ module Geo ...@@ -46,7 +44,7 @@ module Geo
Rails.logger.error "Error syncing repository for project #{project.path_with_namespace}: #{e}" Rails.logger.error "Error syncing repository for project #{project.path_with_namespace}: #{e}"
end end
yield started_at, finished_at [started_at, finished_at]
end end
def fetch_project_repository def fetch_project_repository
...@@ -80,6 +78,9 @@ module Geo ...@@ -80,6 +78,9 @@ module Geo
yield yield
# We should release the lease for a repository, only if we have obtained
# it. If something went wrong when syncing the repository, we should wait
# for the lease timeout to try again.
log('Releasing leases to sync repository') log('Releasing leases to sync repository')
Gitlab::ExclusiveLease.cancel(lease_key, repository_lease) Gitlab::ExclusiveLease.cancel(lease_key, repository_lease)
end end
...@@ -93,7 +94,7 @@ module Geo ...@@ -93,7 +94,7 @@ module Geo
end end
def lease_key def lease_key
@key ||= "#{LEASE_KEY_PREFIX}:#{project.id}" @lease_key ||= "#{LEASE_KEY_PREFIX}:#{project.id}"
end end
def primary_ssh_path_prefix def primary_ssh_path_prefix
......
...@@ -1972,16 +1972,6 @@ describe Repository, models: true do ...@@ -1972,16 +1972,6 @@ describe Repository, models: true do
end end
end end
describe '#after_sync' do
it 'expires repository cache' do
expect(repository).to receive(:expire_all_method_caches)
expect(repository).to receive(:expire_branch_cache)
expect(repository).to receive(:expire_content_cache)
repository.after_sync
end
end
def create_remote_branch(remote_name, branch_name, target) def create_remote_branch(remote_name, branch_name, target)
rugged = repository.rugged rugged = repository.rugged
rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", target.id) rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", target.id)
......
...@@ -22,7 +22,9 @@ describe Geo::RepositoryBackfillService, services: true do ...@@ -22,7 +22,9 @@ describe Geo::RepositoryBackfillService, services: true do
it 'expires repository caches' do it 'expires repository caches' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { true } allow_any_instance_of(Repository).to receive(:fetch_geo_mirror) { true }
expect_any_instance_of(Repository).to receive(:after_sync).once expect_any_instance_of(Repository).to receive(:expire_all_method_caches).once
expect_any_instance_of(Repository).to receive(:expire_branch_cache).once
expect_any_instance_of(Repository).to receive(:expire_content_cache).once
subject.execute subject.execute
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