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

45 lines
1.4 KiB
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
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
2018-03-17 18:26:18 +05:30
include ApplicationWorker
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
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
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))
2018-03-17 18:26:18 +05:30
project.cleanup
2016-11-03 12:29:30 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
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