delayed 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b69cac642b56964d70aba6e210125180c11fcd690ffe666a6501db5f5cc52779
4
- data.tar.gz: 6a2aca999b32ec0d2fb28fde33bb57725085c090eb6f2bcee29e50baa8f66a6f
3
+ metadata.gz: 16d1a1a8e3f41bee282a4d725afdf3be6419fceba8c5cef58a3eb35a7a406823
4
+ data.tar.gz: 9ad241b6880c13997ea7b58ba2bcfba02a1beb6692b050698fac30301cc9c90c
5
5
  SHA512:
6
- metadata.gz: d74267db864b39c8861136fc374a392608f08b47400cc872adfc5c19cc4188be202a4f6e68354f370987f56c9f5f7f9221432358c0a4663021b1718e18703127
7
- data.tar.gz: ac7f99b72554e9ec1ad69ecace6f637bd9aec6d6f4a74034030962ffc0542b3761542622775f52225e762ab248ef9e57a691b07852a16673574998fdb0099585
6
+ metadata.gz: a63a4f0a56f26c9b5bb3a987b6c2877068016823b21efb059744effc0eeb68255b4b193e7f0e0292f6d790e12138ad8c57bab032a7f1f3c1a89e2c533db45df6
7
+ data.tar.gz: 7912ee6d5d633293a2f1707d890c2fd5a60cbdb1c44ec215a8d2414db35902a272e4caad4ece1947c6475f4e90a7b5c1fe54ef320549681c04c948d7786cd4a8
@@ -20,13 +20,17 @@ module Delayed
20
20
  end
21
21
 
22
22
  def upsert_index(*args, **opts)
23
- dir(:up) { _add_or_replace_index(*args, **opts) }
24
- dir(:down) { _drop_index_if_exists(*args, **opts) }
23
+ with_retry_loop(**opts) do
24
+ dir(:up) { _add_or_replace_index(*args, **opts) }
25
+ dir(:down) { _drop_index_if_exists(*args, **opts) }
26
+ end
25
27
  end
26
28
 
27
29
  def remove_index_if_exists(*args, **opts)
28
- dir(:up) { _drop_index_if_exists(*args, **opts) }
29
- dir(:down) { _add_or_replace_index(*args, **opts) }
30
+ with_retry_loop(**opts) do
31
+ dir(:up) { _drop_index_if_exists(*args, **opts) }
32
+ dir(:down) { _add_or_replace_index(*args, **opts) }
33
+ end
30
34
  end
31
35
 
32
36
  RETRY_EXCEPTIONS = [
@@ -48,7 +52,7 @@ module Delayed
48
52
  end
49
53
  end
50
54
 
51
- def with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds)
55
+ def with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds, **_opts)
52
56
  dir(:both) { set_timeouts!(statement_timeout: statement_timeout, lock_timeout: lock_timeout) }
53
57
  yield
54
58
  ensure
@@ -61,24 +65,15 @@ module Delayed
61
65
  index = _lookup_index(table, columns, **opts)
62
66
  if index && !_index_matches?(index, **opts)
63
67
  Delayed.logger.warn("Recreating index #{index.name} (is invalid or does not match desired options)")
64
- _drop_index(table, name: index.name, **opts)
68
+ remove_index(table, name: index.name)
65
69
  end
66
- _add_index(table, columns, **opts) if !index || !_index_matches?(index, **opts)
70
+ opts = opts.except(:wait_timeout, :statement_timeout, :lock_timeout)
71
+ add_index(table, columns, **opts) if !index || !_index_matches?(index, **opts)
67
72
  end
68
73
 
69
74
  def _drop_index_if_exists(table, columns, **opts)
70
75
  index = _lookup_index(table, columns, **opts)
