2015-10-21 09:15:54 -04:00
|
|
|
# Projects::HousekeepingService class
|
|
|
|
#
|
|
|
|
# Used for git housekeeping
|
|
|
|
#
|
|
|
|
# Ex.
|
2015-11-19 09:16:54 -05:00
|
|
|
# Projects::HousekeepingService.new(project).execute
|
2015-10-21 09:15:54 -04:00
|
|
|
#
|
|
|
|
module Projects
|
|
|
|
class HousekeepingService < BaseService
|
2016-03-14 10:18:52 -04:00
|
|
|
LEASE_TIMEOUT = 3600
|
|
|
|
|
2016-03-15 06:03:43 -04: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 09:15:54 -04:00
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-05-30 07:46:47 -04:00
|
|
|
raise LeaseTaken unless try_obtain_lease
|
2016-03-14 11:49:24 -04:00
|
|
|
|
2016-07-12 05:58:06 -04:00
|
|
|
execute_gitlab_shell_gc
|
2016-03-14 11:49:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def needed?
|
|
|
|
@project.pushes_since_gc >= 10
|
|
|
|
end
|
|
|
|
|
|
|
|
def increment!
|
2016-07-12 05:58:06 -04:00
|
|
|
if Gitlab::ExclusiveLease.new("project_housekeeping:increment!:#{@project.id}", timeout: 60).try_obtain
|
|
|
|
Gitlab::Metrics.measure(:increment_pushes_since_gc) do
|
|
|
|
update_pushes_since_gc(@project.pushes_since_gc + 1)
|
|
|
|
end
|
2016-04-13 09:19:50 -04:00
|
|
|
end
|
2016-03-14 10:18:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-07-12 05:58:06 -04:00
|
|
|
def execute_gitlab_shell_gc
|
|
|
|
GitGarbageCollectWorker.perform_async(@project.id)
|
|
|
|
ensure
|
|
|
|
Gitlab::Metrics.measure(:reset_pushes_since_gc) do
|
|
|
|
update_pushes_since_gc(0)
|
2016-07-05 04:03:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-12 05:58:06 -04:00
|
|
|
def update_pushes_since_gc(new_value)
|
|
|
|
@project.update_column(:pushes_since_gc, new_value)
|
|
|
|
end
|
|
|
|
|
2016-03-14 10:18:52 -04:00
|
|
|
def try_obtain_lease
|
2016-04-13 09:19:50 -04:00
|
|
|
Gitlab::Metrics.measure(:obtain_housekeeping_lease) do
|
|
|
|
lease = ::Gitlab::ExclusiveLease.new("project_housekeeping:#{@project.id}", timeout: LEASE_TIMEOUT)
|
|
|
|
lease.try_obtain
|
|
|
|
end
|
2015-10-21 09:15:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|