Doesn't sync if the project was synced after our scheduling timestamp

parent 77b2f72c
...@@ -10,4 +10,12 @@ class Geo::ProjectRegistry < Geo::BaseRegistry ...@@ -10,4 +10,12 @@ class Geo::ProjectRegistry < Geo::BaseRegistry
where.not(last_repository_synced_at: nil, last_repository_successful_sync_at: nil) where.not(last_repository_synced_at: nil, last_repository_successful_sync_at: nil)
.where(resync_repository: false, resync_wiki: false) .where(resync_repository: false, resync_wiki: false)
end end
def repository_synced_since?(timestamp)
last_repository_synced_at && last_repository_synced_at > timestamp
end
def wiki_synced_since?(timestamp)
last_wiki_synced_at && last_wiki_synced_at > timestamp
end
end end
...@@ -14,24 +14,22 @@ module Geo ...@@ -14,24 +14,22 @@ module Geo
project = Project.find(project_id) project = Project.find(project_id)
registry = Geo::ProjectRegistry.find_or_initialize_by(project_id: project_id) registry = Geo::ProjectRegistry.find_or_initialize_by(project_id: project_id)
Geo::RepositorySyncService.new(project).execute if sync_repository?(registry) Geo::RepositorySyncService.new(project).execute if sync_repository?(registry, scheduled_time)
Geo::WikiSyncService.new(project).execute if sync_wiki?(registry) Geo::WikiSyncService.new(project).execute if sync_wiki?(registry, scheduled_time)
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")
end end
private private
def sync_repository?(registry) def sync_repository?(registry, scheduled_time)
registry.resync_repository? || !registry.repository_synced_since?(scheduled_time) &&
registry.last_repository_successful_sync_at.nil? || (registry.resync_repository? || registry.last_repository_successful_sync_at.nil?)
registry.last_repository_synced_at.nil?
end end
def sync_wiki?(registry) def sync_wiki?(registry, scheduled_time)
registry.resync_wiki? || !registry.wiki_synced_since?(scheduled_time) &&
registry.last_wiki_successful_sync_at.nil? || (registry.resync_wiki? || registry.last_wiki_successful_sync_at.nil?)
registry.last_wiki_synced_at.nil?
end end
end end
end end
...@@ -31,4 +31,44 @@ describe Geo::ProjectRegistry, models: true do ...@@ -31,4 +31,44 @@ describe Geo::ProjectRegistry, models: true do
expect(described_class.synced).to match_array([registry]) expect(described_class.synced).to match_array([registry])
end end
end end
describe '#repository_synced_since?' do
it 'returns false when last_repository_synced_at is nil' do
subject.last_repository_synced_at = nil
expect(subject.repository_synced_since?(Time.now)).to be_nil
end
it 'returns false when last_repository_synced_at before timestamp' do
subject.last_repository_synced_at = Time.now - 2.hours
expect(subject.repository_synced_since?(Time.now)).to be false
end
it 'returns true when last_repository_synced_at after timestamp' do
subject.last_repository_synced_at = Time.now + 2.hours
expect(subject.repository_synced_since?(Time.now)).to be true
end
end
describe '#wiki_synced_since?' do
it 'returns false when last_wiki_synced_at is nil' do
subject.last_wiki_synced_at = nil
expect(subject.wiki_synced_since?(Time.now)).to be_nil
end
it 'returns false when last_wiki_synced_at before timestamp' do
subject.last_wiki_synced_at = Time.now - 2.hours
expect(subject.wiki_synced_since?(Time.now)).to be false
end
it 'returns true when last_wiki_synced_at after timestamp' do
subject.last_wiki_synced_at = Time.now + 2.hours
expect(subject.wiki_synced_since?(Time.now)).to be true
end
end
end end
...@@ -103,5 +103,25 @@ RSpec.describe Geo::ProjectSyncWorker do ...@@ -103,5 +103,25 @@ RSpec.describe Geo::ProjectSyncWorker do
expect(wiki_sync_service).to have_received(:execute) expect(wiki_sync_service).to have_received(:execute)
end end
end end
context 'when project repository was synced after the time the job was scheduled in' do
it 'does not perform Geo::RepositorySyncService for the given project' do
create(:geo_project_registry, :synced, :repository_dirty, project: project, last_repository_synced_at: Time.now)
subject.perform(project.id, Time.now - 5.minutes)
expect(repository_sync_service).not_to have_received(:execute)
end
end
context 'when wiki repository was synced after the time the job was scheduled in' do
it 'does not perform Geo::RepositorySyncService for the given project' do
create(:geo_project_registry, :synced, :wiki_dirty, project: project, last_wiki_synced_at: Time.now)
subject.perform(project.id, Time.now - 5.minutes)
expect(wiki_sync_service).not_to have_received(:execute)
end
end
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