Class: OnlineMigrations::BackgroundDataMigrations::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/online_migrations/background_data_migrations/scheduler.rb

Overview

Class responsible for scheduling background data migrations.

Scheduler should be configured to run periodically, for example, via cron.

Examples:

Run via whenever

# add this to schedule.rb
every 1.minute do
  runner "OnlineMigrations.run_background_data_migrations"
end

Specific shard

every 1.minute do
  runner "OnlineMigrations.run_background_data_migrations(shard: :shard_two)"
end

Custom concurrency

every 1.minute do
  # Allow to run 2 data migrations in parallel.
  runner "OnlineMigrations.run_background_data_migrations(concurrency: 2)"
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(**options) ⇒ Object



27
28
29
# File 'lib/online_migrations/background_data_migrations/scheduler.rb', line 27

def self.run(**options)
  new.run(**options)
end

Instance Method Details

#run(shard: nil, concurrency: 1) ⇒ Object

Runs Scheduler



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/online_migrations/background_data_migrations/scheduler.rb', line 32

def run(shard: nil, concurrency: 1)
  relation = Migration.queue_order
  relation = relation.where(shard: shard) if shard

  with_lock do
    running = relation.running
    enqueued = relation.enqueued

    # Ensure no more than 'concurrency' migrations are running at the same time.
    remaining_to_enqueue = concurrency - running.count
    if remaining_to_enqueue > 0
      migrations_to_enqueue = enqueued.limit(remaining_to_enqueue)
      migrations_to_enqueue.each do |migration|
        enqueue_migration(migration)
      end
    end
  end

  true
end