diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2018-07-04 14:04:58 +0300 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2018-07-04 14:04:58 +0300 |
commit | 14d2b52b00b49443146fbfc5c5b90de67fafa4e0 (patch) | |
tree | f9e6f7a1de90f131dffc4ed203c15c8a83c96e40 /app/workers | |
parent | 935969be250435b482863e42684fdd7cef7d37c0 (diff) |
Revert "Merge branch '44726-cancel_lease_upon_completion_in_project_cache_worker' into 'master'"
This reverts merge request !20103
Diffstat (limited to 'app/workers')
-rw-r--r-- | app/workers/project_cache_worker.rb | 33 |
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 |