diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-10-20 13:59:39 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2016-10-20 14:20:47 +0300 |
commit | bc31a489dd8c329cf011ac898498735809c319dd (patch) | |
tree | acc8d0f7b93b1ab375fe31803466890d91e8fdd3 /app/workers/project_cache_worker.rb | |
parent | ba28a64ef93f44e45e6ec04a15f6170061f309e1 (diff) |
Restrict ProjectCacheWorker jobs to one per 15 min
This ensures ProjectCacheWorker jobs for a given project are performed
at most once per 15 minutes. This should reduce disk load a bit in cases
where there are multiple pushes happening (which should schedule
multiple ProjectCacheWorker jobs).
Diffstat (limited to 'app/workers/project_cache_worker.rb')
-rw-r--r-- | app/workers/project_cache_worker.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/app/workers/project_cache_worker.rb b/app/workers/project_cache_worker.rb index ccefd0f71a0..0d524e88dc3 100644 --- a/app/workers/project_cache_worker.rb +++ b/app/workers/project_cache_worker.rb @@ -1,9 +1,30 @@ +# Worker for updating any project specific caches. +# +# This worker runs at most once every 15 minutes per project. This is to ensure +# that multiple instances of jobs for this worker don't hammer the underlying +# storage engine as much. class ProjectCacheWorker include Sidekiq::Worker sidekiq_options queue: :default + LEASE_TIMEOUT = 15.minutes.to_i + def perform(project_id) + if try_obtain_lease_for(project_id) + Rails.logger. + info("Obtained ProjectCacheWorker lease for project #{project_id}") + else + Rails.logger. + info("Could not obtain ProjectCacheWorker lease for project #{project_id}") + + return + end + + update_caches(project_id) + end + + def update_caches(project_id) project = Project.find(project_id) return unless project.repository.exists? @@ -15,4 +36,10 @@ class ProjectCacheWorker project.repository.build_cache end end + + def try_obtain_lease_for(project_id) + Gitlab::ExclusiveLease. + new("project_cache_worker:#{project_id}", timeout: LEASE_TIMEOUT). + try_obtain + end end |