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

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

#remove_index_if_exists(*args, **opts) ⇒ Object



22
23
24
25
# File 'lib/delayed/helpers/migration.rb', line 22

def remove_index_if_exists(*args, **opts)
  dir(:up) { _drop_index_if_exists(*args, **opts) }
  dir(:down) { _add_or_replace_index(*args, **opts) }
end

#upsert_index(*args, **opts) ⇒ Object



17
18
19
20
# File 'lib/delayed/helpers/migration.rb', line 17

def upsert_index(*args, **opts)
  dir(:up) { _add_or_replace_index(*args, **opts) }
  dir(:down) { _drop_index_if_exists(*args, **opts) }
end

#with_retry_loop(wait_timeout: 5.minutes, **opts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/delayed/helpers/migration.rb', line 33

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) ⇒ Object



46
47
48
49
50
51
# File 'lib/delayed/helpers/migration.rb', line 46

def with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds)
  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