switchman-inst-jobs 4.2.0 → 4.3.1

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: e1c01e6bdfd2a5cd3ddd756936a2793526c8e8860883bcd1ea8caa9febad979d
4
- data.tar.gz: 4998d1686e5dfceffb5c1a41dcf0959266c8caf73bd99aa0e50e89523e41ba51
3
+ metadata.gz: 3d126fdf9fae2bd957760dd1fdf1cac821e5103eb691fc2b293b3bda6170bb5c
4
+ data.tar.gz: 977e7915aea7e1e389ad95c190e5ae749a868f869f1d28542ee264342f57b89e
5
5
  SHA512:
6
- metadata.gz: aae64990ef75016c24ded178bf0edcf4b4343499b6d226d6c86104d9d15d23bd7a77acf35e622aff989e3292d0abf59bd15d4dd3d20e11e4f9e66e4fe393e581
7
- data.tar.gz: 854dd08052ca1b042646ce2fd08c4c0f76d2f5075f11ecebf34f04f8293c3ab2c8d9977817adf2b5e0f6e4c67a6726e49bd4dc9d491b961a391588e78c22a274
6
+ metadata.gz: 407591bf52c6068c6133b1654a68ffacb4aae0c0d941f7bdab9536a682c70a0a3ca04017c68e61ac6475fbd75a0b7cb93c35b1ce94e6e54b69fb638020802225
7
+ data.tar.gz: d5a69e5b56e07f4c90dcd5fea9f28f491c56f832acdd821b081ee5d3bb068af516b9ca114d464cda4f154edf4765b91c3d10c05f60b31c6612b5e72dca0c79ec
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddPreviousJobsShard < ActiveRecord::Migration[7.1]
4
+ def change
5
+ add_reference :switchman_shards, :previous_delayed_jobs_shard, foreign_key: { to_table: :switchman_shards }, index: false, if_not_exists: true
6
+ end
7
+ end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "set"
4
3
  require "parallel"
5
4
 
6
5
  module SwitchmanInstJobs
@@ -36,12 +35,24 @@ module SwitchmanInstJobs
36
35
  end
37
36
 
38
37
  def migrate_shards(shard_map)
39
- source_shards = Set[]
38
+ effective_map = shard_map.dup
39
+ source_shards = Hash.new([].freeze)
40
40
  target_shards = Hash.new([].freeze)
41
- shard_map.each do |(shard, target_shard)|
41
+ # Also add any incomplete moves to the source shards to ensure we clean up appropriately
42
+ ::Switchman::Shard.where.not(previous_delayed_jobs_shard_id: nil).find_each do |shard|
43
+ effective_map[shard.id] ||= shard.delayed_jobs_shard.id
44
+ end
45
+ effective_map.each do |(shard, target_shard)|
42
46
  shard = ::Switchman::Shard.find(shard) unless shard.is_a?(::Switchman::Shard)
43
- source_shards << shard.delayed_jobs_shard.id
44
47
  target_shard = target_shard.try(:id) || target_shard
48
+ # if a move was interrupted, the new shard is already set as the delayed_jobs_shard
49
+ # but we still have the old shard stored in previous_delayed_jobs_shard and should
50
+ # act as if we are moving from there in the first place
51
+ if shard.previous_delayed_jobs_shard_id && shard.delayed_jobs_shard.id == target_shard
52
+ source_shards[shard.previous_delayed_jobs_shard_id] += [shard.id]
53
+ else
54
+ source_shards[shard.delayed_jobs_shard.id] += [shard.id]
55
+ end
45
56
  target_shards[target_shard] += [shard.id]
46
57
 
47
58
  @validation_callbacks&.each do |proc|
@@ -49,26 +60,33 @@ module SwitchmanInstJobs
49
60
  end
50
61
  end
51
62
 
