Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/delayed_job/threaded_worker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
module Delayed
class ThreadedWorker < Delayed::Worker
# Delayed::Plugin callbacks register against the base Delayed::Worker.lifecycle,
# but setup_lifecycle rebuilds the lifecycle on the class it is called on. On this
# subclass it would reset a different lifecycle than the one plugins append to,
# so every ThreadedWorker.new would leak another callback into the base lifecycle.
# Delegate to the base class so each instantiation rebuilds the same lifecycle.
def self.setup_lifecycle
Delayed::Worker.setup_lifecycle
end

def self.lifecycle
Delayed::Worker.lifecycle
end

def initialize(options={})
super
@num_threads = options[:num_threads]
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/lib/delayed_job/threaded_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
worker = Delayed::ThreadedWorker.new({ num_threads: 2 })
expect(worker.instance_variable_get(:@grace_period_seconds)).to eq(30)
end

it 'does not accumulate enqueue lifecycle callbacks across instantiations' do
before_enqueue_callbacks = lambda do
Delayed::Worker.lifecycle.instance_variable_get(:@callbacks)[:enqueue].instance_variable_get(:@before).size
end

baseline = before_enqueue_callbacks.call
5.times { Delayed::ThreadedWorker.new(options) }
expect(before_enqueue_callbacks.call).to eq(baseline)
end
end

describe '#start' do
Expand Down
Loading