Welcome to mirror list, hosted at ThFree Co, Russian Federation.

project_cache_worker.rb « workers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: abe86066fb42cdd6f881cb383f8f54222bd7ba23 (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
# frozen_string_literal: true

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

  LEASE_TIMEOUT = 15.minutes.to_i

  # 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
  def perform(project_id, files = [], statistics = [])
    @project = Project.find_by(id: project_id)
    return unless @project&.repository&.exists?

    update_statistics(statistics)

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

    @project.cleanup
  end

  private

  def update_statistics(statistics = [])
    try_obtain_lease do
      Rails.logger.info("Updating statistics for project #{@project.id}")
      @project.statistics.refresh!(only: statistics.to_a.map(&:to_sym))
    end
  end

  def lease_timeout
    LEASE_TIMEOUT
  end

  def lease_key
    "project_cache_worker:#{@project.id}:update_statistics"
  end
end