71
- _drop_index(table, name: index.name, **opts) if index
72
- end
73
-
74
- def _add_index(*args, **opts)
75
- index_opts = opts.slice!(:wait_timeout, :statement_timeout, :lock_timeout)
76
- with_retry_loop(**opts) { add_index(*args, **index_opts) }
77
- end
78
-
79
- def _drop_index(table, name:, **opts)
80
- opts.slice!(:wait_timeout, :statement_timeout, :lock_timeout)
81
- with_retry_loop(**opts) { remove_index(table, name: name) }
76
+ remove_index(table, name: index.name) if index
82
77
  end
83
78
 
84
79
  def _lookup_index(table, columns, **opts)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Delayed
4
- VERSION = '2.0.1'
4
+ VERSION = '2.0.2'
5
5
  end
@@ -33,35 +33,50 @@ describe Delayed::Helpers::Migration do
33
33
  migration.migration_start = Delayed::Job.db_time_now
34
34
  end
35
35
 
36
- describe '#with_retry_loop timeout tracking' do
36
+ describe '#upsert_index retry behavior' do
37
37
  it 'raises exception when wait_timeout is exceeded based on @migration_start' do
38
- # Simulate migration that started 6 minutes ago
39
38
  migration.migration_start = Delayed::Job.db_time_now - 6.minutes
40
39
 
40
+ allow(migration).to receive(:add_index).and_raise(ActiveRecord::LockWaitTimeout)
41
+ allow(migration.connection).to receive(:indexes).and_return([])
42
+
41
43
  expect {
42
- migration.with_retry_loop(wait_timeout: 5.minutes) do
43
- raise ActiveRecord::LockWaitTimeout
44
- end
44
+ migration.upsert_index(:delayed_jobs, :name, wait_timeout: 5.minutes)
45
45
  }.to raise_error(ActiveRecord::LockWaitTimeout)
46
46
  end
47
47
 
48
- it 'continues retrying while within the timeout window' do
49
- call_count = 0
48
+ it 're-checks for invalid index and drops it before retrying after timeout' do
49
+ add_index_calls = 0
50
+ remove_index_calls = 0
51
+ lookup_calls = 0
50
52
 
51
- # First retry is within timeout, second exceeds it
52
- allow(Delayed::Job).to receive(:db_time_now).and_return(
53
- migration.migration_start + 4.minutes, # Within timeout
54
- migration.migration_start + 6.minutes, # Exceeds timeout
53
+ invalid_opts = ActiveRecord.version >= Gem::Version.new('7.1.0') ? { valid?: false } : { unique: true }
54
+ invalid_index = instance_double(
55
+ ActiveRecord::ConnectionAdapters::IndexDefinition,
56
+ name: 'test_idx',
57
+ columns: ['name'],
58
+ **invalid_opts,
55
59
  )
56
60
 
57
- expect {
58
- migration.with_retry_loop(wait_timeout: 5.minutes) do
59
- call_count += 1
60
- raise ActiveRecord::LockWaitTimeout
61
- end
62
- }.to raise_error(ActiveRecord::LockWaitTimeout)
61
+ allow(migration.connection).to receive(:indexes) do
62
+ lookup_calls += 1
63
+ lookup_calls == 2 ? [invalid_index] : []
64
+ end
65
+
66
+ allow(migration).to receive(:add_index) do |*_args|
67
+ add_index_calls += 1
68
+ raise ActiveRecord::StatementTimeout, 'timeout' if add_index_calls == 1
69
+ end
70
+
71
+ allow(migration).to receive(:remove_index) do |*_args|
72
+ remove_index_calls += 1
73
+ end
74
+
75
+ migration.upsert_index(:delayed_jobs, :name, name: 'test_idx', wait_timeout: 5.minutes)
63
76
 
64
- expect(call_count).to eq(2)
77
+ expect(lookup_calls).to eq(3)
78
+ expect(remove_index_calls).to eq(1)
79
+ expect(add_index_calls).to eq(2)
65
80
  end
66
81
  end
67
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Griffith