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

housekeeping_service.rb 1.39 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
      GitGarbageCollectWorker.perform_async(@project.id)
26
    ensure
27
      Gitlab::Metrics.measure(:reset_pushes_since_gc) do
28
        update_pushes_since_gc(0)
29
      end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
30 31 32 33 34 35 36
    end

    def needed?
      @project.pushes_since_gc >= 10
    end

    def increment!
37
      Gitlab::Metrics.measure(:increment_pushes_since_gc) do
38
        update_pushes_since_gc(@project.pushes_since_gc + 1)
39
      end
40 41 42 43
    end

    private

44 45 46 47 48 49
    def update_pushes_since_gc(new_value)
      if Gitlab::ExclusiveLease.new("project_housekeeping:update_pushes_since_gc:#{project.id}", timeout: 60).try_obtain
        @project.update_column(:pushes_since_gc, new_value)
      end
    end

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