dfdfa913ba
- Add two new ActiveRecord models: - RootNamespaceStoragestatistics will persist root namespace statistics - NamespaceAggregationSchedule will save information when a new update to the namespace statistics needs to be scheduled - Inject into UpdateProjectStatistics concern a new callback that will call an async job to insert a new row onto NamespaceAggregationSchedule table - When a new row is inserted a new job is scheduled. This job will update call an specific service to update the statistics and after that it will delete thee aggregated scheduled row - The RefresherServices makes heavy use of arel to build composable queries to update Namespace::RootStorageStatistics attributes. - Add an extra worker to traverse pending rows on NAmespace::AggregationSchedule table and schedule a worker for each one of this rows. - Add an extra worker to traverse pending rows on NAmespace::AggregationSchedule table and schedule a worker for each one of this rows
45 lines
1.5 KiB
Ruby
45 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Namespaces
|
|
class ScheduleAggregationWorker
|
|
include ApplicationWorker
|
|
|
|
queue_namespace :update_namespace_statistics
|
|
|
|
def perform(namespace_id)
|
|
return unless aggregation_schedules_table_exists?
|
|
|
|
namespace = Namespace.find(namespace_id)
|
|
root_ancestor = namespace.root_ancestor
|
|
|
|
return unless update_statistics_enabled_for?(root_ancestor) && !root_ancestor.aggregation_scheduled?
|
|
|
|
Namespace::AggregationSchedule.safe_find_or_create_by!(namespace_id: root_ancestor.id)
|
|
rescue ActiveRecord::RecordNotFound
|
|
log_error(namespace_id)
|
|
end
|
|
|
|
private
|
|
|
|
# On db/post_migrate/20180529152628_schedule_to_archive_legacy_traces.rb
|
|
# traces are archived through build.trace.archive, which in consequence
|
|
# calls UpdateProjectStatistics#schedule_namespace_statistics_worker.
|
|
#
|
|
# The migration and specs fails since NamespaceAggregationSchedule table
|
|
# does not exist at that point.
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/issues/50712
|
|
def aggregation_schedules_table_exists?
|
|
return true unless Rails.env.test?
|
|
|
|
Namespace::AggregationSchedule.table_exists?
|
|
end
|
|
|
|
def log_error(root_ancestor_id)
|
|
Gitlab::SidekiqLogger.error("Namespace can't be scheduled for aggregation: #{root_ancestor_id} does not exist")
|
|
end
|
|
|
|
def update_statistics_enabled_for?(root_ancestor)
|
|
Feature.enabled?(:update_statistics_namespace, root_ancestor)
|
|
end
|
|
end
|
|
end
|