Skip to content

Commit 127a972

Browse files
tarcieristeveklabnik
authored andcommitted
Fix will_fork? handling for before_fork hooks
The intended behavior of before_fork, according to a comment: lib/resque/worker.rb 258: # Only run before_fork hooks if we're actually going to fork If this is the case, then this behavior was actually broken on e.g. JRuby, as there was no guard on the invocation of the hook. This adds that guard, and cleans up the tests to check will_fork? before making assertions.
1 parent ca43fee commit 127a972

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

lib/resque/worker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def fork(job)
257257

258258
# Only run before_fork hooks if we're actually going to fork
259259
# (after checking @cant_fork)
260-
run_hook :before_fork, job
260+
run_hook :before_fork, job if will_fork?
261261

262262
begin
263263
# IronRuby doesn't support `Kernel.fork` yet

test/resque_hook_test.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def self.perform
4444

4545
assert_equal(0, counter)
4646
@worker.work(0)
47-
assert_equal(2, counter)
47+
assert_equal(@worker.will_fork? ? 2 : 0, counter)
4848
end
4949

5050
it 'calls after_fork after each job if forking' do
@@ -102,7 +102,12 @@ def self.perform
102102

103103
assert(!first && !second)
104104
@worker.work(0)
105-
assert(first && second)
105+
106+
if @worker.will_fork?
107+
assert(first && second)
108+
else
109+
assert(!first && !second)
110+
end
106111
end
107112

108113
it 'registers multiple after_forks' do

test/worker_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def self.perform
447447
assert !$BEFORE_FORK_CALLED
448448
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
449449
workerA.work(0)
450-
assert $BEFORE_FORK_CALLED
450+
assert $BEFORE_FORK_CALLED == workerA.will_fork?
451451
end
452452

453453
it "Will not call a before_fork hook when the worker can't fork" do

0 commit comments

Comments
 (0)