Class: OnlineMigrations::BackgroundMigrations::MigrationRunner

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

Overview

Runs single background 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_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_migrations/migration_runner.rb', line 7

def migration
  @migration
end

Instance Method Details

#finishObject

Finishes the background migration.

Keep running until the migration is finished.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/online_migrations/background_migrations/migration_runner.rb', line 79

def finish
  return if migration.completed?

  if migration.composite?
    migration.children.each do |child_migration|
      runner = self.class.new(child_migration)
      runner.finish
    end
  else
    # Mark is as finishing to avoid being picked up
    # by the background migrations scheduler.
    migration.finishing!
    migration.reset_failed_jobs_attempts

    while migration.finishing?
      run_migration_job
    end
  end
end

#run_all_migration_jobsObject

Note:

This method should not be used in production environments

Runs the background migration until completion.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/online_migrations/background_migrations/migration_runner.rb', line 53

def run_all_migration_jobs
  run_inline = OnlineMigrations.config.run_background_migrations_inline
  if run_inline && !run_inline.call
    raise "This method is not intended for use in production environments"
  end

  return if migration.completed?

  mark_as_running

  if migration.composite?
    migration.children.each do |child_migration|
      runner = self.class.new(child_migration)
      runner.run_all_migration_jobs
    end
  else
    while migration.running?
      run_migration_job
    end
  end
end

#run_migration_jobObject

Runs one background migration job.



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
# File 'lib/online_migrations/background_migrations/migration_runner.rb', line 14

def run_migration_job
  raise "Should not be called on a composite (with sharding) migration" if migration.composite?

  mark_as_running if migration.enqueued?
  migration_payload = notifications_payload(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)

    complete_parent_if_needed(migration) if migration.parent.present?
  end

  next_migration_job
end