Class: OnlineMigrations::BackgroundDataMigrations::Scheduler
- Inherits:
-
Object
- Object
- OnlineMigrations::BackgroundDataMigrations::Scheduler
- 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.
Class Method Summary collapse
Instance Method Summary collapse
-
#run(shard: nil, concurrency: 1) ⇒ Object
Runs Scheduler.
Class Method Details
.run(**options) ⇒ Object
27 28 29 |
# File 'lib/online_migrations/background_data_migrations/scheduler.rb', line 27 def self.run(**) new.run(**) 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 52 53 54 55 56 57 58 |
# 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 migrations_to_enqueue = [] with_lock do stuck_migrations, active_migrations = relation.running.partition(&:stuck?) runnable_migrations = migrations_with_existing_classes(relation.pending) + stuck_migrations # Ensure no more than 'concurrency' migrations are running at the same time. remaining_to_enqueue = concurrency - active_migrations.count if remaining_to_enqueue > 0 runnable_migrations.take(remaining_to_enqueue).each do |migration| migration.update!(status: :enqueued) migrations_to_enqueue << migration end end end migrations_to_enqueue.each do |migration| enqueue_migration(migration) end true end |