Module: Delayed::Helpers::Migration
- Defined in:
- lib/delayed/helpers/migration.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- RETRY_EXCEPTIONS =
[ ActiveRecord::LockWaitTimeout, ActiveRecord::StatementTimeout, (PG::LockNotAvailable if defined?(PG::LockNotAvailable)), ].compact.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #exec_migration ⇒ Object
- #remove_index_if_exists(*args, **opts) ⇒ Object
- #upsert_index(*args, **opts) ⇒ Object
- #with_retry_loop(wait_timeout: 5.minutes, **opts) ⇒ Object
- #with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds, **_opts) ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 9 |
# File 'lib/delayed/helpers/migration.rb', line 6 def self.included(base) base.extend(ClassMethods) delegate :concurrent_index_creation_supported?, to: :class end |
Instance Method Details
#exec_migration ⇒ Object
11 12 13 14 |
# File 'lib/delayed/helpers/migration.rb', line 11 def exec_migration(...) @migration_start = Delayed::Job.db_time_now super end |
#remove_index_if_exists(*args, **opts) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/delayed/helpers/migration.rb', line 29 def remove_index_if_exists(*args, **opts) with_retry_loop(**opts) do dir(:up) { _drop_index_if_exists(*args, **opts) } dir(:down) { _add_or_replace_index(*args, **opts) } end end |
#upsert_index(*args, **opts) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/delayed/helpers/migration.rb', line 22 def upsert_index(*args, **opts) with_retry_loop(**opts) do dir(:up) { _add_or_replace_index(*args, **opts) } dir(:down) { _drop_index_if_exists(*args, **opts) } end end |
#with_retry_loop(wait_timeout: 5.minutes, **opts) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/delayed/helpers/migration.rb', line 42 def with_retry_loop(wait_timeout: 5.minutes, **opts) with_timeouts(**opts) do loop do yield break rescue *RETRY_EXCEPTIONS => e raise if Delayed::Job.db_time_now - @migration_start > wait_timeout Delayed.logger.warn("Index creation failed for #{opts[:name]}: #{e.message}. Retrying...") end end end |
#with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds, **_opts) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/delayed/helpers/migration.rb', line 55 def with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds, **_opts) dir(:both) { set_timeouts!(statement_timeout: statement_timeout, lock_timeout: lock_timeout) } yield ensure dir(:both) { set_timeouts!(statement_timeout: nil, lock_timeout: nil) } end |