Class: SidekiqBackgrounder::Worker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/sidekiq_backgrounder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorker

Returns a new instance of Worker.



15
16
17
18
# File 'lib/sidekiq_backgrounder.rb', line 15

def initialize
  @config = SidekiqBackgrounder.configuration
  @logger = config.logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#exception_handler_methodObject (readonly)

Returns the value of attribute exception_handler_method.



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

def exception_handler_method
  @exception_handler_method
end

#gid_strObject (readonly)

Returns the value of attribute gid_str.



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

def gid_str
  @gid_str
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Instance Method Details

#perform(identifier, method, method_args = nil, raise_exception = true, exception_handler_method = nil) ⇒ Object



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sidekiq_backgrounder.rb', line 20

def perform(identifier, method, method_args = nil, raise_exception = true, exception_handler_method = nil)
  @object = if identifier.match(/gid:/)
              GlobalID.new(identifier).find rescue nil
            else
              identifier.constantize.new
            end
  logger.debug("#{object.class}: found/instantiated object ")

  if object.blank?
    logger.error "#{identifier.inspect} not found to call #{method.to_s.inspect} on, exiting..."
    return
  end
  @exception_handler_method = exception_handler_method

  begin
    logger.info "#{self.class}: running: #{object.class rescue object.inspect}##{method}, " +
      "method_args: #{method_args.inspect}"
    object.public_send(method, *method_args)
  rescue Exception => e
    if exception_handler_method.present?
      object.public_send(exception_handler_method, e)
    else
      msg = "class: \"#{object.to_gid.to_str rescue object.class.to_s || "N/A"}\", method: \"#{method}\" " +
        "method_args: #{method_args.inspect} encountered_error: #{extract_errors_with_backtrace(e)}"

      if defined?(:Honeybadger) && config.use_honeybadger
        Honeybadger.notify(
          msg,
          force: true,
          fingerprint: Digest::SHA1.hexdigest(msg),
          error_class: "SideKiqBackgrounder::Worker Error",
        )
      end

      unless raise_exception
        logger.error msg
        return
      end
      raise SidekiqBackgrounder::Error.new(msg)
    end
  end
end