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

64 lines
2.1 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.
2020-04-08 14:13:33 +05:30
class ProjectCacheWorker # rubocop:disable Scalability/IdempotentWorker
2018-03-17 18:26:18 +05:30
include ApplicationWorker
2019-12-26 22:10:19 +05:30
2020-04-08 14:13:33 +05:30
urgency :high
2019-12-26 22:10:19 +05:30
2016-11-03 12:29:30 +05:30
LEASE_TIMEOUT = 15.minutes.to_i
2019-12-21 20:55:43 +05:30
feature_category :source_code_management
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
2019-10-12 21:52:04 +05:30
# refresh_statistics - A boolean that determines whether project statistics should
# be updated.
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2019-10-12 21:52:04 +05:30
def perform(project_id, files = [], statistics = [], refresh_statistics = true)
2017-08-17 22:00:37 +05:30
project = Project.find_by(id: project_id)
2015-09-11 14:41:01 +05:30
2019-09-04 21:01:54 +05:30
return unless project
2016-11-03 12:29:30 +05:30
2019-10-12 21:52:04 +05:30
update_statistics(project, statistics) if refresh_statistics
2016-11-03 12:29:30 +05:30
2019-09-04 21:01:54 +05:30
return unless project.repository.exists?
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
2019-07-31 22:56:46 +05:30
# NOTE: triggering both an immediate update and one in 15 minutes if we
# successfully obtain the lease. That way, we only need to wait for the
# statistics to become accurate if they were already updated once in the
# last 15 minutes.
2017-08-17 22:00:37 +05:30
def update_statistics(project, statistics = [])
2019-07-07 11:18:12 +05:30
return if Gitlab::Database.read_only?
2019-07-31 22:56:46 +05:30
return unless try_obtain_lease_for(project.id, statistics)
2016-06-02 11:05:42 +05:30
2019-07-31 22:56:46 +05:30
Projects::UpdateStatisticsService.new(project, nil, statistics: statistics).execute
2016-06-02 11:05:42 +05:30
2019-07-31 22:56:46 +05:30
UpdateProjectStatisticsWorker.perform_in(LEASE_TIMEOUT, project.id, 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
2019-07-31 22:56:46 +05:30
def try_obtain_lease_for(project_id, statistics)
2017-09-10 17:25:29 +05:30
Gitlab::ExclusiveLease
2019-07-31 22:56:46 +05:30
.new(project_cache_worker_key(project_id, statistics), timeout: LEASE_TIMEOUT)
2017-09-10 17:25:29 +05:30
.try_obtain
2016-11-03 12:29:30 +05:30
end
2019-07-31 22:56:46 +05:30
def project_cache_worker_key(project_id, statistics)
["project_cache_worker", project_id, *statistics.sort].join(":")
end
2015-09-11 14:41:01 +05:30
end
2019-12-04 20:38:33 +05:30
ProjectCacheWorker.prepend_if_ee('EE::ProjectCacheWorker')