switchman-inst-jobs 4.3.2 → 4.3.3

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: 024c887329179bac0f9961d4ee813225593d3012efdfcb90747dfc8013b470d3
4
- data.tar.gz: bcc91999036a817920b7a529a84cbe4f7dffc51e3629f14910e6d9dc44ffef6f
3
+ metadata.gz: 2801cf9e3bb894d8c46915457a1b1cf2f77ca10e49e1947665eb077a8bb58378
4
+ data.tar.gz: 558b1db888887f7463c8cafc3399001341be364dea5e5ff15d88a1213d9ed420
5
5
  SHA512:
6
- metadata.gz: 97ffa7cb9912e45e88330df7dc5ef8d56868f6c1ad09829dc331c9c973096fb82d4518a2b4873a1f6b1572cab5cac947e0a2132e630209aebe9f6f3d9da7e6c9
7
- data.tar.gz: e48e3ad03914eac68efdcb8d5dde9cb2abc0e5f42d9ff883b035176d223838ccf8377b36561af493b7e8ace9a88192a562f651d0297b4d7142c75053fdfc6129
6
+ metadata.gz: b01ca10ff4988509cee9728241d954b4b9bbfc6e2c9971730235f9adbcf50694bcae0226362f68a9e5602591368cf24e866ea0fbf653b05091184f7406fef774
7
+ data.tar.gz: 0b15b1296d8b5bf887f7ca8bd745886757e57e76591829ea2028b4c7a7c803bd86bc219cca4df630b49e8d61807b088437f94c5eb40b0daa8aac4111ebedb4fd
@@ -22,56 +22,82 @@ module SwitchmanInstJobs
22
22
  # Adapted from hold/unhold methods in base delayed jobs base
23
23
  # Wait is required to be able to safely move jobs
24
24
  def hold_jobs!(wait: false)
25
- self.jobs_held = true
26
- save! if changed?
27
- delayed_jobs_shard.activate(::Delayed::Backend::ActiveRecord::AbstractJob) do
28
- lock_jobs_for_hold
29
- end
30
- return unless wait
31
-
32
- delayed_jobs_shard.activate(::Delayed::Backend::ActiveRecord::AbstractJob) do
33
- while ::Delayed::Job.where(shard_id: id)
34
- .where.not(locked_at: nil)
35
- .where.not(locked_by: ::Delayed::Backend::Base::ON_HOLD_LOCKED_BY).exists?
36
- sleep 10
37
- lock_jobs_for_hold
38
- end
39
- end
25
+ ::Switchman::Shard.where(id: self).hold_jobs!(wait:)
40
26
  end
41
27
 
42
28
  def unhold_jobs!
43
- self.jobs_held = false
44
- if changed?
45
- save!
29
+ ::Switchman::Shard.where(id: self).unhold_jobs!
30
+ end
31
+
32
+ module ClassMethods
33
+ # Adapted from hold/unhold methods in base delayed jobs base
34
+ # Wait is required to be able to safely move jobs
35
+ def hold_jobs!(wait: false)
36
+ shards = all.to_a
37
+ wait_for_caches = false
38
+ shards.each do |shard|
39
+ shard.jobs_held = true
40
+ if shard.changed?
41
+ shard.save!
42
+ wait_for_caches = true if wait
43
+ end
44
+ end
45
+ shards_by_jobs_shard(shards).each do |jobs_shard, shard_ids|
46
+ jobs_shard.activate(::Delayed::Backend::ActiveRecord::AbstractJob) do
47
+ lock_jobs_for_hold(shard_ids)
48
+ end
49
+ end
50
+ return unless wait
51
+
46
52
  # Wait a little over the 60 second in-process shard cache clearing
47
53
  # threshold to ensure that all new jobs are now being enqueued
48
- # unlocked
54
+ # locked
49
55
  Rails.logger.debug("Waiting for caches to clear")
