2018-06-27 07:23:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-20 10:59:39 +00:00
|
|
|
# Worker for updating any project specific caches.
|
2020-12-01 12:09:17 +00:00
|
|
|
class ProjectCacheWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2019-10-30 15:14:17 +00:00
|
|
|
|
2021-07-21 12:09:35 +00:00
|
|
|
data_consistency :always
|
|
|
|
|
2021-04-30 18:10:09 +00:00
|
|
|
sidekiq_options retry: 3
|
|
|
|
|
2016-10-20 10:59:39 +00:00
|
|
|
LEASE_TIMEOUT = 15.minutes.to_i
|
|
|
|
|
2019-10-18 11:11:44 +00:00
|
|
|
feature_category :source_code_management
|
2020-06-12 12:08:56 +00:00
|
|
|
urgency :high
|
|
|
|
loggable_arguments 1, 2, 3
|
2020-12-01 12:09:17 +00:00
|
|
|
idempotent!
|
2019-10-18 11:11:44 +00:00
|
|
|
|
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
|
2019-08-16 19:53:56 +00:00
|
|
|
# refresh_statistics - A boolean that determines whether project statistics should
|
|
|
|
# be updated.
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-08-16 19:53:56 +00:00
|
|
|
def perform(project_id, files = [], statistics = [], refresh_statistics = true)
|
2018-07-04 11:04:58 +00:00
|
|
|
project = Project.find_by(id: project_id)
|
2016-10-25 14:01:24 +00:00
|
|
|
|
2019-02-13 22:38:11 +00:00
|
|
|
return unless project
|
2016-10-25 14:01:24 +00:00
|
|
|
|
2019-08-16 19:53:56 +00:00
|
|
|
update_statistics(project, statistics) if refresh_statistics
|
2016-10-20 10:59:39 +00:00
|
|
|
|
2019-02-13 22:38:11 +00:00
|
|
|
return unless project.repository.exists?
|
|
|
|
|
2018-07-04 11:04:58 +00:00
|
|
|
project.repository.refresh_method_caches(files.map(&:to_sym))
|
|
|
|
|
|
|
|
project.cleanup
|
2016-10-20 10:59:39 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-20 10:59:39 +00:00
|
|
|
|
2019-04-04 15:18:17 +00:00
|
|
|
# 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.
|
2018-07-04 11:04:58 +00:00
|
|
|
def update_statistics(project, statistics = [])
|
2019-03-22 12:10:38 +00:00
|
|
|
return if Gitlab::Database.read_only?
|
2019-04-01 06:11:08 +00:00
|
|
|
return unless try_obtain_lease_for(project.id, statistics)
|
2016-03-27 13:17:49 +00:00
|
|
|
|
2019-04-05 13:07:09 +00:00
|
|
|
Projects::UpdateStatisticsService.new(project, nil, statistics: statistics).execute
|
2019-04-04 15:18:17 +00:00
|
|
|
|
2019-04-01 06:11:08 +00:00
|
|
|
UpdateProjectStatisticsWorker.perform_in(LEASE_TIMEOUT, project.id, statistics)
|
2018-06-22 13:31:04 +00:00
|
|
|
end
|
2016-11-18 13:04:18 +00:00
|
|
|
|
2018-07-04 11:04:58 +00:00
|
|
|
private
|
|
|
|
|
2019-04-01 06:11:08 +00:00
|
|
|
def try_obtain_lease_for(project_id, statistics)
|
2018-07-04 11:04:58 +00:00
|
|
|
Gitlab::ExclusiveLease
|
2019-04-01 06:11:08 +00:00
|
|
|
.new(project_cache_worker_key(project_id, statistics), timeout: LEASE_TIMEOUT)
|
2018-07-04 11:04:58 +00:00
|
|
|
.try_obtain
|
2016-10-20 10:59:39 +00:00
|
|
|
end
|
2019-04-01 06:11:08 +00:00
|
|
|
|
|
|
|
def project_cache_worker_key(project_id, statistics)
|
2019-04-04 15:17:52 +00:00
|
|
|
["project_cache_worker", project_id, *statistics.sort].join(":")
|
2019-04-01 06:11:08 +00:00
|
|
|
end
|
2015-07-17 12:50:03 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
ProjectCacheWorker.prepend_mod_with('ProjectCacheWorker')
|