52
- # Do the updates in batches and then just clear redis instead of clearing them one at a time
53
- target_shards.each do |target_shard, shards|
54
- updates = { delayed_jobs_shard_id: target_shard, block_stranded: true }
55
- updates[:updated_at] = Time.zone.now if ::Switchman::Shard.column_names.include?("updated_at")
56
- ::Switchman::Shard.where(id: shards).update_all(updates)
63
+ ::Switchman::Shard.transaction do
64
+ # Do the updates in batches and then just clear redis instead of clearing them one at a time
65
+ source_shards.each do |source_shard, shards|
66
+ updates = { previous_delayed_jobs_shard_id: source_shard }
67
+ ::Switchman::Shard.where(id: shards).update_all(updates)
68
+ end
69
+ target_shards.each do |target_shard, shards|
70
+ updates = { delayed_jobs_shard_id: target_shard, block_stranded: true }
71
+ updates[:updated_at] = Time.zone.now if ::Switchman::Shard.column_names.include?("updated_at")
72
+ ::Switchman::Shard.where(id: shards).update_all(updates)
73
+ end
57
74
  end
58
75
  clear_shard_cache(default: ::Switchman::Shard.exists?(id: target_shards.values.flatten, default: true))
59
76
 
60
77
  ::Switchman::Shard.clear_cache
61
78
  # rubocop:disable Style/CombinableLoops
62
79
  # We first migrate strands so that we can stop blocking strands before we migrate unstranded jobs
63
- source_shards.each do |s|
80
+ source_shards.each_key do |s|
64
81
  ::Switchman::Shard.lookup(s).activate(::Delayed::Backend::ActiveRecord::AbstractJob) { migrate_strands }
65
82
  end
66
83
 
67
- source_shards.each do |s|
84
+ source_shards.each_key do |s|
68
85
  ::Switchman::Shard.lookup(s).activate(::Delayed::Backend::ActiveRecord::AbstractJob) { migrate_everything }
69
86
  end
70
- ensure_unblock_stranded_for(shard_map.map(&:first))
87
+ ensure_unblock_stranded_for(effective_map.map(&:first))
71
88
  # rubocop:enable Style/CombinableLoops
89
+ ::Switchman::Shard.where(id: effective_map.map(&:first)).update_all(previous_delayed_jobs_shard_id: nil)
72
90
  end
73
91
 
74
92
  # if :migrate_strands ran on any shards that fell into scenario 1, then
@@ -350,8 +368,8 @@ module SwitchmanInstJobs
350
368
 
351
369
  private
352
370
 
353
- def create_blocker_job(**kwargs)
354
- first_job = ::Delayed::Job.create!(**kwargs, next_in_strand: false)
371
+ def create_blocker_job(**)
372
+ first_job = ::Delayed::Job.create!(**, next_in_strand: false)
355
373
  first_job.payload_object = ::Delayed::PerformableMethod.new(Kernel, :sleep, args: [0])
356
374
  first_job.tag = "Kernel.sleep"
357
375
  first_job.source = "JobsMigrator::StrandBlocker"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SwitchmanInstJobs
4
- VERSION = "4.2.0"
4
+ VERSION = "4.3.1"
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.2.0
4
+ version: 4.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Petty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-08-22 00:00:00.000000000 Z
11
+ date: 2026-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inst-jobs
@@ -53,7 +53,7 @@ dependencies:
53
53
  version: '7.0'
54
54
  - - "<"
55
55
  - !ruby/object:Gem::Version
56
- version: '8.0'
56
+ version: '8.1'
57
57
  type: :runtime
58
58
  prerelease: false
59
59
  version_requirements: !ruby/object:Gem::Requirement
@@ -63,7 +63,7 @@ dependencies:
63
63
  version: '7.0'
64
64
  - - "<"
65
65
  - !ruby/object:Gem::Version
66
- version: '8.0'
66
+ version: '8.1'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: switchman
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +136,7 @@ files:
136
136
  - db/migrate/20220203063200_remove_old_singleton_index.rb
137
137
  - db/migrate/20220328152900_add_failed_jobs_indicies.rb
138
138
  - db/migrate/20220519204546_add_requeued_job_id_to_failed_jobs.rb
139
+ - db/migrate/20260120092005_add_previous_jobs_shard.rb
139
140
  - lib/switchman-inst-jobs.rb
140
141
  - lib/switchman_inst_jobs.rb
141
142
  - lib/switchman_inst_jobs/active_record/connection_adapters/postgresql_adapter.rb
@@ -171,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
171
172
  requirements:
172
173
  - - ">="
173
174
  - !ruby/object:Gem::Version
174
- version: '3.1'
175
+ version: '3.2'
175
176
  required_rubygems_version: !ruby/object:Gem::Requirement
176
177
  requirements:
177
178
  - - ">="