Module: Delayed::Helpers::Migration

Defined in:
lib/delayed/helpers/migration.rb

Defined Under Namespace

Modules: ClassMethods

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

.retry_exceptionsObject



36
37
38
39
40
41
42
# File 'lib/delayed/helpers/migration.rb', line 36

def self.retry_exceptions
  @retry_exceptions ||= [
    ActiveRecord::LockWaitTimeout,
    ActiveRecord::StatementTimeout,
    (PG::LockNotAvailable if defined?(PG::LockNotAvailable)),
  ].compact.freeze
end

Instance Method Details

#exec_migrationObject



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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/delayed/helpers/migration.rb', line 44

def with_retry_loop(wait_timeout: 5.minutes, **opts)
  with_timeouts(**opts) do
    loop do
      yield
      break
    rescue *Migration.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



57
58
59
60
61
62
# File 'lib/delayed/helpers/migration.rb', line 57

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