debian-mirror-gitlab/app/models/project_import_state.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
3.8 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class ProjectImportState < ApplicationRecord
2018-10-15 14:42:47 +05:30
include AfterCommitQueue
2020-04-22 19:07:51 +05:30
include ImportState::SidekiqJobTracker
2018-10-15 14:42:47 +05:30
self.table_name = "project_mirror_data"
2022-06-21 17:19:12 +05:30
after_commit :expire_etag_cache
2018-10-15 14:42:47 +05:30
belongs_to :project, inverse_of: :import_state
validates :project, presence: true
2023-04-23 21:23:45 +05:30
validates :checksums, json_schema: { filename: "project_import_stats" }
2018-10-15 14:42:47 +05:30
2020-04-22 19:07:51 +05:30
alias_attribute :correlation_id, :correlation_id_value
2018-10-15 14:42:47 +05:30
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
2022-08-13 15:12:31 +05:30
event :cancel do
transition [:none, :scheduled, :started] => :canceled
end
2018-10-15 14:42:47 +05:30
event :fail_op do
transition [:scheduled, :started] => :failed
end
state :scheduled
state :started
state :finished
state :failed
2022-08-13 15:12:31 +05:30
state :canceled
2018-10-15 14:42:47 +05:30
after_transition [:none, :finished, :failed] => :scheduled do |state, _|
state.run_after_commit do
job_id = project.add_import_job
2020-04-22 19:07:51 +05:30
if job_id
correlation_id = Labkit::Correlation::CorrelationId.current_or_new_id
update(jid: job_id, correlation_id_value: correlation_id)
end
2018-10-15 14:42:47 +05:30
end
end
2022-08-13 15:12:31 +05:30
after_transition any => [:canceled, :finished] do |state, _|
2020-01-01 13:55:28 +05:30
if state.jid.present?
Gitlab::SidekiqStatus.unset(state.jid)
state.update_column(:jid, nil)
end
end
2022-08-13 15:12:31 +05:30
after_transition any => [:canceled, :failed] do |state, _|
2022-06-21 17:19:12 +05:30
state.project.remove_import_data
2022-04-04 11:22:00 +05:30
end
2023-04-23 21:23:45 +05:30
before_transition started: [:finished, :canceled, :failed] do |state, _|
project = state.project
if project.github_import?
import_stats = ::Gitlab::GithubImport::ObjectCounter.summary(state.project)
state.update_column(:checksums, import_stats)
end
end
2018-10-15 14:42:47 +05:30
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-07-16 23:28:13 +05:30
Projects::AfterImportWorker.perform_async(project.id)
2018-10-15 14:42:47 +05:30
end
end
end
end
2018-12-13 13:39:08 +05:30
2022-06-21 17:19:12 +05:30
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-22 19:07:51 +05:30
def relation_hard_failures(limit:)
project.import_failures.hard_failures_by_correlation_id(correlation_id).limit(limit)
end
2018-12-13 13:39:08 +05:30
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-06-23 00:09:42 +05:30
Gitlab::Import::Logger.error(
message: 'Error setting import status to failed',
error: e.message,
original_error: sanitized_message
)
2018-12-13 13:39:08 +05:30
ensure
@errors = original_errors
end
2019-02-15 15:39:39 +05:30
alias_method :no_import?, :none?
def in_progress?
scheduled? || started?
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-10-15 14:42:47 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
ProjectImportState.prepend_mod_with('ProjectImportState')