summaryrefslogtreecommitdiff
path: root/app/workers
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2018-07-06 14:38:24 +0900
committerShinya Maeda <shinya@gitlab.com>2018-07-06 14:38:24 +0900
commit25bd5413200c9cd9368c753e2752f07735604547 (patch)
treec4cc75a53face6a6d0da8adca9a3994d3cc688b5 /app/workers
parentb025e89e08888f3b393108f984a25c209526491b (diff)
parent969b7c565c6fe5cdfc54830d1da35f254ddaf530 (diff)
downloadgitlab-ce-25bd5413200c9cd9368c753e2752f07735604547.tar.gz
Merge branch 'master' into build-chunks-on-object-storage
Diffstat (limited to 'app/workers')
-rw-r--r--app/workers/project_cache_worker.rb33
1 files changed, 16 insertions, 17 deletions
diff --git a/app/workers/project_cache_worker.rb b/app/workers/project_cache_worker.rb
index abe86066fb4..b0e1d8837d9 100644
--- a/app/workers/project_cache_worker.rb
+++ b/app/workers/project_cache_worker.rb
@@ -3,7 +3,6 @@
# Worker for updating any project specific caches.
class ProjectCacheWorker
include ApplicationWorker
- include ExclusiveLeaseGuard
LEASE_TIMEOUT = 15.minutes.to_i
@@ -14,30 +13,30 @@ class ProjectCacheWorker
# statistics - An Array containing columns from ProjectStatistics to
# refresh, if empty all columns will be refreshed
def perform(project_id, files = [], statistics = [])
- @project = Project.find_by(id: project_id)
- return unless @project&.repository&.exists?
+ project = Project.find_by(id: project_id)
- update_statistics(statistics)
+ return unless project && project.repository.exists?
- @project.repository.refresh_method_caches(files.map(&:to_sym))
+ update_statistics(project, statistics.map(&:to_sym))
- @project.cleanup
+ project.repository.refresh_method_caches(files.map(&:to_sym))
+
+ project.cleanup
end
- private
+ def update_statistics(project, statistics = [])
+ return unless try_obtain_lease_for(project.id, :update_statistics)
- def update_statistics(statistics = [])
- try_obtain_lease do
- Rails.logger.info("Updating statistics for project #{@project.id}")
- @project.statistics.refresh!(only: statistics.to_a.map(&:to_sym))
- end
- end
+ Rails.logger.info("Updating statistics for project #{project.id}")
- def lease_timeout
- LEASE_TIMEOUT
+ project.statistics.refresh!(only: statistics)
end
- def lease_key
- "project_cache_worker:#{@project.id}:update_statistics"
+ private
+
+ def try_obtain_lease_for(project_id, section)
+ Gitlab::ExclusiveLease
+ .new("project_cache_worker:#{project_id}:#{section}", timeout: LEASE_TIMEOUT)
+ .try_obtain
end
end