Refactor Geo::RepositoryVerificationFinder#find_outdated_projects

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