diff options
author | Hiroyuki Sato <sathiroyuki@gmail.com> | 2019-04-01 15:11:08 +0900 |
---|---|---|
committer | Hiroyuki Sato <sathiroyuki@gmail.com> | 2019-04-05 00:22:56 +0900 |
commit | 0adedbb4822a8daaa215b33c88ace136c31d042d (patch) | |
tree | 7adcd76e9bb9172c5c7d5200803957bb00ed7be5 /app | |
parent | c7f918aa691b1b9c6ee6a489ae28a094eacff72c (diff) | |
download | gitlab-ce-0adedbb4822a8daaa215b33c88ace136c31d042d.tar.gz |
Fix the bug that the project statistics is not updated
Diffstat (limited to 'app')
-rw-r--r-- | app/workers/all_queues.yml | 1 | ||||
-rw-r--r-- | app/workers/project_cache_worker.rb | 14 | ||||
-rw-r--r-- | app/workers/update_project_statistics_worker.rb | 22 |
3 files changed, 31 insertions, 6 deletions
diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index 3e8c2a1209a..f9b2e698fc9 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -143,6 +143,7 @@ - repository_remove_remote - system_hook_push - update_merge_requests +- update_project_statistics - upload_checksum - web_hook - repository_update_remote_mirror diff --git a/app/workers/project_cache_worker.rb b/app/workers/project_cache_worker.rb index b31099bc670..73eb461768c 100644 --- a/app/workers/project_cache_worker.rb +++ b/app/workers/project_cache_worker.rb @@ -28,18 +28,20 @@ class ProjectCacheWorker def update_statistics(project, statistics = []) return if Gitlab::Database.read_only? - return unless try_obtain_lease_for(project.id, :update_statistics) + return unless try_obtain_lease_for(project.id, statistics) - Rails.logger.info("Updating statistics for project #{project.id}") - - project.statistics.refresh!(only: statistics) + UpdateProjectStatisticsWorker.perform_in(LEASE_TIMEOUT, project.id, statistics) end private - def try_obtain_lease_for(project_id, section) + def try_obtain_lease_for(project_id, statistics) Gitlab::ExclusiveLease - .new("project_cache_worker:#{project_id}:#{section}", timeout: LEASE_TIMEOUT) + .new(project_cache_worker_key(project_id, statistics), timeout: LEASE_TIMEOUT) .try_obtain end + + def project_cache_worker_key(project_id, statistics) + (["project_cache_worker", project_id] + statistics.sort).join(":") + end end diff --git a/app/workers/update_project_statistics_worker.rb b/app/workers/update_project_statistics_worker.rb new file mode 100644 index 00000000000..46673c4b07e --- /dev/null +++ b/app/workers/update_project_statistics_worker.rb @@ -0,0 +1,22 @@ + +# frozen_string_literal: true + +# Worker for updating project statistics. +class UpdateProjectStatisticsWorker + include ApplicationWorker + + # project_id - The ID of the project for which to flush the cache. + # statistics - An Array containing columns from ProjectStatistics to + # refresh, if empty all columns will be refreshed + # rubocop: disable CodeReuse/ActiveRecord + def perform(project_id, statistics = []) + project = Project.find_by(id: project_id) + + return unless project && project.repository.exists? + + Rails.logger.info("Updating statistics for project #{project.id}") + + project.statistics.refresh!(only: statistics.map(&:to_sym)) + end + # rubocop: enable CodeReuse/ActiveRecord +end |