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

housekeeping_service.rb 1.45 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
10 11
    LEASE_TIMEOUT = 3600

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

18 19 20 21 22
    def initialize(project)
      @project = project
    end

    def execute
23
      raise LeaseTaken unless try_obtain_lease
Jacob Vosmaer's avatar
Jacob Vosmaer committed
24

25
      execute_gitlab_shell_gc
Jacob Vosmaer's avatar
Jacob Vosmaer committed
26 27 28 29 30 31 32
    end

    def needed?
      @project.pushes_since_gc >= 10
    end

    def increment!
33 34 35 36
      if Gitlab::ExclusiveLease.new("project_housekeeping:increment!:#{@project.id}", timeout: 60).try_obtain
        Gitlab::Metrics.measure(:increment_pushes_since_gc) do
          update_pushes_since_gc(@project.pushes_since_gc + 1)
        end
37
      end
38 39 40 41
    end

    private

42 43 44 45 46
    def execute_gitlab_shell_gc
      GitGarbageCollectWorker.perform_async(@project.id)
    ensure
      Gitlab::Metrics.measure(:reset_pushes_since_gc) do
        update_pushes_since_gc(0)
47 48 49
      end
    end

50 51 52 53
    def update_pushes_since_gc(new_value)
      @project.update_column(:pushes_since_gc, new_value)
    end

54
    def try_obtain_lease
55 56 57 58
      Gitlab::Metrics.measure(:obtain_housekeeping_lease) do
        lease = ::Gitlab::ExclusiveLease.new("project_housekeeping:#{@project.id}", timeout: LEASE_TIMEOUT)
        lease.try_obtain
      end
59 60 61
    end
  end
end