Extract retryable scope

Refactor code to make the finder class
closer to a Geo replicator.
parent 2c191849
......@@ -5,8 +5,7 @@ module Geo
# rubocop:disable CodeReuse/ActiveRecord
def find_retryable_dirty_registries(batch_size:, except_ids: [])
registry_class
.failed
.retry_due
.retryable
.model_id_not_in(except_ids)
.order(Gitlab::Database.nulls_first_order(:last_synced_at))
.limit(batch_size)
......
......@@ -5,7 +5,7 @@ module Geo
# rubocop:disable CodeReuse/ActiveRecord
def find_retryable_dirty_registries(batch_size:, except_ids: [])
registry_class
.updated_recently
.retryable
.model_id_not_in(except_ids)
.order(Gitlab::Database.nulls_first_order(:last_synced_at))
.limit(batch_size)
......
......@@ -20,8 +20,7 @@ module Geo
# rubocop:disable CodeReuse/ActiveRecord
def find_retryable_failed_registries(batch_size:, except_ids: [])
registry_class
.failed
.retry_due
.retryable
.model_id_not_in(except_ids)
.limit(batch_size)
end
......
......@@ -30,8 +30,7 @@ module Geo
# rubocop:disable CodeReuse/ActiveRecord
def find_retryable_dirty_registries(batch_size:, except_ids: [])
Geo::ProjectRegistry
.dirty
.retry_due
.retryable
.model_id_not_in(except_ids)
.order(Gitlab::Database.nulls_first_order(:last_repository_synced_at))
.limit(batch_size)
......
......@@ -40,6 +40,7 @@ module Geo::ReplicableRegistry
scope :synced, -> { with_state(:synced) }
scope :pending, -> { with_state(:pending) }
scope :retry_due, -> { where(arel_table[:retry_at].eq(nil).or(arel_table[:retry_at].lt(Time.current))) }
scope :retryable, -> { failed.retry_due }
scope :ordered, -> { order(:id) }
state_machine :state, initial: :pending do
......
......@@ -4,9 +4,11 @@ module Geo::Syncable
extend ActiveSupport::Concern
included do
scope :failed, -> { where(success: false) }
scope :synced, -> { where(success: true) }
scope :retry_due, -> { where('retry_at is NULL OR retry_at < ?', Time.current) }
scope :failed, -> { where(success: false).where.not(retry_count: nil) }
scope :missing_on_primary, -> { where(missing_on_primary: true) }
scope :never_synced, -> { where(success: false, retry_count: nil) }
scope :retry_due, -> { where('retry_at is NULL OR retry_at < ?', Time.current) }
scope :retryable, -> { failed.retry_due }
scope :synced, -> { where(success: true) }
end
end
......@@ -52,8 +52,7 @@ class Geo::BaseRegistry < Geo::TrackingBase
end
def self.find_failed_registries(batch_size:, except_ids: [])
failed
.retry_due
retryable
.model_id_not_in(except_ids)
.limit(batch_size)
end
......
......@@ -12,6 +12,7 @@ class Geo::ContainerRepositoryRegistry < Geo::BaseRegistry
scope :failed, -> { with_state(:failed) }
scope :synced, -> { with_state(:synced) }
scope :retry_due, -> { where(arel_table[:retry_at].eq(nil).or(arel_table[:retry_at].lt(Time.current))) }
scope :retryable, -> { failed.retry_due }
state_machine :state, initial: :pending do
state :started
......
......@@ -15,6 +15,7 @@ class Geo::DesignRegistry < Geo::BaseRegistry
scope :failed, -> { with_state(:failed) }
scope :synced, -> { with_state(:synced) }
scope :retry_due, -> { where(arel_table[:retry_at].eq(nil).or(arel_table[:retry_at].lt(Time.current))) }
scope :retryable, -> { pending.or(failed.retry_due) }
state_machine :state, initial: :pending do
state :started
......@@ -75,10 +76,6 @@ class Geo::DesignRegistry < Geo::BaseRegistry
designs_repositories
end
def self.updated_recently
pending.or(failed.retry_due)
end
def fail_sync!(message, error, attrs = {})
new_retry_count = retry_count + 1
......
......@@ -6,12 +6,6 @@ class Geo::JobArtifactRegistry < Geo::BaseRegistry
MODEL_CLASS = ::Ci::JobArtifact
MODEL_FOREIGN_KEY = :artifact_id
scope :never_synced, -> { where(success: false, retry_count: nil) }
def self.failed
where(success: false).where.not(retry_count: nil)
end
def self.finder_class
::Geo::JobArtifactRegistryFinder
end
......
......@@ -11,12 +11,6 @@ class Geo::LfsObjectRegistry < Geo::BaseRegistry
belongs_to :lfs_object, class_name: 'LfsObject'
scope :never_synced, -> { where(success: false, retry_count: nil) }
def self.failed
where(success: false).where.not(retry_count: nil)
end
def self.finder_class
::Geo::LfsObjectRegistryFinder
end
......
......@@ -21,8 +21,9 @@ class Geo::ProjectRegistry < Geo::BaseRegistry
validates :project, presence: true, uniqueness: true
scope :never_synced, -> { where(last_repository_synced_at: nil) }
scope :dirty, -> { where(arel_table[:resync_repository].eq(true).or(arel_table[:resync_wiki].eq(true))) }
scope :retryable, -> { dirty.retry_due }
scope :never_synced, -> { where(last_repository_synced_at: nil) }
scope :synced_repos, -> { where(resync_repository: false) }
scope :synced_wikis, -> { where(resync_wiki: false) }
scope :failed_repos, -> { where(arel_table[:repository_retry_count].gt(0)) }
......
......@@ -10,9 +10,7 @@ class Geo::UploadRegistry < Geo::BaseRegistry
belongs_to :upload, foreign_key: :file_id
scope :failed, -> { where(success: false).where.not(retry_count: nil) }
scope :fresh, -> { order(created_at: :desc) }
scope :never_synced, -> { where(success: false, retry_count: nil) }
def self.finder_class
::Geo::AttachmentRegistryFinder
......
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