2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-21 13:15:54 +00:00
|
|
|
# Projects::HousekeepingService class
|
|
|
|
#
|
|
|
|
# Used for git housekeeping
|
|
|
|
#
|
|
|
|
# Ex.
|
2015-11-19 14:16:54 +00:00
|
|
|
# Projects::HousekeepingService.new(project).execute
|
2015-10-21 13:15:54 +00:00
|
|
|
#
|
|
|
|
module Projects
|
|
|
|
class HousekeepingService < BaseService
|
2017-08-21 15:01:37 +00:00
|
|
|
# Timeout set to 24h
|
|
|
|
LEASE_TIMEOUT = 86400
|
2016-03-14 14:18:52 +00:00
|
|
|
|
2016-03-15 10:03:43 +00:00
|
|
|
class LeaseTaken < StandardError
|
|
|
|
def to_s
|
|
|
|
"Somebody already triggered housekeeping for this project in the past #{LEASE_TIMEOUT / 60} minutes"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-21 13:15:54 +00:00
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-10-27 12:59:52 +00:00
|
|
|
lease_uuid = try_obtain_lease
|
|
|
|
raise LeaseTaken unless lease_uuid.present?
|
2016-03-14 15:49:24 +00:00
|
|
|
|
2017-08-23 12:25:44 +00:00
|
|
|
yield if block_given?
|
|
|
|
|
2016-10-27 12:59:52 +00:00
|
|
|
execute_gitlab_shell_gc(lease_uuid)
|
2016-03-14 15:49:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def needed?
|
2016-10-27 12:59:52 +00:00
|
|
|
pushes_since_gc > 0 && period_match? && housekeeping_enabled?
|
2016-03-14 15:49:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def increment!
|
2016-09-13 16:28:45 +00:00
|
|
|
Gitlab::Metrics.measure(:increment_pushes_since_gc) do
|
|
|
|
@project.increment_pushes_since_gc
|
2016-04-13 13:19:50 +00:00
|
|
|
end
|
2016-03-14 14:18:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-27 12:59:52 +00:00
|
|
|
def execute_gitlab_shell_gc(lease_uuid)
|
|
|
|
GitGarbageCollectWorker.perform_async(@project.id, task, lease_key, lease_uuid)
|
2016-07-12 09:58:06 +00:00
|
|
|
ensure
|
2016-10-27 12:59:52 +00:00
|
|
|
if pushes_since_gc >= gc_period
|
|
|
|
Gitlab::Metrics.measure(:reset_pushes_since_gc) do
|
|
|
|
@project.reset_pushes_since_gc
|
|
|
|
end
|
2016-07-05 08:03:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-14 14:18:52 +00:00
|
|
|
def try_obtain_lease
|
2016-04-13 13:19:50 +00:00
|
|
|
Gitlab::Metrics.measure(:obtain_housekeeping_lease) do
|
2016-10-27 12:59:52 +00:00
|
|
|
lease = ::Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT)
|
2016-04-13 13:19:50 +00:00
|
|
|
lease.try_obtain
|
|
|
|
end
|
2015-10-21 13:15:54 +00:00
|
|
|
end
|
2016-10-27 12:59:52 +00:00
|
|
|
|
|
|
|
def lease_key
|
|
|
|
"project_housekeeping:#{@project.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pushes_since_gc
|
|
|
|
@project.pushes_since_gc
|
|
|
|
end
|
|
|
|
|
|
|
|
def task
|
|
|
|
if pushes_since_gc % gc_period == 0
|
|
|
|
:gc
|
|
|
|
elsif pushes_since_gc % full_repack_period == 0
|
|
|
|
:full_repack
|
|
|
|
else
|
|
|
|
:incremental_repack
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def period_match?
|
|
|
|
[gc_period, full_repack_period, repack_period].any? { |period| pushes_since_gc % period == 0 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def housekeeping_enabled?
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.housekeeping_enabled
|
2016-10-27 12:59:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def gc_period
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.housekeeping_gc_period
|
2016-10-27 12:59:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_repack_period
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.housekeeping_full_repack_period
|
2016-10-27 12:59:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def repack_period
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.housekeeping_incremental_repack_period
|
2016-10-27 12:59:52 +00:00
|
|
|
end
|
2015-10-21 13:15:54 +00:00
|
|
|
end
|
|
|
|
end
|