2016-10-20 10:59:39 +00:00
|
|
|
# Worker for updating any project specific caches.
|
2015-07-17 12:50:03 +00:00
|
|
|
class ProjectCacheWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2015-07-17 12:50:03 +00:00
|
|
|
|
2016-10-20 10:59:39 +00:00
|
|
|
LEASE_TIMEOUT = 15.minutes.to_i
|
|
|
|
|
2016-11-18 13:04:18 +00:00
|
|
|
# project_id - The ID of the project for which to flush the cache.
|
2016-11-22 16:58:10 +00:00
|
|
|
# files - An Array containing extra types of files to refresh such as
|
|
|
|
# `:readme` to flush the README and `:changelog` to flush the
|
|
|
|
# CHANGELOG.
|
|
|
|
# statistics - An Array containing columns from ProjectStatistics to
|
|
|
|
# refresh, if empty all columns will be refreshed
|
|
|
|
def perform(project_id, files = [], statistics = [])
|
2016-11-18 13:04:18 +00:00
|
|
|
project = Project.find_by(id: project_id)
|
2016-10-25 14:01:24 +00:00
|
|
|
|
2016-11-18 13:04:18 +00:00
|
|
|
return unless project && project.repository.exists?
|
2016-10-25 14:01:24 +00:00
|
|
|
|
2016-11-22 16:58:10 +00:00
|
|
|
update_statistics(project, statistics.map(&:to_sym))
|
2016-10-20 10:59:39 +00:00
|
|
|
|
2016-11-22 16:58:10 +00:00
|
|
|
project.repository.refresh_method_caches(files.map(&:to_sym))
|
2016-10-20 10:59:39 +00:00
|
|
|
end
|
|
|
|
|
2016-11-22 16:58:10 +00:00
|
|
|
def update_statistics(project, statistics = [])
|
|
|
|
return unless try_obtain_lease_for(project.id, :update_statistics)
|
2016-03-27 13:17:49 +00:00
|
|
|
|
2016-11-22 16:58:10 +00:00
|
|
|
Rails.logger.info("Updating statistics for project #{project.id}")
|
2016-03-27 13:17:49 +00:00
|
|
|
|
2016-11-22 16:58:10 +00:00
|
|
|
project.statistics.refresh!(only: statistics)
|
2015-07-17 12:50:03 +00:00
|
|
|
end
|
2016-10-20 10:59:39 +00:00
|
|
|
|
2016-11-18 13:04:18 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def try_obtain_lease_for(project_id, section)
|
2017-06-21 13:48:12 +00:00
|
|
|
Gitlab::ExclusiveLease
|
|
|
|
.new("project_cache_worker:#{project_id}:#{section}", timeout: LEASE_TIMEOUT)
|
|
|
|
.try_obtain
|
2016-10-20 10:59:39 +00:00
|
|
|
end
|
2015-07-17 12:50:03 +00:00
|
|
|
end
|