2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class ProjectImportState < ApplicationRecord
|
2018-05-02 09:35:04 -04:00
|
|
|
include AfterCommitQueue
|
2020-04-06 08:10:44 -04:00
|
|
|
include ImportState::SidekiqJobTracker
|
2018-05-02 09:35:04 -04:00
|
|
|
|
|
|
|
self.table_name = "project_mirror_data"
|
|
|
|
|
2022-03-31 14:08:39 -04:00
|
|
|
after_commit :expire_etag_cache
|
|
|
|
|
2018-05-02 09:35:04 -04:00
|
|
|
belongs_to :project, inverse_of: :import_state
|
|
|
|
|
|
|
|
validates :project, presence: true
|
|
|
|
|
2020-04-08 14:09:16 -04:00
|
|
|
alias_attribute :correlation_id, :correlation_id_value
|
|
|
|
|
2018-05-02 09:35:04 -04:00
|
|
|
state_machine :status, initial: :none do
|
|
|
|
event :schedule do
|
|
|
|
transition [:none, :finished, :failed] => :scheduled
|
|
|
|
end
|
|
|
|
|
|
|
|
event :force_start do
|
|
|
|
transition [:none, :finished, :failed] => :started
|
|
|
|
end
|
|
|
|
|
|
|
|
event :start do
|
|
|
|
transition scheduled: :started
|
|
|
|
end
|
|
|
|
|
|
|
|
event :finish do
|
|
|
|
transition started: :finished
|
|
|
|
end
|
|
|
|
|
|
|
|
event :fail_op do
|
|
|
|
transition [:scheduled, :started] => :failed
|
|
|
|
end
|
|
|
|
|
|
|
|
state :scheduled
|
|
|
|
state :started
|
|
|
|
state :finished
|
|
|
|
state :failed
|
|
|
|
|
|
|
|
after_transition [:none, :finished, :failed] => :scheduled do |state, _|
|
|
|
|
state.run_after_commit do
|
|
|
|
job_id = project.add_import_job
|
2020-04-08 14:09:16 -04:00
|
|
|
|
|
|
|
if job_id
|
|
|
|
correlation_id = Labkit::Correlation::CorrelationId.current_or_new_id
|
|
|
|
update(jid: job_id, correlation_id_value: correlation_id)
|
|
|
|
end
|
2018-05-02 09:35:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-21 19:06:08 -05:00
|
|
|
after_transition any => :finished do |state, _|
|
|
|
|
if state.jid.present?
|
|
|
|
Gitlab::SidekiqStatus.unset(state.jid)
|
|
|
|
|
|
|
|
state.update_column(:jid, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-09 07:12:04 -05:00
|
|
|
after_transition any => :failed do |state, _|
|
2022-03-31 11:08:32 -04:00
|
|
|
state.project.remove_import_data
|
2022-02-09 07:12:04 -05:00
|
|
|
end
|
|
|
|
|
2018-05-02 09:35:04 -04:00
|
|
|
after_transition started: :finished do |state, _|
|
|
|
|
project = state.project
|
|
|
|
|
|
|
|
project.reset_cache_and_import_attrs
|
|
|
|
|
|
|
|
if Gitlab::ImportSources.importer_names.include?(project.import_type) && project.repo_exists?
|
|
|
|
state.run_after_commit do
|
2022-04-22 08:08:38 -04:00
|
|
|
Projects::AfterImportWorker.perform_async(project.id)
|
2018-05-02 09:35:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-07 09:32:15 -05:00
|
|
|
|
2022-03-31 14:08:39 -04:00
|
|
|
def expire_etag_cache
|
|
|
|
if realtime_changes_path
|
|
|
|
Gitlab::EtagCaching::Store.new.tap do |store|
|
|
|
|
store.touch(realtime_changes_path)
|
|
|
|
rescue Gitlab::EtagCaching::Store::InvalidKeyError
|
|
|
|
# no-op: not every realtime changes endpoint is using etag caching
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def realtime_changes_path
|
|
|
|
Gitlab::Routing.url_helpers.polymorphic_path([:realtime_changes_import, project.import_type.to_sym], format: :json)
|
|
|
|
rescue NoMethodError
|
|
|
|
# polymorphic_path throws NoMethodError when no such path exists
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def relation_hard_failures(limit:)
|
|
|
|
project.import_failures.hard_failures_by_correlation_id(correlation_id).limit(limit)
|
|
|
|
end
|
|
|
|
|
2018-11-07 09:32:15 -05:00
|
|
|
def mark_as_failed(error_message)
|
|
|
|
original_errors = errors.dup
|
|
|
|
sanitized_message = Gitlab::UrlSanitizer.sanitize(error_message)
|
|
|
|
|
|
|
|
fail_op
|
|
|
|
|
|
|
|
update_column(:last_error, sanitized_message)
|
|
|
|
rescue ActiveRecord::ActiveRecordError => e
|
2020-05-25 11:07:58 -04:00
|
|
|
Gitlab::Import::Logger.error(
|
|
|
|
message: 'Error setting import status to failed',
|
|
|
|
error: e.message,
|
|
|
|
original_error: sanitized_message
|
|
|
|
)
|
2018-11-07 09:32:15 -05:00
|
|
|
ensure
|
|
|
|
@errors = original_errors
|
|
|
|
end
|
2018-11-27 04:41:27 -05:00
|
|
|
|
|
|
|
alias_method :no_import?, :none?
|
|
|
|
|
|
|
|
def in_progress?
|
2018-11-27 11:27:36 -05:00
|
|
|
scheduled? || started?
|
2018-11-27 04:41:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
|
|
|
# import? does SQL work so only run it if it looks like there's an import running
|
|
|
|
status == 'started' && project.import?
|
|
|
|
end
|
2018-05-02 09:35:04 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
ProjectImportState.prepend_mod_with('ProjectImportState')
|