Class: OnlineMigrations::BackgroundSchemaMigrations::MigrationRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/online_migrations/background_schema_migrations/migration_runner.rb

Overview

Runs single background schema migration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migration) ⇒ MigrationRunner

Returns a new instance of MigrationRunner.



9
10
11
# File 'lib/online_migrations/background_schema_migrations/migration_runner.rb', line 9

def initialize(migration)
  @migration = migration
end

Instance Attribute Details

#migrationObject (readonly)

Returns the value of attribute migration.



7
8
9
# File 'lib/online_migrations/background_schema_migrations/migration_runner.rb', line 7

def migration
  @migration
end

Instance Method Details

#runObject



13
14
15
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/online_migrations/background_schema_migrations/migration_runner.rb', line 13

def run
  return if migration.cancelled? || migration.succeeded?

  migration.running! if migration.enqueued? || migration.errored?
  migration_payload = { migration: migration }

  if migration.attempts == 0
    ActiveSupport::Notifications.instrument("started.background_schema_migrations", migration_payload)
  else
    ActiveSupport::Notifications.instrument("retried.background_schema_migrations", migration_payload)
  end

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

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

  ActiveSupport::Notifications.instrument("run.background_schema_migrations", migration_payload) do
    migration.run
  end

  # Background schema migrations could take a while to run. It is possible, that the process
  # never reaches this (or the rescue below) line of code. E.g., when it is force quitted
  # (SIGKILL etc.) and so the migration will end up in the "running" state and the query is
  # still executing (or already finished) in the database. This migration can either be safely
  # manually retried or will be picked up in the future by scheduler when it decides that
  # this migration is stuck.

  migration.update!(status: :succeeded, finished_at: Time.current)

  ActiveSupport::Notifications.instrument("completed.background_schema_migrations", migration_payload)
rescue Exception => e # rubocop:disable Lint/RescueException
  backtrace_cleaner = ::OnlineMigrations.config.backtrace_cleaner

  status = migration.attempts_exceeded? ? :failed : :errored

  migration.update!(
    status: status,
    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_schema_migrations.error_handler.call(e, migration)
  raise if Utils.run_background_migrations_inline?
end