BigW Consortium Gitlab

project_cache_worker.rb 1.25 KB
Newer Older
1
# Worker for updating any project specific caches.
2 3
class ProjectCacheWorker
  include Sidekiq::Worker
4
  include DedicatedSidekiqQueue
5

6 7
  LEASE_TIMEOUT = 15.minutes.to_i

8
  # project_id - The ID of the project for which to flush the cache.
9 10 11 12 13 14
  # 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 = [])
15
    project = Project.find_by(id: project_id)
16

17
    return unless project && project.repository.exists?
18

19
    update_statistics(project, statistics.map(&:to_sym))
20

21
    project.repository.refresh_method_caches(files.map(&:to_sym))
22 23
  end

24 25
  def update_statistics(project, statistics = [])
    return unless try_obtain_lease_for(project.id, :update_statistics)
26

27
    Rails.logger.info("Updating statistics for project #{project.id}")
28

29
    project.statistics.refresh!(only: statistics)
30
  end
31

32 33 34
  private

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