GitLab wurde erfolgreich aktualisiert. Durch regelmäßige Updates bleibt das THM GitLab sicher. Danke für Ihre Geduld.

housekeeping_service.rb 2.23 KB
Newer Older
1 2 3 4 5
# Projects::HousekeepingService class
#
# Used for git housekeeping
#
# Ex.
6
#   Projects::HousekeepingService.new(project).execute
7 8 9
#
module Projects
  class HousekeepingService < BaseService
Jacob Vosmaer's avatar
Jacob Vosmaer committed
10 11
    include Gitlab::CurrentSettings

12 13
    LEASE_TIMEOUT = 3600

14 15 16 17 18 19
    class LeaseTaken < StandardError
      def to_s
        "Somebody already triggered housekeeping for this project in the past #{LEASE_TIMEOUT / 60} minutes"
      end
    end

20 21 22 23 24
    def initialize(project)
      @project = project
    end

    def execute
Jacob Vosmaer's avatar
Jacob Vosmaer committed
25 26
      lease_uuid = try_obtain_lease
      raise LeaseTaken unless lease_uuid.present?
Jacob Vosmaer's avatar
Jacob Vosmaer committed
27

Jacob Vosmaer's avatar
Jacob Vosmaer committed
28
      execute_gitlab_shell_gc(lease_uuid)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
29 30 31
    end

    def needed?
Jacob Vosmaer's avatar
Jacob Vosmaer committed
32
      pushes_since_gc > 0 && period_match? && housekeeping_enabled?
Jacob Vosmaer's avatar
Jacob Vosmaer committed
33 34 35
    end

    def increment!
Yorick Peterse's avatar
Yorick Peterse committed
36 37
      Gitlab::Metrics.measure(:increment_pushes_since_gc) do
        @project.increment_pushes_since_gc
38
      end
39 40 41 42
    end

    private

Jacob Vosmaer's avatar
Jacob Vosmaer committed
43 44
    def execute_gitlab_shell_gc(lease_uuid)
      GitGarbageCollectWorker.perform_async(@project.id, task, lease_key, lease_uuid)
45
    ensure
Jacob Vosmaer's avatar
Jacob Vosmaer committed
46 47 48 49
      if pushes_since_gc >= gc_period
        Gitlab::Metrics.measure(:reset_pushes_since_gc) do
          @project.reset_pushes_since_gc
        end
50 51 52
      end
    end

53
    def try_obtain_lease
54
      Gitlab::Metrics.measure(:obtain_housekeeping_lease) do
Jacob Vosmaer's avatar
Jacob Vosmaer committed
55
        lease = ::Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT)
56 57
        lease.try_obtain
      end
58
    end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

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

    def pushes_since_gc
      @project.pushes_since_gc
    end

    def task
      if pushes_since_gc % gc_period == 0
        :gc
      elsif pushes_since_gc % full_repack_period == 0
        :full_repack
      else
        :incremental_repack
      end
    end

    def period_match?
      [gc_period, full_repack_period, repack_period].any? { |period| pushes_since_gc % period == 0 }
    end

    def housekeeping_enabled?
      current_application_settings.housekeeping_enabled
    end

    def gc_period
      current_application_settings.housekeeping_gc_period
    end

    def full_repack_period
      current_application_settings.housekeeping_full_repack_period
    end

    def repack_period
      current_application_settings.housekeeping_incremental_repack_period
    end
97 98
  end
end