Class: OnlineMigrations::BackgroundDataMigrations::MigrationJob
- Inherits:
-
Object
- Object
- OnlineMigrations::BackgroundDataMigrations::MigrationJob
- Includes:
- Sidekiq::IterableJob
- Defined in:
- lib/online_migrations/background_data_migrations/migration_job.rb
Overview
Sidekiq job responsible for running background data migrations.
Constant Summary collapse
- TICKER_INTERVAL =
seconds
5
Instance Method Summary collapse
- #build_enumerator(migration_id, cursor:) ⇒ Object
- #each_iteration(item, _migration_id) ⇒ Object
-
#initialize ⇒ MigrationJob
constructor
A new instance of MigrationJob.
- #on_complete ⇒ Object
- #on_resume ⇒ Object
- #on_start ⇒ Object
- #on_stop ⇒ Object
Constructor Details
#initialize ⇒ MigrationJob
Returns a new instance of MigrationJob.
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_data_migrations/migration_job.rb', line 24 def initialize super @migration = nil @data_migration = nil @ticker = Ticker.new(TICKER_INTERVAL) do |ticks, duration| # TODO: use 'cursor' accessor from sidekiq in the future. # https://github.com/sidekiq/sidekiq/pull/6606 @migration.persist_progress(@_cursor, ticks, duration) # When using a scheduler, these are running only from a single shard, but when inline - # these are run from each shard (not needed, but simplifies the implementation). # # Do not reload the migration when running inline, because it can be from a different shard # than the "default" shard (which is used to lookup background migrations). if !Utils.run_background_migrations_inline? # Reload to check if the migration's status changed etc. @migration.reload end end @throttle_checked_at = current_time end |
Instance Method Details
#build_enumerator(migration_id, cursor:) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/online_migrations/background_data_migrations/migration_job.rb', line 78 def build_enumerator(migration_id, cursor:) @migration = BackgroundDataMigrations::Migration.find(migration_id) cursor ||= @migration.cursor @migration.on_shard_if_present do @data_migration = @migration.data_migration collection_enum = @data_migration.build_enumerator(cursor: cursor) if collection_enum if !collection_enum.is_a?(Enumerator) raise ArgumentError, <<~MSG.squish #{@data_migration.class.name}#build_enumerator must return an Enumerator, got #{collection_enum.class.name}. MSG end collection_enum else collection = @data_migration.collection case collection when ActiveRecord::Relation = { cursor: cursor, batch_size: @data_migration.class.active_record_enumerator_batch_size || 100, } active_record_records_enumerator(collection, **) when ActiveRecord::Batches::BatchEnumerator if collection.start || collection.finish raise ArgumentError, <<~MSG.squish #{@data_migration.class.name}#collection does not support a batch enumerator with the "start" or "finish" options. MSG end active_record_relations_enumerator( collection.relation, batch_size: collection.batch_size, cursor: cursor, use_ranges: collection.use_ranges ) when Array array_enumerator(collection, cursor: cursor) else raise ArgumentError, <<~MSG.squish #{@data_migration.class.name}#collection must be either an ActiveRecord::Relation, ActiveRecord::Batches::BatchEnumerator, or Array. MSG end end end end |
#each_iteration(item, _migration_id) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/online_migrations/background_data_migrations/migration_job.rb', line 131 def each_iteration(item, _migration_id) if @migration.cancelling? || @migration.cancelled? || @migration.pausing? || @migration.paused? # Finish this exact sidekiq job. When the migration is paused # and will be resumed, a new job will be enqueued. finished = true throw :abort, finished elsif should_throttle? ActiveSupport::Notifications.instrument("throttled.background_data_migrations", migration: @migration) finished = false throw :abort, finished else @data_migration.around_process do @migration.data_migration.process(item) # Migration is refreshed regularly by ticker. pause = @migration.iteration_pause.to_f sleep(pause) if pause > 0 end @ticker.tick end end |
#on_complete ⇒ Object
71 72 73 74 75 76 |
# File 'lib/online_migrations/background_data_migrations/migration_job.rb', line 71 def on_complete # Job was manually cancelled. @migration.cancel if cancelled? @migration.complete end |
#on_resume ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/online_migrations/background_data_migrations/migration_job.rb', line 53 def on_resume if @migration.errored? # the job was retried @migration.update!( status: :running, error_class: nil, error_message: nil, backtrace: nil ) end @data_migration.after_resume end |
#on_start ⇒ Object
49 50 51 |
# File 'lib/online_migrations/background_data_migrations/migration_job.rb', line 49 def on_start @migration.start end |
#on_stop ⇒ Object
66 67 68 69 |
# File 'lib/online_migrations/background_data_migrations/migration_job.rb', line 66 def on_stop @ticker.persist @migration.stop end |