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

housekeeping_service.rb 974 Bytes
Newer Older
1 2 3 4 5
# Projects::HousekeepingService class
#
# Used for git housekeeping
#
# Ex.
6
#   Projects::HousekeepingService.new(project).execute
7 8 9 10 11
#
module Projects
  class HousekeepingService < BaseService
    include Gitlab::ShellAdapter

12 13
    LEASE_TIMEOUT = 3600

14 15 16 17 18
    def initialize(project)
      @project = project
    end

    def execute
19 20 21
      if !try_obtain_lease
        return "Housekeeping was already triggered in the past #{LEASE_TIMEOUT / 60} minutes"
      end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
22

23
      GitlabShellWorker.perform_async(:gc, @project.path_with_namespace)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
24 25 26 27 28 29 30 31 32 33 34 35 36
      @project.pushes_since_gc = 0
      @project.save!

      "Housekeeping successfully started"
    end

    def needed?
      @project.pushes_since_gc >= 10
    end

    def increment!
      @project.pushes_since_gc += 1
      @project.save!
37 38 39 40 41 42 43
    end

    private

    def try_obtain_lease
      lease = ::Gitlab::ExclusiveLease.new("project_housekeeping:#{@project.id}", timeout: LEASE_TIMEOUT)
      lease.try_obtain
44 45 46
    end
  end
end