2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-20 06:59:39 -04:00
|
|
|
# Worker for updating any project specific caches.
|
2015-07-17 08:50:03 -04:00
|
|
|
class ProjectCacheWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2015-07-17 08:50:03 -04:00
|
|
|
|
2016-10-20 06:59:39 -04:00
|
|
|
LEASE_TIMEOUT = 15.minutes.to_i
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
# project_id - The ID of the project for which to flush the cache.
|
2016-11-22 11:58:10 -05: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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-22 11:58:10 -05:00
|
|
|
def perform(project_id, files = [], statistics = [])
|
2018-07-04 07:04:58 -04:00
|
|
|
project = Project.find_by(id: project_id)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
return unless project && project.repository.exists?
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2019-04-05 09:07:09 -04:00
|
|
|
update_statistics(project, statistics)
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
project.repository.refresh_method_caches(files.map(&:to_sym))
|
|
|
|
|
|
|
|
project.cleanup
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2019-04-04 11:18:17 -04: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 07:04:58 -04:00
|
|
|
def update_statistics(project, statistics = [])
|
2019-03-22 08:10:38 -04:00
|
|
|
return if Gitlab::Database.read_only?
|
2019-04-01 02:11:08 -04:00
|
|
|
return unless try_obtain_lease_for(project.id, statistics)
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2019-04-05 09:07:09 -04:00
|
|
|
Projects::UpdateStatisticsService.new(project, nil, statistics: statistics).execute
|
2019-04-04 11:18:17 -04:00
|
|
|
|
2019-04-01 02:11:08 -04:00
|
|
|
UpdateProjectStatisticsWorker.perform_in(LEASE_TIMEOUT, project.id, statistics)
|
2018-06-22 09:31:04 -04:00
|
|
|
end
|
2016-11-18 08:04:18 -05:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
private
|
|
|
|
|
2019-04-01 02:11:08 -04:00
|
|
|
def try_obtain_lease_for(project_id, statistics)
|
2018-07-04 07:04:58 -04:00
|
|
|
Gitlab::ExclusiveLease
|
2019-04-01 02:11:08 -04:00
|
|
|
.new(project_cache_worker_key(project_id, statistics), timeout: LEASE_TIMEOUT)
|
2018-07-04 07:04:58 -04:00
|
|
|
.try_obtain
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
2019-04-01 02:11:08 -04:00
|
|
|
|
|
|
|
def project_cache_worker_key(project_id, statistics)
|
2019-04-04 11:17:52 -04:00
|
|
|
["project_cache_worker", project_id, *statistics.sort].join(":")
|
2019-04-01 02:11:08 -04:00
|
|
|
end
|
2015-07-17 08:50:03 -04:00
|
|
|
end
|