Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
docs for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
defunkt committed Mar 3, 2010
1 parent 1bdd41e commit a145eda
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Expand Up @@ -675,6 +675,10 @@ The `after_fork` hook will be run in the child process and is passed
the current job. Any changes you make, therefor, will only live as
long as the job currently being processes.

All hooks can also be set using a setter, e.g.

Resque.after_fork = proc { puts "called" }

Namespaces
----------

Expand Down
47 changes: 37 additions & 10 deletions lib/resque.rb
Expand Up @@ -48,24 +48,51 @@ def redis
self.redis
end

#Set a proc that will be called once before the worker forks
# The `before_first_fork` hook will be run in the **parent** process
# only once, before forking to run the first job. Be careful- any
# changes you make will be permanent for the lifespan of the
# worker.
#
# Call with a block to set the hook.
# Call with no arguments to return the hook.
def before_first_fork(&block)
block ? (@before_first_fork = block) : @before_first_fork
end

# Set a proc that will be called in the parent process before the
# worker forks for the first time.
def before_first_fork=(before_first_fork)
@before_first_fork = before_first_fork
end

#Returns the before_first_fork proc
def before_first_fork
@before_first_fork
# The `before_fork` hook will be run in the **parent** process
# before every job, so be careful- any changes you make will be
# permanent for the lifespan of the worker.
#
# Call with a block to set the hook.
# Call with no arguments to return the hook.
def before_fork(&block)
block ? (@before_fork = block) : @before_fork
end

#Set a proc that will be called after the worker forks
def after_fork=(after_fork)
@after_fork = after_fork
# Set the before_fork proc.
def before_fork=(before_fork)
@before_fork = before_fork
end

#Returns the after_fork proc
def after_fork
@after_fork
# The `after_fork` hook will be run in the child process and is passed
# the current job. Any changes you make, therefor, will only live as
# long as the job currently being processes.
#
# Call with a block to set the hook.
# Call with no arguments to return the hook.
def after_fork(&block)
block ? (@after_fork = block) : @after_fork
end

# Set the after_fork proc.
def after_fork=(after_fork)
@after_fork = after_fork
end

def to_s
Expand Down
4 changes: 2 additions & 2 deletions test/worker_test.rb
Expand Up @@ -241,7 +241,7 @@
assert_equal 1, Resque.info[:processed]
end

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

test "Will call a after_fork proc after the worker has successfully forked" do
test "Will call an after_fork hook after forking" do
Resque.redis.flush_all
$AFTER_FORK_CALLED = false
Resque.after_fork = Proc.new { $AFTER_FORK_CALLED = true }
Expand Down

0 comments on commit a145eda

Please sign in to comment.