Skip to content

Commit a145eda

Browse files
committed
docs for hooks
1 parent 1bdd41e commit a145eda

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

README.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,10 @@ The `after_fork` hook will be run in the child process and is passed
675675
the current job. Any changes you make, therefor, will only live as
676676
long as the job currently being processes.
677677

678+
All hooks can also be set using a setter, e.g.
679+
680+
Resque.after_fork = proc { puts "called" }
681+
678682
Namespaces
679683
----------
680684

lib/resque.rb

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

51-
#Set a proc that will be called once before the worker forks
51+
# The `before_first_fork` hook will be run in the **parent** process
52+
# only once, before forking to run the first job. Be careful- any
53+
# changes you make will be permanent for the lifespan of the
54+
# worker.
55+
#
56+
# Call with a block to set the hook.
57+
# Call with no arguments to return the hook.
58+
def before_first_fork(&block)
59+
block ? (@before_first_fork = block) : @before_first_fork
60+
end
61+
62+
# Set a proc that will be called in the parent process before the
63+
# worker forks for the first time.
5264
def before_first_fork=(before_first_fork)
5365
@before_first_fork = before_first_fork
5466
end
5567

56-
#Returns the before_first_fork proc
57-
def before_first_fork
58-
@before_first_fork
68+
# The `before_fork` hook will be run in the **parent** process
69+
# before every job, so be careful- any changes you make will be
70+
# permanent for the lifespan of the worker.
71+
#
72+
# Call with a block to set the hook.
73+
# Call with no arguments to return the hook.
74+
def before_fork(&block)
75+
block ? (@before_fork = block) : @before_fork
5976
end
6077

61-
#Set a proc that will be called after the worker forks
62-
def after_fork=(after_fork)
63-
@after_fork = after_fork
78+
# Set the before_fork proc.
79+
def before_fork=(before_fork)
80+
@before_fork = before_fork
6481
end
6582

66-
#Returns the after_fork proc
67-
def after_fork
68-
@after_fork
83+
# The `after_fork` hook will be run in the child process and is passed
84+
# the current job. Any changes you make, therefor, will only live as
85+
# long as the job currently being processes.
86+
#
87+
# Call with a block to set the hook.
88+
# Call with no arguments to return the hook.
89+
def after_fork(&block)
90+
block ? (@after_fork = block) : @after_fork
91+
end
92+
93+
# Set the after_fork proc.
94+
def after_fork=(after_fork)
95+
@after_fork = after_fork
6996
end
7097

7198
def to_s

test/worker_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241
assert_equal 1, Resque.info[:processed]
242242
end
243243

244-
test "Will call a before_fork proc when the worker starts if set in the initializer only once" do
244+
test "Will call a before_first_fork hook only once" do
245245
Resque.redis.flush_all
246246
$BEFORE_FORK_CALLED = false
247247
Resque.before_first_fork = Proc.new { $BEFORE_FORK_CALLED = true }
@@ -254,7 +254,7 @@
254254
Resque.before_first_fork = nil
255255
end
256256

257-
test "Will call a after_fork proc after the worker has successfully forked" do
257+
test "Will call an after_fork hook after forking" do
258258
Resque.redis.flush_all
259259
$AFTER_FORK_CALLED = false
260260
Resque.after_fork = Proc.new { $AFTER_FORK_CALLED = true }

0 commit comments

Comments
 (0)