2017-10-13 16:50:36 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
# AdvanceStageWorker is a worker used by the GitHub importer to wait for a
|
|
|
|
# number of jobs to complete, without blocking a thread. Once all jobs have
|
|
|
|
# been completed this worker will advance the import process to the next
|
|
|
|
# stage.
|
|
|
|
class AdvanceStageWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2017-10-13 16:50:36 +00:00
|
|
|
|
2017-11-28 16:16:50 +00:00
|
|
|
sidekiq_options dead: false
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
INTERVAL = 30.seconds.to_i
|
|
|
|
|
|
|
|
# The number of seconds to wait (while blocking the thread) before
|
2018-10-30 10:53:01 +00:00
|
|
|
# continuing to the next waiter.
|
2017-10-13 16:50:36 +00:00
|
|
|
BLOCKING_WAIT_TIME = 5
|
|
|
|
|
|
|
|
# The known importer stages and their corresponding Sidekiq workers.
|
|
|
|
STAGES = {
|
|
|
|
issues_and_diff_notes: Stage::ImportIssuesAndDiffNotesWorker,
|
|
|
|
notes: Stage::ImportNotesWorker,
|
2018-06-06 16:42:18 +00:00
|
|
|
lfs_objects: Stage::ImportLfsObjectsWorker,
|
2017-10-13 16:50:36 +00:00
|
|
|
finish: Stage::FinishImportWorker
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
# project_id - The ID of the project being imported.
|
|
|
|
# waiters - A Hash mapping Gitlab::JobWaiter keys to the number of
|
|
|
|
# remaining jobs.
|
|
|
|
# next_stage - The name of the next stage to start when all jobs have been
|
|
|
|
# completed.
|
|
|
|
def perform(project_id, waiters, next_stage)
|
2018-11-27 09:41:27 +00:00
|
|
|
return unless import_state = find_import_state(project_id)
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
new_waiters = wait_for_jobs(waiters)
|
|
|
|
|
|
|
|
if new_waiters.empty?
|
|
|
|
# We refresh the import JID here so workers importing individual
|
|
|
|
# resources (e.g. notes) don't have to do this all the time, reducing
|
|
|
|
# the pressure on Redis. We _only_ do this once all jobs are done so
|
|
|
|
# we don't get stuck forever if one or more jobs failed to notify the
|
|
|
|
# JobWaiter.
|
2018-11-27 09:41:27 +00:00
|
|
|
import_state.refresh_jid_expiration
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
STAGES.fetch(next_stage.to_sym).perform_async(project_id)
|
|
|
|
else
|
|
|
|
self.class.perform_in(INTERVAL, project_id, new_waiters, next_stage)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_jobs(waiters)
|
|
|
|
waiters.each_with_object({}) do |(key, remaining), new_waiters|
|
|
|
|
waiter = JobWaiter.new(remaining, key)
|
|
|
|
|
|
|
|
# We wait for a brief moment of time so we don't reschedule if we can
|
|
|
|
# complete the work fast enough.
|
|
|
|
waiter.wait(BLOCKING_WAIT_TIME)
|
|
|
|
|
|
|
|
next unless waiter.jobs_remaining.positive?
|
|
|
|
|
|
|
|
new_waiters[waiter.key] = waiter.jobs_remaining
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-11-27 09:41:27 +00:00
|
|
|
def find_import_state(project_id)
|
|
|
|
ProjectImportState.select(:jid).with_status(:started).find_by(project_id: project_id)
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|