Class: Delayed::Lifecycle

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

Constant Summary collapse

EVENTS =
{
  execute: [nil],
  enqueue: [:job],
  perform: %i(worker job),
  error: %i(worker job),
  failure: %i(worker job),
  thread: %i(worker job),
  invoke_job: [:job],
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeLifecycle

Returns a new instance of Lifecycle.



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

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

Instance Method Details

#after(event, &block) ⇒ Object



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

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

#around(event, &block) ⇒ Object



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

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

#before(event, &block) ⇒ Object



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

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

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



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

def run_callbacks(event, *args, &block)
  missing_callback(event) unless @callbacks.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