Call Geo:: RepositoryBackfillService instead of worker

parent ee99b4d3
Pipeline #6884979 failed with stages
in 83 minutes and 42 seconds
module Geo module Geo
class RepositoryBackfillService class RepositoryBackfillService
attr_reader :project_id, :backfill_lease attr_reader :project_id
LEASE_TIMEOUT = 8.hours.freeze LEASE_TIMEOUT = 8.hours.freeze
LEASE_KEY_PREFIX = 'repository_backfill_service'.freeze LEASE_KEY_PREFIX = 'repository_backfill_service'.freeze
def initialize(project_id, backfill_lease) def initialize(project_id)
@project_id = project_id @project_id = project_id
@backfill_lease = backfill_lease
end end
def execute def execute
...@@ -19,9 +18,6 @@ module Geo ...@@ -19,9 +18,6 @@ module Geo
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
log('Releasing leases to sync repositories')
Gitlab::ExclusiveLease.cancel(LEASE_KEY_PREFIX, backfill_lease)
end end
private private
......
...@@ -27,7 +27,7 @@ class GeoBackfillWorker ...@@ -27,7 +27,7 @@ class GeoBackfillWorker
# one repo at a time. If we don't obtain the lease here, every 5 # one repo at a time. If we don't obtain the lease here, every 5
# minutes all of 100 projects will be synced. # minutes all of 100 projects will be synced.
try_obtain_lease do |lease| try_obtain_lease do |lease|
GeoSingleRepositoryBackfillWorker.new.perform(project_id, lease) Geo::RepositoryBackfillService.new(project_id).execute
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")
...@@ -65,7 +65,11 @@ class GeoBackfillWorker ...@@ -65,7 +65,11 @@ class GeoBackfillWorker
return unless lease return unless lease
yield lease begin
yield lease
ensure
Gitlab::ExclusiveLease.cancel(lease_key, lease)
end
end end
def lease_key def lease_key
......
class GeoSingleRepositoryBackfillWorker
include Sidekiq::Worker
include ::GeoDynamicBackoff
include GeoQueue
def perform(project_id, lease)
Geo::RepositoryBackfillService.new(project_id, lease).execute
end
end
...@@ -4,7 +4,7 @@ describe Geo::RepositoryBackfillService, services: true do ...@@ -4,7 +4,7 @@ describe Geo::RepositoryBackfillService, services: true do
let!(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') } let!(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') }
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
subject { described_class.new(project.id, '123456') } subject { described_class.new(project.id) }
describe '#execute' do describe '#execute' do
it 'fetches project repositories' do it 'fetches project repositories' do
...@@ -29,8 +29,8 @@ describe Geo::RepositoryBackfillService, services: true do ...@@ -29,8 +29,8 @@ describe Geo::RepositoryBackfillService, services: true do
subject.execute subject.execute
end end
it 'releases leases' do it 'releases lease' do
expect(Gitlab::ExclusiveLease).to receive(:cancel).twice.and_call_original expect(Gitlab::ExclusiveLease).to receive(:cancel).once.and_call_original
subject.execute subject.execute
end end
......
...@@ -12,32 +12,32 @@ describe Geo::GeoBackfillWorker, services: true do ...@@ -12,32 +12,32 @@ describe Geo::GeoBackfillWorker, services: true do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true } allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true }
end end
it 'performs GeoSingleRepositoryBackfillWorker for each project' do it 'performs Geo::RepositoryBackfillService for each project' do
expect(GeoSingleRepositoryBackfillWorker).to receive(:new).twice.and_return(spy) expect(Geo::RepositoryBackfillService).to receive(:new).twice.and_return(spy)
subject.perform subject.perform
end end
it 'does not perform GeoSingleRepositoryBackfillWorker when node is disabled' do it 'does not perform Geo::RepositoryBackfillService when node is disabled' do
allow_any_instance_of(GeoNode).to receive(:enabled?) { false } allow_any_instance_of(GeoNode).to receive(:enabled?) { false }
expect(GeoSingleRepositoryBackfillWorker).not_to receive(:new) expect(Geo::RepositoryBackfillService).not_to receive(:new)
subject.perform subject.perform
end end
it 'does not perform GeoSingleRepositoryBackfillWorker for projects that repository exists' do it 'does not perform Geo::RepositoryBackfillService for projects that repository exists' do
create_list(:project, 2) create_list(:project, 2)
expect(GeoSingleRepositoryBackfillWorker).to receive(:new).twice.and_return(spy) expect(Geo::RepositoryBackfillService).to receive(:new).twice.and_return(spy)
subject.perform subject.perform
end end
it 'does not perform GeoSingleRepositoryBackfillWorker when can not obtain a lease' do it 'does not perform Geo::RepositoryBackfillService when can not obtain a lease' do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { false } allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { false }
expect(GeoSingleRepositoryBackfillWorker).not_to receive(:new) expect(Geo::RepositoryBackfillService).not_to receive(:new)
subject.perform subject.perform
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