summaryrefslogtreecommitdiff
path: root/app/services/projects/housekeeping_service.rb
blob: 11be5b1badfc36a8aeb8e2785f48064e27918210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Projects::HousekeepingService class
#
# Used for git housekeeping
#
# Ex.
#   Projects::HousekeepingService.new(project).execute
#
module Projects
  class HousekeepingService < BaseService
    include Gitlab::ShellAdapter

    LEASE_TIMEOUT = 3600

    def initialize(project)
      @project = project
    end

    def execute
      if !try_obtain_lease
        return "Housekeeping was already triggered in the past #{LEASE_TIMEOUT / 60} minutes"
      end
      
      GitlabShellWorker.perform_async(:gc, @project.path_with_namespace)
      return "Housekeeping successfully started"
    end

    private

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