Skip to content

Commit 4d63ed2

Browse files
authored
Log worker name in steno logger (#4170)
1 parent cd7e670 commit 4d63ed2

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

lib/delayed_job/delayed_worker.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def get_initialized_delayed_worker(config, logger)
6161

6262
worker = Delayed::Worker.new(@queue_options)
6363
worker.name = @queue_options[:worker_name]
64+
Steno.config.context.data[:worker_name] = worker.name
6465
worker
6566
end
6667

lib/delayed_job/threaded_worker.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def start
3333
@num_threads.times do |thread_index|
3434
thread = Thread.new do
3535
Thread.current[:thread_index] = thread_index
36+
Steno.config.context.data[:worker_name] = name # override logged worker name with thread specific name
3637
threaded_start
3738
rescue Exception => e # rubocop:disable Lint/RescueException
3839
say "Unexpected error: #{e.message}\n#{e.backtrace.join("\n")}", 'error'

spec/unit/lib/delayed_job/delayed_worker_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
describe '#start_working' do
7070
let(:cc_delayed_worker) { CloudController::DelayedWorker.new(options) }
7171

72+
before { allow(delayed_worker).to receive(:name).and_return(options[:name]) }
73+
7274
it 'sets up the environment and starts the worker' do
7375
expect(environment).to receive(:setup_environment).with(nil)
7476
expect(Delayed::Worker).to receive(:new).with(anything).and_return(delayed_worker)
@@ -86,10 +88,16 @@
8688
expect(Delayed::Worker.sleep_delay).to eq(5)
8789
end
8890

91+
it 'sets the worker name in the Steno context' do
92+
cc_delayed_worker.start_working
93+
expect(Steno.config.context.data[:worker_name]).to eq(options[:name])
94+
end
95+
8996
context 'when the number of threads is specified' do
9097
before do
9198
allow(Delayed).to receive(:remove_const).with(:Worker)
9299
allow(Delayed).to receive(:const_set).with(:Worker, Delayed::ThreadedWorker)
100+
allow(threaded_worker).to receive(:name)
93101
options[:num_threads] = 7
94102
end
95103

@@ -122,7 +130,7 @@
122130
expect(environment).to receive(:setup_environment).with(nil)
123131
expect(Delayed::Worker).to receive(:new).with(anything).and_return(delayed_worker).once
124132
expect(delayed_worker).to receive(:name=).with(options[:name]).once
125-
expect(delayed_worker).to receive(:name).and_return(options[:name]).once
133+
expect(delayed_worker).to receive(:name).and_return(options[:name]).twice
126134
expect(Delayed::Job).to receive(:clear_locks!).with(options[:name]).once
127135

128136
cc_delayed_worker.clear_locks!
@@ -134,7 +142,7 @@
134142
expect(environment).to receive(:setup_environment).with(nil)
135143
expect(Delayed::Worker).to receive(:new).with(anything).and_return(threaded_worker).once
136144
expect(threaded_worker).to receive(:name=).with(options[:name]).once
137-
expect(threaded_worker).to receive(:name).and_return(options[:name]).once
145+
expect(threaded_worker).to receive(:name).and_return(options[:name]).twice
138146
expect(threaded_worker).to receive(:names_with_threads).and_return(["#{options[:name]} thread:1", "#{options[:name]} thread:2"]).once
139147
expect(Delayed::Job).to receive(:clear_locks!).with(options[:name]).once
140148
expect(Delayed::Job).to receive(:clear_locks!).with("#{options[:name]} thread:1").once

spec/unit/lib/delayed_job/threaded_worker_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@
6262
expect { worker.start }.to raise_error('Unexpected error occurred in one of the worker threads')
6363
expect(worker.instance_variable_get(:@unexpected_error)).to be true
6464
end
65+
66+
it 'sets the worker name in the Steno context' do
67+
steno_data_spy = spy('data')
68+
allow(Steno.config.context).to receive(:data).and_return(steno_data_spy)
69+
70+
worker.start
71+
worker.instance_variable_get(:@threads).each_with_index do |_, index|
72+
expect(steno_data_spy).to have_received(:[]=).with(:worker_name, "#{worker_name} thread:#{index + 1}")
73+
end
74+
end
6575
end
6676

6777
describe '#names_with_threads' do

0 commit comments

Comments
 (0)