Module: Bellbro::Hooks::ClassMethods

Defined in:
lib/bellbro/hooks.rb

Instance Method Summary collapse

Instance Method Details

#after(*hooks, &block) ⇒ Object

Public: Declare hooks to run after Worker invocation. The after method may be called multiple times; subsequent calls prepend declared hooks to existing after hooks.

hooks - Zero or more Symbol method names representing instance methods

to be called after worker invocation.

block - An optional block to be executed as a hook. If given, the block

is executed before methods corresponding to any given Symbols.

Examples

class MyWorker
  include Worker

  after :set_finish_time

  after do
    puts "finished"
  end

  def call
    puts "called"
  end

  private

  def set_finish_time
    context.finish_time = Time.now
  end
end

Returns nothing.



147
148
149
150
# File 'lib/bellbro/hooks.rb', line 147

def after(*hooks, &block)
  hooks << block if block
  hooks.each { |hook| after_hooks.push(hook) }
end

#after_hooksObject

Internal: An Array of declared hooks to run before Worker invocation. The hooks appear in the order in which they will be run.

Examples

class MyWorker
  include Sidekiq::Worker

  after :set_finish_time, :say_goodbye
end

MyWorker.after_hooks
# => [:say_goodbye, :set_finish_time]

Returns an Array of Symbols and Procs.



210
211
212
# File 'lib/bellbro/hooks.rb', line 210

def after_hooks
  @after_hooks ||= []
end

#always(*hooks, &block) ⇒ Object



152
153
154
155
# File 'lib/bellbro/hooks.rb', line 152

def always(*hooks, &block)
  hooks << block if block
  hooks.each { |hook| always_hooks.unshift(hook) }
end

#always_hooksObject



214
215
216
# File 'lib/bellbro/hooks.rb', line 214

def always_hooks
  @always_hooks ||= []
end

#around(*hooks, &block) ⇒ Object

Public: Declare hooks to run around worker invocation. The around method may be called multiple times; subsequent calls append declared hooks to existing around hooks.

hooks - Zero or more Symbol method names representing instance methods

to be called around worker invocation. Each instance method
invocation receives an argument representing the next link in
the around hook chain.

block - An optional block to be executed as a hook. If given, the block

is executed after methods corresponding to any given Symbols.

Examples

class MyWorker
  include Worker

  around :time_execution

  around do |worker|
    puts "started"
    worker.call
    puts "finished"
  end

  def call
    puts "called"
  end

  private

  def time_execution(worker)
    context.start_time = Time.now
    worker.call
    context.finish_time = Time.now
  end
end

Returns nothing.



65
66
67
68
# File 'lib/bellbro/hooks.rb', line 65

def around(*hooks, &block)
  hooks << block if block
  hooks.each { |hook| around_hooks.push(hook) }
end

#around_hooksObject

Internal: An Array of declared hooks to run around Worker invocation. The hooks appear in the order in which they will be run.

Examples

class MyWorker
  include Worker

  around :time_execution, :use_transaction
end

MyWorker.around_hooks
# => [:time_execution, :use_transaction]

Returns an Array of Symbols and Procs.



172
173
174
# File 'lib/bellbro/hooks.rb', line 172

def around_hooks
  @around_hooks ||= []
end

#before(*hooks, &block) ⇒ Object

Public: Declare hooks to run before Worker invocation. The before method may be called multiple times; subsequent calls append declared hooks to existing before hooks.

hooks - Zero or more Symbol method names representing instance methods

to be called before worker invocation.

block - An optional block to be executed as a hook. If given, the block

is executed after methods corresponding to any given Symbols.

Examples

class MyWorker
  include Worker

  before :set_start_time

  before do
    puts "started"
  end

  def call
    puts "called"
  end

  private

  def set_start_time
    context.start_time = Time.now
  end
end

Returns nothing.



110
111
112
113
# File 'lib/bellbro/hooks.rb', line 110

def before(*hooks, &block)
  hooks << block if block
  hooks.each { |hook| before_hooks.push(hook) }
end

#before_hooksObject

Internal: An Array of declared hooks to run before Worker invocation. The hooks appear in the order in which they will be run.

Examples

class MyWorker
  include Sidekiq::Worker

  before :set_start_time, :say_hello
end

MyWorker.before_hooks
# => [:set_start_time, :say_hello]

Returns an Array of Symbols and Procs.



191
192
193
# File 'lib/bellbro/hooks.rb', line 191

def before_hooks
  @before_hooks ||= []
end

#time_out_in(interval) ⇒ Object



70
71
72
# File 'lib/bellbro/hooks.rb', line 70

def time_out_in(interval)
  @time_out_interval = interval
end

#time_out_intervalObject



74
75
76
# File 'lib/bellbro/hooks.rb', line 74

def time_out_interval
  @time_out_interval
end