Refactor Geo::RepositoryVerificationFinder#find_outdated_projects

parent 9c654efc
module Geo module Geo
class RepositoryVerificationFinder class RepositoryVerificationFinder
attr_reader :shard_name
def initialize(shard_name: nil) def initialize(shard_name: nil)
@shard_name = shard_name @shard_name = shard_name
end end
def find_outdated_projects(batch_size:) def find_outdated_projects(batch_size:)
cte_definition = query = build_query_to_find_outdated_projects(batch_size: batch_size)
projects_table cte = Gitlab::SQL::CTE.new(:outdated_projects, query)
.join(repository_state_table).on(project_id_matcher)
.project(projects_table[:id], projects_table[:last_repository_updated_at])
.where(repository_outdated.or(wiki_outdated))
.take(batch_size)
if shard_name.present?
cte_definition = shard_restriction(cte_definition)
end
cte_table = Arel::Table.new(:outdated_projects) Project.with(cte.to_arel)
composed_cte = Arel::Nodes::As.new(cte_table, cte_definition) .from(cte.alias_to(projects_table))
alias_to = Arel::Nodes::As.new(cte_table, projects_table)
Project.with(composed_cte)
.from(alias_to)
.order(last_repository_updated_at_asc) .order(last_repository_updated_at_asc)
end end
...@@ -35,10 +21,7 @@ module Geo ...@@ -35,10 +21,7 @@ module Geo
.where(repository_never_verified) .where(repository_never_verified)
.limit(batch_size) .limit(batch_size)
if shard_name.present? relation = apply_shard_restriction(relation) if shard_name.present?
relation = shard_restriction(relation)
end
relation relation
end end
...@@ -58,7 +41,21 @@ module Geo ...@@ -58,7 +41,21 @@ module Geo
Project.verification_failed_wikis.count Project.verification_failed_wikis.count
end end
protected private
attr_reader :shard_name
def build_query_to_find_outdated_projects(batch_size:)
query =
projects_table
.join(repository_state_table).on(project_id_matcher)
.project(projects_table[:id], projects_table[:last_repository_updated_at])
.where(repository_outdated.or(wiki_outdated))
.take(batch_size)
query = apply_shard_restriction(query) if shard_name.present?
query
end
def projects_table def projects_table
Project.arel_table Project.arel_table
...@@ -97,7 +94,7 @@ module Geo ...@@ -97,7 +94,7 @@ module Geo
Gitlab::Database.nulls_last_order('projects.last_repository_updated_at', 'ASC') Gitlab::Database.nulls_last_order('projects.last_repository_updated_at', 'ASC')
end end
def shard_restriction(relation) def apply_shard_restriction(relation)
relation.where(projects_table[:repository_storage].eq(shard_name)) relation.where(projects_table[:repository_storage].eq(shard_name))
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