Class: SidekiqGlass::Worker Abstract

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

Overview

This class is abstract.

Subclass can be used for all workers in app

Note:

Please use the execute method instead of perform - because of the extra reentracy layer that has been introduced. Also if you define additional after_failure method you can handle any timeout (or any other) errors and ensure reentrancy for your workers

Base worker class for all other workers

end

Examples:

Create a worker that will have a reentrancy for its task, that will

run for max 15 seconds

class LazyWorker < SidekiqGlass::Worker
  self.timeout = 15

  def execute(arg)
    # do some stuff here
  end

 def after_failure(arg)
   # do something if there is a timeout or any other error
 end

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggerLogger

Returns logger that we want to use.

Returns:

  • (Logger)

    logger that we want to use



32
33
34
# File 'lib/sidekiq_glass/worker.rb', line 32

def logger
  @logger ||= NullLogger.new
end

.timeoutObject

Returns the value of attribute timeout.



28
29
30
# File 'lib/sidekiq_glass/worker.rb', line 28

def timeout
  @timeout
end

Instance Method Details

#perform(*args) ⇒ Object

Parameters:

  • args

    Any arguments that we can get from Sidekiq



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sidekiq_glass/worker.rb', line 38

def perform(*args)
  if self.class.timeout
    ::SidekiqGlass::Timeout.perform(self.class.timeout) { execute(*args) }
  else
    execute(*args)
  end
rescue => exception
  self.class.logger.fatal(exception)
  after_failure(*args) if respond_to?(:after_failure)
  raise exception
end