Skip to content

Commit f1042b1

Browse files
scotttamdefunkt
authored andcommitted
Add ability to see a pre_fork hook that will be called once before each worker forks for the first time.
1 parent aa48ac6 commit f1042b1

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

README.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ this way we can tell our Sinatra app about the config file:
648648

649649
Now everyone is on the same page.
650650

651+
If you wish to have a Proc called before the worker forks for the first time, you
652+
can add it in the initializer like so:
653+
654+
Resque.before_fork = Proc.new { puts "CALL ME ONCE BEFORE THE WORKER FORKS THE FIRST TIME" }
655+
651656

652657
Namespaces
653658
----------

lib/resque.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def redis
4848
self.redis
4949
end
5050

51+
#Set a proc that will be called once before the worker forks
52+
def before_fork=(before_fork)
53+
@before_fork = before_fork
54+
end
55+
56+
#Returns the before_fork proc
57+
def before_fork
58+
@before_fork
59+
end
60+
5161
def to_s
5262
"Resque Client connected to #{redis.server}"
5363
end

lib/resque/worker.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def self.exists?(worker_id)
6161
redis.sismember(:workers, worker_id)
6262
end
6363

64+
#Sets the before_hook proc
65+
def self.before_fork=(before_fork)
66+
@@before_fork = before_fork
67+
end
68+
6469
# Workers should be initialized with an array of string queue
6570
# names. The order is important: a Worker will check the first
6671
# queue given for a job. If none is found, it will check the
@@ -202,6 +207,7 @@ def startup
202207
enable_gc_optimizations
203208
register_signal_handlers
204209
prune_dead_workers
210+
before_fork
205211
register_worker
206212
end
207213

@@ -306,6 +312,11 @@ def register_worker
306312
started!
307313
end
308314

315+
#Call any before_fork procs, if any
316+
def before_fork
317+
@@before_fork.call if Worker.class_variable_defined?(:@@before_fork)
318+
end
319+
309320
# Unregisters ourself as a worker. Useful when shutting down.
310321
def unregister_worker
311322
redis.srem(:workers, self)

test/worker_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,21 @@
240240
@worker.work(0)
241241
assert_equal 1, Resque.info[:processed]
242242
end
243+
244+
test "Will call a pre-fork proc when the worker starts if set in the initializer only once" do
245+
$BEFORE_FORK_CALLED = false
246+
Resque.before_fork = Proc.new { $BEFORE_FORK_CALLED = true }
247+
Resque::Worker.before_fork = Resque.before_fork
248+
workerA = Resque::Worker.new(:jobs)
249+
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
250+
251+
assert !$BEFORE_FORK_CALLED
252+
workerA.work(0)
253+
assert $BEFORE_FORK_CALLED
254+
255+
$BEFORE_FORK_CALLED = false
256+
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
257+
assert !$BEFORE_FORK_CALLED
258+
Resque::Worker.send(:remove_class_variable, :@@before_fork)
259+
end
243260
end

0 commit comments

Comments
 (0)