Class: Delayed::Lifecycle

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

Constant Summary collapse

EVENTS =
{
  :enqueue    => [:job],
  :execute    => [:worker],
  :loop       => [:worker],
  :perform    => [:worker, :job],
  :error      => [:worker, :job],
  :failure    => [:worker, :job],
  :invoke_job => [:job]
}

Instance Method Summary collapse

Constructor Details

#initializeLifecycle

Returns a new instance of Lifecycle.



15
16
17
# File 'lib/delayed/lifecycle.rb', line 15

def initialize
  @callbacks = EVENTS.keys.inject({}) { |hash, e| hash[e] = Callback.new; hash }
end

Instance Method Details

#after(event, &block) ⇒ Object



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

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

#around(event, &block) ⇒ Object



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

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

#before(event, &block) ⇒ Object



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

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

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



31
32
33
34
35
36
37
38
39
# File 'lib/delayed/lifecycle.rb', line 31

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