summaryrefslogtreecommitdiff
path: root/app/workers/project_cache_worker.rb
blob: cb1a7c8560ad6b8c904d5995bd8f738e98fe556b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

# Worker for updating any project specific caches.
class ProjectCacheWorker
  include ApplicationWorker

  data_consistency :always

  sidekiq_options retry: 3

  LEASE_TIMEOUT = 15.minutes.to_i

  feature_category :source_code_management
  urgency :high
  loggable_arguments 1, 2, 3
  idempotent!

  # 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
  # refresh_statistics - A boolean that determines whether project statistics should
  #                     be updated.
  def perform(project_id, files = [], statistics = [], refresh_statistics = true)
    project = Project.find_by_id(project_id)

    return unless project

    update_statistics(project, statistics) if refresh_statistics

    return unless project.repository.exists?

    project.repository.refresh_method_caches(files.map(&:to_sym))

    project.cleanup
  end

  # 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.
  def update_statistics(project, statistics = [])
    return if Gitlab::Database.read_only?
    return unless try_obtain_lease_for(project.id, statistics)

    Projects::UpdateStatisticsService.new(project, nil, statistics: statistics).execute

    lease_key = project_cache_worker_key(project.id, statistics)
    UpdateProjectStatisticsWorker.perform_in(LEASE_TIMEOUT, lease_key, project.id, statistics)
  end

  private

  def try_obtain_lease_for(project_id, statistics)
    Gitlab::ExclusiveLease
      .new(project_cache_worker_key(project_id, statistics), timeout: LEASE_TIMEOUT)
      .try_obtain
  end

  def project_cache_worker_key(project_id, statistics)
    ["project_cache_worker", project_id, *statistics.sort].join(":")
  end
end

ProjectCacheWorker.prepend_mod_with('ProjectCacheWorker')