2016-10-20 06:59:39 -04:00
|
|
|
# Worker for updating any project specific caches.
|
2015-07-17 08:50:03 -04:00
|
|
|
class ProjectCacheWorker
|
|
|
|
include Sidekiq::Worker
|
2016-10-21 12:13:41 -04:00
|
|
|
include DedicatedSidekiqQueue
|
2015-07-17 08:50:03 -04:00
|
|
|
|
2016-10-20 06:59:39 -04:00
|
|
|
LEASE_TIMEOUT = 15.minutes.to_i
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
# project_id - The ID of the project for which to flush the cache.
|
|
|
|
# refresh - An Array containing extra types of data to refresh such as
|
|
|
|
# `:readme` to flush the README and `:changelog` to flush the
|
|
|
|
# CHANGELOG.
|
|
|
|
def perform(project_id, refresh = [])
|
|
|
|
project = Project.find_by(id: project_id)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
return unless project && project.repository.exists?
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
update_repository_size(project)
|
|
|
|
project.update_commit_count
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
project.repository.refresh_method_caches(refresh.map(&:to_sym))
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
def update_repository_size(project)
|
|
|
|
return unless try_obtain_lease_for(project.id, :update_repository_size)
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
Rails.logger.info("Updating repository size for project #{project.id}")
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2015-07-17 09:22:56 -04:00
|
|
|
project.update_repository_size
|
2015-07-17 08:50:03 -04:00
|
|
|
end
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def try_obtain_lease_for(project_id, section)
|
|
|
|
Gitlab::ExclusiveLease.
|
|
|
|
new("project_cache_worker:#{project_id}:#{section}", timeout: LEASE_TIMEOUT).
|
|
|
|
try_obtain
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
2015-07-17 08:50:03 -04:00
|
|
|
end
|