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

40 lines
1.3 KiB
Ruby
Raw Normal View History

2016-11-03 12:29:30 +05:30
# Worker for updating any project specific caches.
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
2017-08-17 22:00:37 +05:30
# project_id - The ID of the project for which to flush the cache.
# 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 = [])
project = Project.find_by(id: project_id)
2015-09-11 14:41:01 +05:30
2017-08-17 22:00:37 +05:30
return unless project && project.repository.exists?
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
update_statistics(project, statistics.map(&:to_sym))
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
project.repository.refresh_method_caches(files.map(&:to_sym))
2016-11-03 12:29:30 +05:30
end
2017-08-17 22:00:37 +05:30
def update_statistics(project, statistics = [])
return unless try_obtain_lease_for(project.id, :update_statistics)
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
Rails.logger.info("Updating statistics for project #{project.id}")
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
project.statistics.refresh!(only: statistics)
2015-09-11 14:41:01 +05:30
end
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
private
def try_obtain_lease_for(project_id, section)
2017-09-10 17:25:29 +05:30
Gitlab::ExclusiveLease
.new("project_cache_worker:#{project_id}:#{section}", timeout: LEASE_TIMEOUT)
.try_obtain
2016-11-03 12:29:30 +05:30
end
2015-09-11 14:41:01 +05:30
end