debian-mirror-gitlab/app/workers/repository_update_remote_mirror_worker.rb

54 lines
1.8 KiB
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
class RepositoryUpdateRemoteMirrorWorker
UpdateError = Class.new(StandardError)
include ApplicationWorker
2019-10-12 21:52:04 +05:30
include Gitlab::ExclusiveLeaseHelpers
2018-10-15 14:42:47 +05:30
sidekiq_options retry: 3, dead: false
2019-10-12 21:52:04 +05:30
LOCK_WAIT_TIME = 30.seconds
MAX_TRIES = 3
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
def perform(remote_mirror_id, scheduled_time, tries = 0)
remote_mirror = RemoteMirror.find_by_id(remote_mirror_id)
return unless remote_mirror
2018-10-15 14:42:47 +05:30
return if remote_mirror.updated_since?(scheduled_time)
2019-10-12 21:52:04 +05:30
# If the update is already running, wait for it to finish before running again
# This will wait for a total of 90 seconds in 3 steps
in_lock(remote_mirror_update_lock(remote_mirror.id),
retries: 3,
ttl: remote_mirror.max_runtime,
sleep_sec: LOCK_WAIT_TIME) do
update_mirror(remote_mirror, scheduled_time, tries)
end
rescue Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError
# If an update runs longer than 1.5 minutes, we'll reschedule it
# with a backoff. The next run will check if the previous update would
# include the changes that triggered this update and become a no-op.
self.class.perform_in(remote_mirror.backoff_delay, remote_mirror.id, scheduled_time, tries)
end
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
private
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
def update_mirror(mirror, scheduled_time, tries)
project = mirror.project
2018-10-15 14:42:47 +05:30
current_user = project.creator
2019-10-12 21:52:04 +05:30
result = Projects::UpdateRemoteMirrorService.new(project, current_user).execute(mirror, tries)
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
if result[:status] == :error && mirror.to_retry?
schedule_retry(mirror, scheduled_time, tries)
end
2018-10-15 14:42:47 +05:30
end
2019-10-12 21:52:04 +05:30
def remote_mirror_update_lock(mirror_id)
[self.class.name, mirror_id].join(':')
end
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
def schedule_retry(mirror, scheduled_time, tries)
self.class.perform_in(mirror.backoff_delay, mirror.id, scheduled_time, tries + 1)
2018-10-15 14:42:47 +05:30
end
end