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

housekeeping_service.rb 1.21 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!
Yorick Peterse's avatar
Yorick Peterse committed
33 34
      Gitlab::Metrics.measure(:increment_pushes_since_gc) do
        @project.increment_pushes_since_gc
35
      end
36 37 38 39
    end

    private

40 41 42 43
    def execute_gitlab_shell_gc
      GitGarbageCollectWorker.perform_async(@project.id)
    ensure
      Gitlab::Metrics.measure(:reset_pushes_since_gc) do
Yorick Peterse's avatar
Yorick Peterse committed
44
        @project.reset_pushes_since_gc
45 46 47
      end
    end

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