summaryrefslogtreecommitdiff
path: root/app/workers/project_cache_worker.rb
blob: 27d7e6527210cc503e4ce68a42b15ea4bb3ed91b (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
# Worker for updating any project specific caches.
class ProjectCacheWorker
  include Sidekiq::Worker
  include DedicatedSidekiqQueue

  LEASE_TIMEOUT = 15.minutes.to_i

  # project_id - The ID of the project for which to flush the cache.
  # refresh - An Array containing extra types of data to refresh such as
  #           `:readme` to flush the README and `:changelog` to flush the
  #           CHANGELOG.
  def perform(project_id, refresh = [])
    project = Project.find_by(id: project_id)

    return unless project && project.repository.exists?

    update_repository_size(project)
    project.update_commit_count

    project.repository.refresh_method_caches(refresh.map(&:to_sym))
  end

  def update_repository_size(project)
    return unless try_obtain_lease_for(project.id, :update_repository_size)

    Rails.logger.info("Updating repository size for project #{project.id}")

    project.update_repository_size
  end

  private

  def try_obtain_lease_for(project_id, section)
    Gitlab::ExclusiveLease.
      new("project_cache_worker:#{project_id}:#{section}", timeout: LEASE_TIMEOUT).
      try_obtain
  end
end