Skip to content
Snippets Groups Projects

Phase 2 enqueuer

Merged Steve Abrams requested to merge 349744-phase2-enqueuer into master
All threads resolved!
Compare and Show latest version
14 files
+ 285
74
Compare changes
  • Side-by-side
  • Inline
Files
14
@@ -10,14 +10,17 @@ class ContainerRepository < ApplicationRecord
REQUIRING_CLEANUP_STATUSES = %i[cleanup_unscheduled cleanup_scheduled].freeze
IDLE_MIGRATION_STATES = %w[default pre_import_done import_done import_aborted import_skipped].freeze
ACTIVE_MIGRATION_STATES = %w[pre_importing importing].freeze
ABORTABLE_MIGRATION_STATES = %w[default pre_importing importing].freeze
MIGRATION_STATES = (IDLE_MIGRATION_STATES + ACTIVE_MIGRATION_STATES).freeze
TooManyImportsError = Class.new(StandardError)
belongs_to :project
validates :name, length: { minimum: 0, allow_nil: false }
validates :name, uniqueness: { scope: :project_id }
validates :migration_state, presence: true, inclusion: { in: MIGRATION_STATES }
validates :migration_aborted_in_state, inclusion: { in: ACTIVE_MIGRATION_STATES }, allow_nil: true
validates :migration_aborted_in_state, inclusion: { in: ABORTABLE_MIGRATION_STATES }, allow_nil: true
validates :migration_retries_count, presence: true,
numericality: { greater_than_or_equal_to: 0 },
@@ -49,18 +52,8 @@ class ContainerRepository < ApplicationRecord
scope :aborted_imports, -> { where(migration_state: :import_aborted).unordered }
scope :recently_imported, -> { where(migration_state: :import_done).order(:migration_import_done_at) }
scope :with_target_import_tier, -> do
if ContainerRegistry::Migration.limit_gitlab_org?
where(namespaces: { id: 9970 })
else
where(plans: { name: ContainerRegistry::Migration.target_plan_name })
end
end
scope :ready_for_import, -> do
joins(
project: [namespace: [gitlab_subscription: [:hosted_plan]]]
).where(
joins(:project).where(
migration_state: [:default],
created_at: ...ContainerRegistry::Migration.created_before
).with_target_import_tier
@@ -75,7 +68,7 @@ class ContainerRepository < ApplicationRecord
).unordered
end
state_machine :migration_state, initial: :default do
state_machine :migration_state, initial: :default, use_transactions: false do
state :pre_importing do
validates :migration_pre_import_started_at, presence: true
validates :migration_pre_import_done_at, presence: false
@@ -126,11 +119,11 @@ class ContainerRepository < ApplicationRecord
end
event :abort_import do
transition %i[pre_importing importing] => :import_aborted
transition ABORTABLE_MIGRATION_STATES.map(&:to_sym) => :import_aborted
end
event :skip_import do
transition %i[default pre_importing importing] => :import_skipped
transition ABORTABLE_MIGRATION_STATES.map(&:to_sym) => :import_skipped
end
event :retry_pre_import do
@@ -147,7 +140,9 @@ class ContainerRepository < ApplicationRecord
end
after_transition any => :pre_importing do |container_repository|
container_repository.abort_import unless container_repository.migration_pre_import == :ok
container_repository.try_import do
container_repository.migration_pre_import
end
end
before_transition pre_importing: :pre_import_done do |container_repository|
@@ -160,7 +155,9 @@ class ContainerRepository < ApplicationRecord
end
after_transition any => :importing do |container_repository|
container_repository.abort_import unless container_repository.migration_import == :ok
container_repository.try_import do
container_repository.migration_import
end
end
before_transition importing: :import_done do |container_repository|
@@ -206,6 +203,11 @@ def self.with_unfinished_cleanup
with_enabled_policy.cleanup_unfinished
end
def self.with_target_import_tier
# overridden in ee
all
end
def skip_import(reason:)
self.migration_skipped_reason = reason
@@ -235,7 +237,7 @@ def abort_import
# it separately here instead of using callbacks
result = super
::ContainerRegistry::Import::EnqueuerWorker.perform_async
::ContainerRegistry::Migration::EnqueuerWorker.perform_async
result
end
@@ -245,11 +247,37 @@ def finish_import
# it separately here instead of using callbacks
result = super
::ContainerRegistry::Import::EnqueuerWorker.perform_async
::ContainerRegistry::Migration::EnqueuerWorker.perform_async
result
end
def try_import
raise ArgumentError, 'block not given' unless block_given?
try_count = 0
begin
try_count += 1
case yield
when :ok
true
when :too_many_imports
raise TooManyImportsError
else
abort_import
false
end
rescue TooManyImportsError
if try_count <= ::ContainerRegistry::Migration.start_max_retries
sleep 0.1 * try_count
retry
else
abort_import
false
end
end
end
# rubocop: disable CodeReuse/ServiceClass
def registry
@registry ||= begin
Loading