module WorkerGlass::Timeout

This module provides additional timeout functionality for background processing engine @example 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

Public Class Methods

prepended(base) click to toggle source

Adds a timeout class attribute to prepended class @param base [Class] base class to which we prepend this module

# File lib/worker_glass/timeout.rb, line 19
def self.prepended(base)
  base.class_attribute :timeout
end

Public Instance Methods

perform(*args) click to toggle source

Executes a business logic with additional timeouts @param args Any arguments that we passed when scheduling a background job @raise [WorkerGlass::Errors::TimeoutNotDefined] if we didn't define timeout

Calls superclass method
# 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