2016-10-20 10:59:39 +00:00
|
|
|
# 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-07-17 12:50:03 +00:00
|
|
|
class ProjectCacheWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
sidekiq_options queue: :default
|
|
|
|
|
2016-10-20 10:59:39 +00:00
|
|
|
LEASE_TIMEOUT = 15.minutes.to_i
|
|
|
|
|
2015-07-17 12:50:03 +00:00
|
|
|
def perform(project_id)
|
2016-10-20 10:59:39 +00:00
|
|
|
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-07-17 13:22:56 +00:00
|
|
|
project = Project.find(project_id)
|
2016-03-27 13:17:49 +00:00
|
|
|
|
|
|
|
return unless project.repository.exists?
|
|
|
|
|
2015-07-17 13:22:56 +00:00
|
|
|
project.update_repository_size
|
|
|
|
project.update_commit_count
|
2015-07-17 14:03:38 +00:00
|
|
|
|
|
|
|
if project.repository.root_ref
|
|
|
|
project.repository.build_cache
|
|
|
|
end
|
2015-07-17 12:50:03 +00:00
|
|
|
end
|
2016-10-20 10:59:39 +00:00
|
|
|
|
|
|
|
def try_obtain_lease_for(project_id)
|
|
|
|
Gitlab::ExclusiveLease.
|
|
|
|
new("project_cache_worker:#{project_id}", timeout: LEASE_TIMEOUT).
|
|
|
|
try_obtain
|
|
|
|
end
|
2015-07-17 12:50:03 +00:00
|
|
|
end
|