Skip to content

Commit 408b7e8

Browse files
committed
before_fork and after_fork hooks are now arrays (no user API change)
1 parent d6b229a commit 408b7e8

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

README.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ All hooks can also be set using a setter, e.g.
687687

688688
Resque.after_fork = proc { puts "called" }
689689

690+
To clear all hooks, set nil or false:
691+
692+
Resque.after_fork = nil
690693

691694
Namespaces
692695
----------

lib/resque.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ def redis
5656
# Call with a block to set the hook.
5757
# Call with no arguments to return the hook.
5858
def before_first_fork(&block)
59-
block ? (@before_first_fork = block) : @before_first_fork
59+
@before_first_fork ||= []
60+
block ? (@before_first_fork << block) : @before_first_fork
6061
end
6162

6263
# Set a proc that will be called in the parent process before the
6364
# worker forks for the first time.
64-
def before_first_fork=(before_first_fork)
65-
@before_first_fork = before_first_fork
65+
def before_first_fork=(block)
66+
block ? (before_first_fork << block) : before_first_fork.clear
6667
end
6768

6869
# The `before_fork` hook will be run in the **parent** process
@@ -72,12 +73,13 @@ def before_first_fork=(before_first_fork)
7273
# Call with a block to set the hook.
7374
# Call with no arguments to return the hook.
7475
def before_fork(&block)
75-
block ? (@before_fork = block) : @before_fork
76+
@before_fork ||= []
77+
block ? (@before_fork << block) : @before_fork
7678
end
7779

7880
# Set the before_fork proc.
79-
def before_fork=(before_fork)
80-
@before_fork = before_fork
81+
def before_fork=(block)
82+
block ? (before_fork << block) : before_fork.clear
8183
end
8284

8385
# The `after_fork` hook will be run in the child process and is passed
@@ -87,12 +89,13 @@ def before_fork=(before_fork)
8789
# Call with a block to set the hook.
8890
# Call with no arguments to return the hook.
8991
def after_fork(&block)
90-
block ? (@after_fork = block) : @after_fork
92+
@after_fork ||= []
93+
block ? (@after_fork << block) : @after_fork
9194
end
9295

9396
# Set the after_fork proc.
94-
def after_fork=(after_fork)
95-
@after_fork = after_fork
97+
def after_fork=(block)
98+
block ? (after_fork << block) : after_fork.clear
9699
end
97100

98101
def to_s

lib/resque/worker.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,13 @@ def register_worker
312312

313313
# Runs a named hook, passing along any arguments.
314314
def run_hook(name, *args)
315-
return unless hook = Resque.send(name)
316-
msg = "Running #{name} hook"
317-
msg << " with #{args.inspect}" if args.any?
318-
log msg
315+
Resque.send(name).each do |hook|
316+
msg = "Running #{name} hook"
317+
msg << " with #{args.inspect}" if args.any?
318+
log msg
319319

320-
args.any? ? hook.call(*args) : hook.call
320+
args.any? ? hook.call(*args) : hook.call
321+
end
321322
end
322323

323324
# Unregisters ourself as a worker. Useful when shutting down.

0 commit comments

Comments
 (0)