Commit b546ade1 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre 🌴

Call Geo:: RepositoryBackfillService instead of worker

parent ee99b4d3
Pipeline #6884979 failed with stages
in 83 minutes and 42 seconds
module Geo
class RepositoryBackfillService
attr_reader :project_id, :backfill_lease
attr_reader :project_id
LEASE_TIMEOUT = 8.hours.freeze
LEASE_KEY_PREFIX = 'repository_backfill_service'.freeze
def initialize(project_id, backfill_lease)
def initialize(project_id)
@project_id = project_id
@backfill_lease = backfill_lease
end
def execute
......@@ -19,9 +18,6 @@ module Geo
end
rescue ActiveRecord::RecordNotFound
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
private
......
......@@ -27,7 +27,7 @@ class GeoBackfillWorker
# one repo at a time. If we don't obtain the lease here, every 5
# minutes all of 100 projects will be synced.
try_obtain_lease do |lease|
GeoSingleRepositoryBackfillWorker.new.perform(project_id, lease)
Geo::RepositoryBackfillService.new(project_id).execute
end
rescue ActiveRecord::RecordNotFound
logger.error("Couldn't find project with ID=#{project_id}, skipping syncing")
......@@ -65,7 +65,11 @@ class GeoBackfillWorker
return unless lease
yield lease
begin
yield lease
ensure
Gitlab::ExclusiveLease.cancel(lease_key, lease)
end
end
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
let!(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') }
let(:project) { create(:empty_project) }
subject { described_class.new(project.id, '123456') }
subject { described_class.new(project.id) }
describe '#execute' do
it 'fetches project repositories' do
......@@ -29,8 +29,8 @@ describe Geo::RepositoryBackfillService, services: true do
subject.execute
end
it 'releases leases' do
expect(Gitlab::ExclusiveLease).to receive(:cancel).twice.and_call_original
it 'releases lease' do
expect(Gitlab::ExclusiveLease).to receive(:cancel).once.and_call_original
subject.execute
end
......
......@@ -12,32 +12,32 @@ describe Geo::GeoBackfillWorker, services: true do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true }
end
it 'performs GeoSingleRepositoryBackfillWorker for each project' do
expect(GeoSingleRepositoryBackfillWorker).to receive(:new).twice.and_return(spy)
it 'performs Geo::RepositoryBackfillService for each project' do
expect(Geo::RepositoryBackfillService).to receive(:new).twice.and_return(spy)
subject.perform
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 }
expect(GeoSingleRepositoryBackfillWorker).not_to receive(:new)
expect(Geo::RepositoryBackfillService).not_to receive(:new)
subject.perform
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)
expect(GeoSingleRepositoryBackfillWorker).to receive(:new).twice.and_return(spy)
expect(Geo::RepositoryBackfillService).to receive(:new).twice.and_return(spy)
subject.perform
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 }
expect(GeoSingleRepositoryBackfillWorker).not_to receive(:new)
expect(Geo::RepositoryBackfillService).not_to receive(:new)
subject.perform
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