Module: WorkerGlass::Timeout

Defined in:
lib/worker_glass/timeout.rb

Overview

This module provides additional timeout functionality for background processing engine

Examples:

Example usage with Sidekiq - will fail with timeout error after 10 seconds

class Worker
  include Sidekiq::Worker
  prepend WorkerGlass::Timeout

  self.timeout = 10

  def perform(*args)
    SlowService.new.run(*args)
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object

Adds a timeout class attribute to prepended class

Parameters:

  • base (Class)

    base class to which we prepend this module



19
20
21
# File 'lib/worker_glass/timeout.rb', line 19

def self.prepended(base)
  base.class_attribute :timeout
end

Instance Method Details

#perform(*args) ⇒ Object

Executes a business logic with additional timeouts

Parameters:

  • args

    Any arguments that we passed when scheduling a background job

Raises:



26
27
28
29
30
# File 'lib/worker_glass/timeout.rb', line 26

def perform(*args)
  raise Errors::TimeoutNotDefined unless self.class.timeout

  ::Timeout.timeout(self.class.timeout, Errors::TimeoutError) { super }
end