Module: Joblin::Batching::Batch::Callback::CallbackWorkerCommon

Included in:
Compat::ActiveJob::ActiveJobCallbackWorker, Compat::Sidekiq::SidekiqCallbackWorker
Defined in:
lib/joblin/batching/callback.rb

Instance Method Summary collapse

Instance Method Details

#perform(definition, event, opts, bid, parent_bid) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/joblin/batching/callback.rb', line 11

def perform(definition, event, opts, bid, parent_bid)
  return unless VALID_CALLBACKS.include?(event.to_sym)

  method = nil
  target = :instance
  clazz = definition
  if clazz.is_a?(String)
    if clazz.include?('#')
      clazz, method = clazz.split("#")
    elsif clazz.include?('.')
      clazz, method = clazz.split(".")
      target = :class
    end
  end

  method ||= "on_#{event}"
  status = Batch::Status.new(bid)

  # safe_constantize returns nil (rather than raising NameError) for an
  # unknown/typo'd class, so a bad callback name logs a warning instead
  # of retrying forever and stranding the triggering batch.
  object = clazz.is_a?(String) ? clazz.safe_constantize : clazz

  if object
    target = target == :instance ? object.new : object
    if target.respond_to?(method, true)
      target.send(method, status, opts.with_indifferent_access)
    else
      Batch.logger.warn("Invalid callback method #{definition} - #{target.to_s} does not respond to #{method}")
    end
  else
    Batch.logger.warn("Invalid callback method #{definition} - Class #{clazz} not found")
  end
end