class OnlineMigrations::BackgroundMigrations::MigrationRunner

Runs single background migration.

Attributes

migration[R]

Public Class Methods

new(migration) click to toggle source
# File lib/online_migrations/background_migrations/migration_runner.rb, line 9
def initialize(migration)
  @migration = migration
end

Public Instance Methods

finish() click to toggle source

Finishes the background migration.

Keep running until the migration is finished.

# File lib/online_migrations/background_migrations/migration_runner.rb, line 64
def finish
  return if migration.completed?

  # Mark is as finishing to avoid being picked up
  # by the background migrations scheduler.
  migration.finishing!

  while migration.finishing?
    run_migration_job
  end
end
run_all_migration_jobs() click to toggle source

Runs the background migration until completion.

@note This method should not be used in production environments

# File lib/online_migrations/background_migrations/migration_runner.rb, line 49
def run_all_migration_jobs
  raise "This method is not intended for use in production environments" if !Utils.developer_env?
  return if migration.completed?

  migration.running!

  while migration.running?
    run_migration_job
  end
end
run_migration_job() click to toggle source

Runs one background migration job.

# File lib/online_migrations/background_migrations/migration_runner.rb, line 14
def run_migration_job
  migration.running! if migration.enqueued?
  migration_payload = { background_migration: migration }

  if !migration.migration_jobs.exists?
    ActiveSupport::Notifications.instrument("started.background_migrations", migration_payload)
  end

  if should_throttle?
    ActiveSupport::Notifications.instrument("throttled.background_migrations", migration_payload)
    return
  end

  next_migration_job = find_or_create_next_migration_job

  if next_migration_job
    job_runner = MigrationJobRunner.new(next_migration_job)
    job_runner.run
  elsif !migration.migration_jobs.active.exists?
    if migration.migration_jobs.failed.exists?
      migration.failed!
    else
      migration.succeeded!
    end

    ActiveSupport::Notifications.instrument("completed.background_migrations", migration_payload)
  end

  next_migration_job
end

Private Instance Methods

create_migration_job!(min_value, max_value) click to toggle source
# File lib/online_migrations/background_migrations/migration_runner.rb, line 91
def create_migration_job!(min_value, max_value)
  migration.migration_jobs.create!(
    min_value: min_value,
    max_value: max_value
  )
end
find_or_create_next_migration_job() click to toggle source
# File lib/online_migrations/background_migrations/migration_runner.rb, line 81
def find_or_create_next_migration_job
  min_value, max_value = migration.next_batch_range

  if min_value && max_value
    create_migration_job!(min_value, max_value)
  else
    migration.migration_jobs.retriable.first
  end
end
should_throttle?() click to toggle source
# File lib/online_migrations/background_migrations/migration_runner.rb, line 77
def should_throttle?
  ::OnlineMigrations.config.background_migrations.throttler.call
end