Skip to content

Commit

Permalink
Flush on execution end + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelchcki committed Jun 26, 2018
1 parent 767d697 commit a9fc131
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/ddtrace/contrib/delayed_job/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ def self.instrument(job, &block)
span.set_tag('delayed_job.attempts'.freeze, job.attempts)
span.span_type = pin.app_type

block.call(job)
yield job
end
end

def self.flush(worker, &block)
yield worker

pin = Pin.get_from(::Delayed::Worker)
pin.tracer.shutdown! if pin && pin.tracer
end

callbacks do |lifecycle|
lifecycle.around(:invoke_job, &method(:instrument))
lifecycle.around(:execute, &method(:flush))
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/ddtrace/contrib/delayed_job/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
Delayed::Worker.delay_jobs = false
end

describe 'instrumenting worker execution' do
let(:worker) { double(:worker, name: 'worker') }
before do
allow(tracer).to receive(:shutdown!).and_call_original
end

it 'execution callback yields control' do
expect { |b| Delayed::Worker.lifecycle.run_callbacks(:execute, worker, &b) }.to yield_with_args(worker)
end

it 'shutdown happens after yielding' do
Delayed::Worker.lifecycle.run_callbacks(:execute, worker) do
expect(tracer).not_to have_received(:shutdown!)
end

expect(tracer).to have_received(:shutdown!)
end
end

describe 'instrumented job invocation' do
let(:job_params) { {} }
subject(:job_run) { Delayed::Job.enqueue(SampleJob.new, job_params) }
Expand Down

0 comments on commit a9fc131

Please sign in to comment.