Skip to content

Commit ddfef01

Browse files
ssteele110claude
andcommitted
Refactor: extract execute_master_setup to remove duplication
Extract shared master setup logic (reorder tests, store chunk metadata, push to queue) into execute_master_setup method. Used by both initial master setup in populate() and takeover in run_master_setup(). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 48442ba commit ddfef01

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

ruby/lib/ci/queue/redis/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def wait_for_master(timeout: 120)
7070
if respond_to?(:attempt_master_takeover, true) && attempt_master_takeover
7171
# Takeover succeeded - run master setup
7272
if respond_to?(:run_master_setup, true)
73-
run_master_setup
73+
execute_master_setup
7474
return true
7575
end
7676
end

ruby/lib/ci/queue/redis/worker.rb

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,9 @@ def populate(tests, random: Random.new)
3939
# All workers need an index of tests to resolve IDs
4040
@index = tests.map { |t| [t.id, t] }.to_h
4141
@total = tests.size
42+
@random = random
4243

43-
if acquire_master_role?
44-
with_master_setup_heartbeat do
45-
executables = reorder_tests(tests, random: random)
46-
47-
chunks = executables.select { |e| e.is_a?(CI::Queue::TestChunk) }
48-
individual_tests = executables.reject { |e| e.is_a?(CI::Queue::TestChunk) }
49-
50-
store_chunk_metadata(chunks) if chunks.any?
51-
52-
all_ids = chunks.map(&:id) + individual_tests.map(&:id)
53-
push(all_ids)
54-
end
55-
end
44+
execute_master_setup(tests) if acquire_master_role?
5645

5746
register_worker_presence
5847

@@ -589,18 +578,12 @@ def register_worker_presence
589578
raise if master?
590579
end
591580

592-
# Run master setup after a successful takeover.
593-
# Reconstructs the work the original master would have done.
594-
def run_master_setup
581+
# Shared logic for master setup - reorders tests, stores chunk metadata, and pushes to queue.
582+
# Used by both initial master setup (populate) and takeover.
583+
def execute_master_setup(tests)
595584
return unless @master && @index
596-
597-
# Reconstruct tests array from index
598-
tests = @index.values
599-
600585
with_master_setup_heartbeat do
601-
# Use the same seed for deterministic ordering
602-
random = Random.new(Digest::MD5.hexdigest(config.seed).to_i(16))
603-
executables = reorder_tests(tests, random: random)
586+
executables = reorder_tests(tests, random: @random)
604587

605588
chunks = executables.select { |e| e.is_a?(CI::Queue::TestChunk) }
606589
individual_tests = executables.reject { |e| e.is_a?(CI::Queue::TestChunk) }

0 commit comments

Comments
 (0)