debian-mirror-gitlab/app/services/projects/housekeeping_service.rb

108 lines
2.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
# Projects::HousekeepingService class
#
# Used for git housekeeping
#
# Ex.
# Projects::HousekeepingService.new(project).execute
#
module Projects
class HousekeepingService < BaseService
2017-09-10 17:25:29 +05:30
# Timeout set to 24h
LEASE_TIMEOUT = 86400
2019-07-31 22:56:46 +05:30
PACK_REFS_PERIOD = 6
2016-06-02 11:05:42 +05:30
class LeaseTaken < StandardError
def to_s
"Somebody already triggered housekeeping for this project in the past #{LEASE_TIMEOUT / 60} minutes"
end
end
2019-07-07 11:18:12 +05:30
def initialize(project, task = nil)
@project = project
2019-07-07 11:18:12 +05:30
@task = task
end
def execute
2017-08-17 22:00:37 +05:30
lease_uuid = try_obtain_lease
raise LeaseTaken unless lease_uuid.present?
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
yield if block_given?
2017-08-17 22:00:37 +05:30
execute_gitlab_shell_gc(lease_uuid)
2016-06-02 11:05:42 +05:30
end
def needed?
2017-08-17 22:00:37 +05:30
pushes_since_gc > 0 && period_match? && housekeeping_enabled?
2016-06-02 11:05:42 +05:30
end
def increment!
2016-09-29 09:46:39 +05:30
Gitlab::Metrics.measure(:increment_pushes_since_gc) do
@project.increment_pushes_since_gc
2016-06-02 11:05:42 +05:30
end
end
private
2017-08-17 22:00:37 +05:30
def execute_gitlab_shell_gc(lease_uuid)
GitGarbageCollectWorker.perform_async(@project.id, task, lease_key, lease_uuid)
2016-08-24 12:49:21 +05:30
ensure
2017-08-17 22:00:37 +05:30
if pushes_since_gc >= gc_period
Gitlab::Metrics.measure(:reset_pushes_since_gc) do
@project.reset_pushes_since_gc
end
2016-08-24 12:49:21 +05:30
end
end
2016-06-02 11:05:42 +05:30
def try_obtain_lease
Gitlab::Metrics.measure(:obtain_housekeeping_lease) do
2017-08-17 22:00:37 +05:30
lease = ::Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT)
2016-06-02 11:05:42 +05:30
lease.try_obtain
end
end
2017-08-17 22:00:37 +05:30
def lease_key
"project_housekeeping:#{@project.id}"
end
def pushes_since_gc
@project.pushes_since_gc
end
def task
2019-07-07 11:18:12 +05:30
return @task if @task
2017-08-17 22:00:37 +05:30
if pushes_since_gc % gc_period == 0
:gc
elsif pushes_since_gc % full_repack_period == 0
:full_repack
2019-07-31 22:56:46 +05:30
elsif pushes_since_gc % repack_period == 0
2017-08-17 22:00:37 +05:30
:incremental_repack
2019-07-31 22:56:46 +05:30
else
:pack_refs
2017-08-17 22:00:37 +05:30
end
end
def period_match?
2019-07-31 22:56:46 +05:30
[gc_period, full_repack_period, repack_period, PACK_REFS_PERIOD].any? { |period| pushes_since_gc % period == 0 }
2017-08-17 22:00:37 +05:30
end
def housekeeping_enabled?
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.housekeeping_enabled
2017-08-17 22:00:37 +05:30
end
def gc_period
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.housekeeping_gc_period
2017-08-17 22:00:37 +05:30
end
def full_repack_period
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.housekeeping_full_repack_period
2017-08-17 22:00:37 +05:30
end
def repack_period
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.housekeeping_incremental_repack_period
2017-08-17 22:00:37 +05:30
end
end
end