Store the time the sync started before fetching the repositories

parent 9d3b8832
...@@ -33,8 +33,7 @@ module Geo ...@@ -33,8 +33,7 @@ module Geo
def sync_project_repository def sync_project_repository
return unless sync_repository? return unless sync_repository?
started_at, finished_at = fetch_project_repository fetch_project_repository
update_registry(:repository, started_at, finished_at)
expire_repository_caches expire_repository_caches
end end
...@@ -47,8 +46,7 @@ module Geo ...@@ -47,8 +46,7 @@ module Geo
def sync_wiki_repository def sync_wiki_repository
return unless sync_wiki? return unless sync_wiki?
started_at, finished_at = fetch_wiki_repository fetch_wiki_repository
update_registry(:wiki, started_at, finished_at)
end end
def sync_wiki? def sync_wiki?
...@@ -58,17 +56,14 @@ module Geo ...@@ -58,17 +56,14 @@ module Geo
end end
def fetch_project_repository def fetch_project_repository
return unless sync_repository?
log('Fetching project repository') log('Fetching project repository')
started_at = DateTime.now update_registry(:repository, started_at: DateTime.now)
finished_at = nil
begin begin
project.ensure_repository project.ensure_repository
project.repository.fetch_geo_mirror(ssh_url_to_repo) project.repository.fetch_geo_mirror(ssh_url_to_repo)
finished_at = DateTime.now update_registry(:repository, finished_at: DateTime.now)
rescue Gitlab::Shell::Error => e rescue Gitlab::Shell::Error => e
Rails.logger.error("#{self.class.name}: Error syncing repository for project #{project.path_with_namespace}: #{e}") Rails.logger.error("#{self.class.name}: Error syncing repository for project #{project.path_with_namespace}: #{e}")
rescue Gitlab::Git::Repository::NoRepository => e rescue Gitlab::Git::Repository::NoRepository => e
...@@ -76,27 +71,20 @@ module Geo ...@@ -76,27 +71,20 @@ module Geo
log('Expiring caches') log('Expiring caches')
project.repository.after_create project.repository.after_create
end end
[started_at, finished_at]
end end
def fetch_wiki_repository def fetch_wiki_repository
return unless sync_wiki?
log('Fetching wiki repository') log('Fetching wiki repository')
started_at = DateTime.now update_registry(:wiki, started_at: DateTime.now)
finished_at = nil
begin begin
project.wiki.ensure_repository project.wiki.ensure_repository
project.wiki.repository.fetch_geo_mirror(ssh_url_to_wiki) project.wiki.repository.fetch_geo_mirror(ssh_url_to_wiki)
finished_at = DateTime.now update_registry(:wiki, finished_at: DateTime.now)
rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error, ProjectWiki::CouldNotCreateWikiError => e rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error, ProjectWiki::CouldNotCreateWikiError => e
Rails.logger.error("#{self.class.name}: Error syncing wiki repository for project #{project.path_with_namespace}: #{e}") Rails.logger.error("#{self.class.name}: Error syncing wiki repository for project #{project.path_with_namespace}: #{e}")
end end
[started_at, finished_at]
end end
def expire_repository_caches def expire_repository_caches
...@@ -122,9 +110,14 @@ module Geo ...@@ -122,9 +110,14 @@ module Geo
Gitlab::ExclusiveLease.cancel(lease_key, repository_lease) Gitlab::ExclusiveLease.cancel(lease_key, repository_lease)
end end
def update_registry(type, started_at, finished_at) def update_registry(type, started_at: nil, finished_at: nil)
return unless started_at || finished_at
log("Updating #{type} sync information") log("Updating #{type} sync information")
registry.public_send("last_#{type}_synced_at=", started_at)
if started_at
registry.public_send("last_#{type}_synced_at=", started_at)
end
if finished_at if finished_at
registry.public_send("last_#{type}_successful_sync_at=", finished_at) registry.public_send("last_#{type}_successful_sync_at=", finished_at)
......
...@@ -30,6 +30,7 @@ class GeoRepositorySyncWorker ...@@ -30,6 +30,7 @@ class GeoRepositorySyncWorker
try_obtain_lease do |lease| try_obtain_lease do |lease|
Geo::RepositorySyncService.new(project_id).execute Geo::RepositorySyncService.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")
next next
......
...@@ -59,40 +59,72 @@ describe Geo::RepositorySyncService, services: true do ...@@ -59,40 +59,72 @@ describe Geo::RepositorySyncService, services: true do
expect { subject.execute }.to change(Geo::ProjectRegistry, :count).by(1) expect { subject.execute }.to change(Geo::ProjectRegistry, :count).by(1)
end end
it 'sets last_repository_successful_sync_at when repository sync succeed' do context 'when repository sync succeed' do
subject.execute let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) }
registry = Geo::ProjectRegistry.find_by(project_id: project.id) before do
subject.execute
end
expect(registry.last_repository_successful_sync_at).not_to be_nil it 'sets last_repository_synced_at' do
expect(registry.last_repository_synced_at).not_to be_nil
end
it 'sets last_repository_successful_sync_at' do
expect(registry.last_repository_successful_sync_at).not_to be_nil
end
end end
it 'resets last_repository_successful_sync_at when repository sync fail' do context 'when repository sync fail' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.git/) { raise Gitlab::Shell::Error } let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) }
subject.execute before do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.git/) { raise Gitlab::Shell::Error }
subject.execute
end
registry = Geo::ProjectRegistry.find_by(project_id: project.id) it 'sets last_repository_synced_at' do
expect(registry.last_repository_synced_at).not_to be_nil
end
expect(registry.last_repository_successful_sync_at).to be_nil it 'resets last_repository_successful_sync_at' do
expect(registry.last_repository_successful_sync_at).to be_nil
end
end end
it 'sets last_wiki_successful_sync_at when wiki sync succeed' do context 'when wiki sync succeed' do
subject.execute let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) }
registry = Geo::ProjectRegistry.find_by(project_id: project.id) before do
subject.execute
end
expect(registry.last_wiki_successful_sync_at).not_to be_nil it 'sets last_wiki_synced_at' do
expect(registry.last_wiki_synced_at).not_to be_nil
end
it 'sets last_wiki_successful_sync_at' do
expect(registry.last_wiki_successful_sync_at).not_to be_nil
end
end end
it 'resets last_wiki_successful_sync_at when wiki sync fail' do context 'when wiki sync fail' do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.wiki.git/) { raise Gitlab::Shell::Error } let(:registry) { Geo::ProjectRegistry.find_by(project_id: project.id) }
subject.execute before do
allow_any_instance_of(Repository).to receive(:fetch_geo_mirror).with(/#{project.path_with_namespace}\.wiki.git/) { raise Gitlab::Shell::Error }
subject.execute
end
registry = Geo::ProjectRegistry.find_by(project_id: project.id) it 'sets last_wiki_synced_at' do
expect(registry.last_wiki_synced_at).not_to be_nil
end
expect(registry.last_wiki_successful_sync_at).to be_nil it 'resets last_wiki_successful_sync_at' do
expect(registry.last_wiki_successful_sync_at).to be_nil
end
end end
end end
end end
...@@ -122,7 +154,7 @@ describe Geo::RepositorySyncService, services: true do ...@@ -122,7 +154,7 @@ describe Geo::RepositorySyncService, services: true do
expect { subject.execute }.not_to change(Geo::ProjectRegistry, :count) expect { subject.execute }.not_to change(Geo::ProjectRegistry, :count)
end end
it 'does not update last_repository_successful_sync_at' do it 'does not update last repository sync times' do
subject.execute subject.execute
registry.reload registry.reload
...@@ -131,7 +163,7 @@ describe Geo::RepositorySyncService, services: true do ...@@ -131,7 +163,7 @@ describe Geo::RepositorySyncService, services: true do
expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(last_repository_synced_at) expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(last_repository_synced_at)
end end
it 'does not update last_wiki_successful_sync_at' do it 'does not update last wiki sync times' do
subject.execute subject.execute
registry.reload registry.reload
...@@ -159,19 +191,24 @@ describe Geo::RepositorySyncService, services: true do ...@@ -159,19 +191,24 @@ describe Geo::RepositorySyncService, services: true do
end end
context 'tracking database' do context 'tracking database' do
it 'sets last_repository_successful_sync_at' do before do
subject.execute subject.execute
registry.reload registry.reload
end
expect(registry.last_repository_successful_sync_at).not_to be_nil it 'updates last_repository_synced_at' do
expect(registry.last_repository_synced_at).to be_within(1.minute).of(DateTime.now)
end end
it 'sets last_wiki_successful_sync_at' do it 'sets last_repository_successful_sync_at' do
subject.execute expect(registry.last_repository_successful_sync_at).not_to be_nil
end
registry.reload it 'updates last_wiki_synced_at' do
expect(registry.last_wiki_synced_at).to be_within(1.minute).of(DateTime.now)
end
it 'sets last_wiki_successful_sync_at' do
expect(registry.last_wiki_successful_sync_at).not_to be_nil expect(registry.last_wiki_successful_sync_at).not_to be_nil
end end
end end
...@@ -211,29 +248,22 @@ describe Geo::RepositorySyncService, services: true do ...@@ -211,29 +248,22 @@ describe Geo::RepositorySyncService, services: true do
end end
context 'tracking database' do context 'tracking database' do
it 'updates last_repository_successful_sync_at' do before do
subject.execute subject.execute
registry.reload registry.reload
end
it 'updates last repository sync times' do
expect(registry.last_repository_synced_at).to be_within(1.minute).of(DateTime.now) expect(registry.last_repository_synced_at).to be_within(1.minute).of(DateTime.now)
expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(DateTime.now) expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(DateTime.now)
end end
it 'does not update last_wiki_successful_sync_at' do it 'does not update last wiki sync times' do
subject.execute
registry.reload
expect(registry.last_wiki_synced_at).to be_within(1.minute).of(last_wiki_synced_at) expect(registry.last_wiki_synced_at).to be_within(1.minute).of(last_wiki_synced_at)
expect(registry.last_wiki_successful_sync_at).to be_within(1.minute).of(last_wiki_synced_at) expect(registry.last_wiki_successful_sync_at).to be_within(1.minute).of(last_wiki_synced_at)
end end
it 'resets resync_repository' do it 'resets resync_repository' do
subject.execute
registry.reload
expect(registry.resync_repository).to be false expect(registry.resync_repository).to be false
end end
end end
...@@ -271,29 +301,22 @@ describe Geo::RepositorySyncService, services: true do ...@@ -271,29 +301,22 @@ describe Geo::RepositorySyncService, services: true do
end end
context 'tracking database' do context 'tracking database' do
it 'updates last_wiki_successful_sync_at' do before do
subject.execute subject.execute
registry.reload registry.reload
end
it 'updates last wiki sync times' do
expect(registry.last_wiki_synced_at).to be_within(1.minute).of(DateTime.now) expect(registry.last_wiki_synced_at).to be_within(1.minute).of(DateTime.now)
expect(registry.last_wiki_successful_sync_at).to be_within(1.minute).of(DateTime.now) expect(registry.last_wiki_successful_sync_at).to be_within(1.minute).of(DateTime.now)
end end
it 'does not update last_repository_successful_sync_at' do it 'does not update last repository sync times' do
subject.execute
registry.reload
expect(registry.last_repository_synced_at).to be_within(1.minute).of(last_repository_synced_at) expect(registry.last_repository_synced_at).to be_within(1.minute).of(last_repository_synced_at)
expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(last_repository_synced_at) expect(registry.last_repository_successful_sync_at).to be_within(1.minute).of(last_repository_synced_at)
end end
it 'resets resync_wiki' do it 'resets resync_wiki' do
subject.execute
registry.reload
expect(registry.resync_wiki).to be false expect(registry.resync_wiki).to be false
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