@@ -136,10 +136,9 @@ def work(interval = 5.0, &block)
136
136
if not paused? and job = reserve
137
137
log "got: #{ job . inspect } "
138
138
job . worker = self
139
- run_hook :before_fork , job
140
139
working_on job
141
140
142
- if @child = fork
141
+ if @child = fork ( job )
143
142
srand # Reseeding
144
143
procline "Forked #{ @child } at #{ Time . now . to_i } "
145
144
begin
@@ -148,11 +147,11 @@ def work(interval = 5.0, &block)
148
147
nil
149
148
end
150
149
else
151
- unregister_signal_handlers if ! @cant_fork && term_child
150
+ unregister_signal_handlers if will_fork? && term_child
152
151
procline "Processing #{ job . queue } since #{ Time . now . to_i } "
153
152
redis . client . reconnect # Don't share connection with parent
154
153
perform ( job , &block )
155
- exit! unless @cant_fork
154
+ exit! if will_fork?
156
155
end
157
156
158
157
done_working
@@ -184,7 +183,7 @@ def process(job = nil, &block)
184
183
# Processes a given job in the child.
185
184
def perform ( job )
186
185
begin
187
- run_hook :after_fork , job
186
+ run_hook :after_fork , job unless @cant_fork
188
187
job . perform
189
188
rescue Object => e
190
189
log "#{ job . inspect } failed: #{ e . inspect } "
@@ -228,15 +227,17 @@ def queues
228
227
229
228
# Not every platform supports fork. Here we do our magic to
230
229
# determine if yours does.
231
- def fork
232
- @cant_fork = true if $TESTING
233
-
230
+ def fork ( job )
234
231
return if @cant_fork
232
+
233
+ # Only run before_fork hooks if we're actually going to fork
234
+ # (after checking @cant_fork)
235
+ run_hook :before_fork , job
235
236
236
237
begin
237
238
# IronRuby doesn't support `Kernel.fork` yet
238
239
if Kernel . respond_to? ( :fork )
239
- Kernel . fork
240
+ Kernel . fork if will_fork?
240
241
else
241
242
raise NotImplementedError
242
243
end
@@ -507,6 +508,10 @@ def working?
507
508
def idle?
508
509
state == :idle
509
510
end
511
+
512
+ def will_fork?
513
+ !( @cant_fork || $TESTING)
514
+ end
510
515
511
516
# Returns a symbol representing the current worker state,
512
517
# which can be either :working or :idle
0 commit comments