Skip to content

Commit 74056d9

Browse files
committed
Revert "Merge pull request resque#680 from mrzor/hooks"
This reverts commit f793235, reversing changes made to 4eabd83.
1 parent 55daf0d commit 74056d9

File tree

5 files changed

+23
-171
lines changed

5 files changed

+23
-171
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
Gemfile.lock
22
doc/
33
test/dump.rdb
4-
test/dump-cluster.rdb

lib/resque.rb

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -85,46 +85,41 @@ def redis_id
8585
# changes you make will be permanent for the lifespan of the
8686
# worker.
8787
#
88-
# Call with a block to register a hook.
89-
# Call with no arguments to return all registered hooks.
88+
# Call with a block to set the hook.
89+
# Call with no arguments to return the hook.
9090
def before_first_fork(&block)
91-
block ? register_hook(:before_first_fork, block) : hooks(:before_first_fork)
91+
block ? (@before_first_fork = block) : @before_first_fork
9292
end
9393

94-
# Register a before_first_fork proc.
95-
def before_first_fork=(block)
96-
register_hook(:before_first_fork, block)
97-
end
94+
# Set a proc that will be called in the parent process before the
95+
# worker forks for the first time.
96+
attr_writer :before_first_fork
9897

9998
# The `before_fork` hook will be run in the **parent** process
10099
# before every job, so be careful- any changes you make will be
101100
# permanent for the lifespan of the worker.
102101
#
103-
# Call with a block to register a hook.
104-
# Call with no arguments to return all registered hooks.
102+
# Call with a block to set the hook.
103+
# Call with no arguments to return the hook.
105104
def before_fork(&block)
106-
block ? register_hook(:before_fork, block) : hooks(:before_fork)
105+
block ? (@before_fork = block) : @before_fork
107106
end
108107

109-
# Register a before_fork proc.
110-
def before_fork=(block)
111-
register_hook(:before_fork, block)
112-
end
108+
# Set the before_fork proc.
109+
attr_writer :before_fork
113110

114111
# The `after_fork` hook will be run in the child process and is passed
115112
# the current job. Any changes you make, therefore, will only live as
116113
# long as the job currently being processed.
117114
#
118-
# Call with a block to register a hook.
119-
# Call with no arguments to return all registered hooks.
115+
# Call with a block to set the hook.
116+
# Call with no arguments to return the hook.
120117
def after_fork(&block)
121-
block ? register_hook(:after_fork, block) : hooks(:after_fork)
118+
block ? (@after_fork = block) : @after_fork
122119
end
123120

124-
# Register an after_fork proc.
125-
def after_fork=(block)
126-
register_hook(:after_fork, block)
127-
end
121+
# Set the after_fork proc.
122+
attr_writer :after_fork
128123

129124
# The `before_pause` hook will be run in the parent process before the
130125
# worker has paused processing (via #pause_processing or SIGUSR2).
@@ -408,29 +403,5 @@ def keys
408403
key.sub("#{redis.namespace}:", '')
409404
end
410405
end
411-
412-
private
413-
414-
# Register a new proc as a hook. If the block is nil this is the
415-
# equivalent of removing all hooks of the given name.
416-
#
417-
# `name` is the hook that the block should be registered with.
418-
def register_hook(name, block)
419-
return clear_hooks(name) if block.nil?
420-
421-
@hooks ||= {}
422-
@hooks[name] ||= []
423-
@hooks[name] << block
424-
end
425-
426-
# Clear all hooks given a hook name.
427-
def clear_hooks(name)
428-
@hooks && @hooks[name] = []
429-
end
430-
431-
# Retrieve all hooks of a given name.
432-
def hooks(name)
433-
(@hooks && @hooks[name]) || []
434-
end
435406
end
436407

lib/resque/worker.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,12 @@ def register_worker
405405

406406
# Runs a named hook, passing along any arguments.
407407
def run_hook(name, *args)
408-
return unless hooks = Resque.send(name)
409-
msg = "Running #{name} hooks"
408+
return unless hook = Resque.send(name)
409+
msg = "Running #{name} hook"
410410
msg << " with #{args.inspect}" if args.any?
411411
log msg
412412

413-
hooks.each do |hook|
414-
args.any? ? hook.call(*args) : hook.call
415-
end
413+
args.any? ? hook.call(*args) : hook.call
416414
end
417415

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

test/resque_hook_test.rb

Lines changed: 0 additions & 120 deletions
This file was deleted.

test/worker_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
Resque.redis = Resque.redis # reset state in Resque object
66
Resque.redis.flushall
77

8+
Resque.before_first_fork = nil
9+
Resque.before_fork = nil
10+
Resque.after_fork = nil
11+
812
@worker = Resque::Worker.new(:jobs)
913
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
1014
end

0 commit comments

Comments
 (0)