@@ -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
435406end
436407
0 commit comments