summaryrefslogtreecommitdiff
path: root/app/workers/project_cache_worker.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/workers/project_cache_worker.rb')
-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