Class: NatsWork::JobHooks
- Inherits:
-
Object
- Object
- NatsWork::JobHooks
- Defined in:
- lib/natswork/job_hooks.rb
Constant Summary collapse
- VALID_HOOKS =
i[before after retry failure success].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #after(&block) ⇒ Object
- #before(&block) ⇒ Object
- #clear(type = nil) ⇒ Object
-
#initialize ⇒ JobHooks
constructor
A new instance of JobHooks.
- #on_failure(&block) ⇒ Object
- #on_retry(&block) ⇒ Object
- #on_success(&block) ⇒ Object
- #register(type, &block) ⇒ Object
- #run(type, job, job_message, *args) ⇒ Object
Constructor Details
#initialize ⇒ JobHooks
Returns a new instance of JobHooks.
13 14 15 16 |
# File 'lib/natswork/job_hooks.rb', line 13 def initialize @hooks = {} VALID_HOOKS.each { |hook| @hooks[hook] = [] } end |
Class Method Details
.global ⇒ Object
8 9 10 |
# File 'lib/natswork/job_hooks.rb', line 8 def global @global ||= new end |
Instance Method Details
#after(&block) ⇒ Object
28 29 30 |
# File 'lib/natswork/job_hooks.rb', line 28 def after(&block) register(:after, &block) end |
#before(&block) ⇒ Object
24 25 26 |
# File 'lib/natswork/job_hooks.rb', line 24 def before(&block) register(:before, &block) end |
#clear(type = nil) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/natswork/job_hooks.rb', line 55 def clear(type = nil) if type @hooks[type] = [] if @hooks[type] else @hooks.keys.each { |key| @hooks[key] = [] } end end |
#on_failure(&block) ⇒ Object
36 37 38 |
# File 'lib/natswork/job_hooks.rb', line 36 def on_failure(&block) register(:failure, &block) end |
#on_retry(&block) ⇒ Object
32 33 34 |
# File 'lib/natswork/job_hooks.rb', line 32 def on_retry(&block) register(:retry, &block) end |
#on_success(&block) ⇒ Object
40 41 42 |
# File 'lib/natswork/job_hooks.rb', line 40 def on_success(&block) register(:success, &block) end |
#register(type, &block) ⇒ Object
18 19 20 21 22 |
# File 'lib/natswork/job_hooks.rb', line 18 def register(type, &block) raise ArgumentError, "Invalid hook type: #{type}" unless VALID_HOOKS.include?(type) @hooks[type] << block end |
#run(type, job, job_message, *args) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/natswork/job_hooks.rb', line 44 def run(type, job, , *args) return unless @hooks[type] @hooks[type].each do |hook| hook.call(job, , *args) rescue StandardError # Log error but don't fail job execution # In production, use proper logging end end |