debian-mirror-gitlab/app/workers/project_cache_worker.rb

55 lines
1.4 KiB
Ruby
Raw Normal View History

2016-11-03 12:29:30 +05: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.
2015-09-11 14:41:01 +05:30
class ProjectCacheWorker
include Sidekiq::Worker
2016-11-03 12:29:30 +05:30
include DedicatedSidekiqQueue
2015-09-11 14:41:01 +05:30
2016-11-03 12:29:30 +05:30
LEASE_TIMEOUT = 15.minutes.to_i
def self.lease_for(project_id)
Gitlab::ExclusiveLease.
new("project_cache_worker:#{project_id}", timeout: LEASE_TIMEOUT)
end
# Overwrite Sidekiq's implementation so we only schedule when actually needed.
def self.perform_async(project_id)
# If a lease for this project is still being held there's no point in
# scheduling a new job.
super unless lease_for(project_id).exists?
end
2015-09-11 14:41:01 +05:30
def perform(project_id)
2016-11-03 12:29:30 +05:30
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)
2015-09-11 14:41:01 +05:30
project = Project.find(project_id)
2016-06-02 11:05:42 +05:30
return unless project.repository.exists?
2015-09-11 14:41:01 +05:30
project.update_repository_size
project.update_commit_count
if project.repository.root_ref
project.repository.build_cache
end
end
2016-11-03 12:29:30 +05:30
def try_obtain_lease_for(project_id)
self.class.lease_for(project_id).try_obtain
end
2015-09-11 14:41:01 +05:30
end