Class: Delayed::Lifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/delayed/lifecycle.rb

Constant Summary collapse

EVENTS =
{
  :perform          => [:worker, :job],
  :pop              => [:worker],
  :exceptional_exit => [:worker, :exception],
  :invoke_job       => [:job],
  :error            => [:worker, :job, :exception],
}

Instance Method Summary collapse

Constructor Details

#initializeLifecycle

Returns a new instance of Lifecycle.



13
14
15
# File 'lib/delayed/lifecycle.rb', line 13

def initialize
  reset!
end

Instance Method Details

#after(event, &block) ⇒ Object



26
27
28
# File 'lib/delayed/lifecycle.rb', line 26

def after(event, &block)
  add(:after, event, &block)
end

#around(event, &block) ⇒ Object



30
31
32
# File 'lib/delayed/lifecycle.rb', line 30

def around(event, &block)
  add(:around, event, &block)
end

#before(event, &block) ⇒ Object



22
23
24
# File 'lib/delayed/lifecycle.rb', line 22

def before(event, &block)
  add(:before, event, &block)
end

#reset!Object



17
18
19
20
# File 'lib/delayed/lifecycle.rb', line 17

def reset!
  @callbacks = EVENTS.keys.inject({}) { |hash, e| hash[e] = Callback.new; hash }
  Delayed::Worker.plugins.each { |plugin| plugin.reset! }
end

#run_callbacks(event, *args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/delayed/lifecycle.rb', line 34

def run_callbacks(event, *args, &block)
  missing_callback(event) unless @callbacks.has_key?(event)

  unless EVENTS[event].size == args.size
    raise ArgumentError, "Callback #{event} expects #{EVENTS[event].size} parameter(s): #{EVENTS[event].join(', ')}"
  end

  @callbacks[event].execute(*args, &block)
end