Add group restrictions to Geo::RepositorySyncWorker

parent ea0674fc
......@@ -16,24 +16,39 @@ module Geo
end
def load_pending_resources
project_ids_not_synced = find_project_ids_not_synced
project_ids_updated_recently = find_project_ids_updated_recently
restricted_project_ids = Gitlab::Geo.current_node.project_ids
project_ids_not_synced = find_project_ids_not_synced(restricted_project_ids)
project_ids_updated_recently = find_project_ids_updated_recently(restricted_project_ids)
interleave(project_ids_not_synced, project_ids_updated_recently)
end
def find_project_ids_not_synced
Project.where.not(id: Geo::ProjectRegistry.synced.pluck(:project_id))
.order(last_repository_updated_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id)
def find_project_ids_not_synced(restricted_project_ids)
relation =
if restricted_project_ids
Project.where(id: restricted_project_ids)
else
Project.all
end
relation.where.not(id: Geo::ProjectRegistry.synced.pluck(:project_id))
.order(last_repository_updated_at: :desc)
.limit(db_retrieve_batch_size)
.pluck(:id)
end
def find_project_ids_updated_recently
Geo::ProjectRegistry.dirty
.order(Gitlab::Database.nulls_first_order(:last_repository_synced_at, :desc))
.limit(db_retrieve_batch_size)
.pluck(:project_id)
def find_project_ids_updated_recently(restricted_project_ids)
relation =
if restricted_project_ids
Geo::ProjectRegistry.where(project_id: restricted_project_ids)
else
Geo::ProjectRegistry.all
end
relation.dirty
.order(Gitlab::Database.nulls_first_order(:last_repository_synced_at, :desc))
.limit(db_retrieve_batch_size)
.pluck(:project_id)
end
end
end
......@@ -3,7 +3,8 @@ require 'spec_helper'
describe Geo::RepositorySyncWorker do
let!(:primary) { create(:geo_node, :primary, host: 'primary-geo-node') }
let!(:secondary) { create(:geo_node, :current) }
let!(:project_1) { create(:project) }
let(:group) { create(:group) }
let!(:project_1) { create(:project, group: group) }
let!(:project_2) { create(:project) }
subject { described_class.new }
......@@ -62,5 +63,27 @@ describe Geo::RepositorySyncWorker do
subject.perform
end
context 'when node have group restrictions' do
before do
allow(Gitlab::Geo).to receive(:current_node).and_return(secondary)
secondary.update_attribute(:groups, [group])
end
it 'does not perform Geo::ProjectSyncWorker for projects that do not belong to selected groups to replicate' do
expect(Geo::ProjectSyncWorker).to receive(:perform_in).once.and_return(spy)
subject.perform
end
it 'does not perform Geo::ProjectSyncWorker for synced projects updated recently that do not belong to selected groups to replicate' do
create(:geo_project_registry, :synced, :repository_dirty, project: project_1)
create(:geo_project_registry, :synced, :repository_dirty, project: project_2)
expect(Geo::ProjectSyncWorker).to receive(:perform_in).once.and_return(spy)
subject.perform
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