50
- sleep(65)
51
- end
52
- delayed_jobs_shard.activate(::Delayed::Backend::ActiveRecord::AbstractJob) do
53
- ::Delayed::Job.where(locked_by: ::Delayed::Backend::Base::ON_HOLD_LOCKED_BY, shard_id: id)
54
- .in_batches(of: 10_000)
55
- .update_all(
56
- locked_by: nil,
57
- locked_at: nil,
58
- attempts: 0,
59
- failed_at: nil
60
- )
56
+ sleep(65) if wait && wait_for_caches
57
+
58
+ shards_by_jobs_shard(shards).each do |jobs_shard, shard_ids|
59
+ jobs_shard.activate(::Delayed::Backend::ActiveRecord::AbstractJob) do
60
+ while ::Delayed::Job.where(shard_id: shard_ids)
61
+ .where.not(locked_at: nil)
62
+ .where.not(locked_by: ::Delayed::Backend::Base::ON_HOLD_LOCKED_BY).exists?
63
+ sleep 10
64
+ lock_jobs_for_hold(shard_ids)
65
+ end
66
+ end
67
+ end
61
68
  end
62
- end
63
-
64
- private
65
69
 
66
- def lock_jobs_for_hold
67
- ::Delayed::Job.where(locked_at: nil, shard_id: id).in_batches(of: 10_000).update_all(
68
- locked_by: ::Delayed::Backend::Base::ON_HOLD_LOCKED_BY,
69
- locked_at: ::Delayed::Job.db_time_now,
70
- attempts: ::Delayed::Backend::Base::ON_HOLD_COUNT
71
- )
72
- end
70
+ def unhold_jobs!
71
+ shards = all.to_a
72
+ waited = false
73
+ shards.each do |shard|
74
+ shard.jobs_held = false
75
+ next unless shard.changed?
76
+
77
+ shard.save!
78
+ next if waited
79
+
80
+ # Wait a little over the 60 second in-process shard cache clearing
81
+ # threshold to ensure that all new jobs are now being enqueued
82
+ # unlocked
83
+ Rails.logger.debug("Waiting for caches to clear")
84
+ sleep(65)
85
+ waited = true
86
+ end
87
+ shards_by_jobs_shard(shards).each do |jobs_shard, shard_ids|
88
+ jobs_shard.activate(::Delayed::Backend::ActiveRecord::AbstractJob) do
89
+ ::Delayed::Job.where(locked_by: ::Delayed::Backend::Base::ON_HOLD_LOCKED_BY, shard_id: shard_ids)
90
+ .in_batches(of: 10_000)
91
+ .update_all(
92
+ locked_by: nil,
93
+ locked_at: nil,
94
+ attempts: 0,
95
+ failed_at: nil
96
+ )
97
+ end
98
+ end
99
+ end
73
100
 
74
- module ClassMethods
75
101
  def clear_cache
76
102
  super
77
103
  remove_instance_variable(:@delayed_jobs_shards) if instance_variable_defined?(:@delayed_jobs_shards)
@@ -130,6 +156,22 @@ module SwitchmanInstJobs
130
156
 
131
157
  ::Switchman::Shard.merge(scope)
132
158
  end
159
+
160
+ private
161
+
162
+ # Group the given shards by the shard their jobs live on, returning a
163
+ # hash of delayed_jobs_shard => [shard_id, ...]
164
+ def shards_by_jobs_shard(shards)
165
+ shards.group_by(&:delayed_jobs_shard).transform_values { |group| group.map(&:id) }
166
+ end
167
+
168
+ def lock_jobs_for_hold(shard_ids)
169
+ ::Delayed::Job.where(locked_at: nil, shard_id: shard_ids).in_batches(of: 10_000).update_all(
170
+ locked_by: ::Delayed::Backend::Base::ON_HOLD_LOCKED_BY,
171
+ locked_at: ::Delayed::Job.db_time_now,
172
+ attempts: ::Delayed::Backend::Base::ON_HOLD_COUNT
173
+ )
174
+ end
133
175
  end
134
176
  end
135
177
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SwitchmanInstJobs
4
- VERSION = "4.3.2"
4
+ VERSION = "4.3.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman-inst-jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.2
4
+ version: 4.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Petty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-02-02 00:00:00.000000000 Z
11
+ date: 2026-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inst-jobs