Extract retryable scope

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