housekeeping_service.rb 783 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
22
      if !try_obtain_lease
        return "Housekeeping was already triggered in the past #{LEASE_TIMEOUT / 60} minutes"
      end
      
23
      GitlabShellWorker.perform_async(:gc, @project.path_with_namespace)
24
25
26
27
28
29
30
31
      return "Housekeeping successfully started"
    end

    private

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