Class: OnlineMigrations::BackgroundMigrations::MigrationJobRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/online_migrations/background_migrations/migration_job_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migration_job) ⇒ MigrationJobRunner

Returns a new instance of MigrationJobRunner.



12
13
14
# File 'lib/online_migrations/background_migrations/migration_job_runner.rb', line 12

def initialize(migration_job)
  @migration_job = migration_job
end

Instance Attribute Details

#migration_jobObject (readonly)

Returns the value of attribute migration_job.



7
8
9
# File 'lib/online_migrations/background_migrations/migration_job_runner.rb', line 7

def migration_job
  @migration_job
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/online_migrations/background_migrations/migration_job_runner.rb', line 16

def run
  job_payload = { background_migration_job: migration_job }
  if migration_job.attempts >= 1
    ActiveSupport::Notifications.instrument("retried.background_migrations", job_payload)
  end

  migration_job.update!(
    attempts: attempts + 1,
    status: :running,
    started_at: Time.current,
    finished_at: nil,
    error_class: nil,
    error_message: nil,
    backtrace: nil
  )

  ActiveSupport::Notifications.instrument("process_batch.background_migrations", job_payload) do
    migration.on_shard { run_batch }
  end

  migration_job.update!(status: :succeeded, finished_at: Time.current)
rescue Exception => e # rubocop:disable Lint/RescueException
  backtrace_cleaner = ::OnlineMigrations.config.backtrace_cleaner

  migration_job.update!(
    status: :failed,
    finished_at: Time.current,
    error_class: e.class.name,
    error_message: e.message,
    backtrace: backtrace_cleaner ? backtrace_cleaner.clean(e.backtrace) : e.backtrace
  )

  ::OnlineMigrations.config.background_migrations.error_handler.call(e, migration_job)
  raise if Utils.run_background_migrations_inline?
end