class Thread

Public Class Methods

log(message = nil) click to toggle source
# File lib/workety/extensions/thread.rb, line 88
def self.log message = nil
  threads = self.list
  Rails.logger.warn(message) if message
  Rails.logger.warn "Thread list: #{threads.length} threads total at #{Time.now}"
  
  threads.each_with_index do |item, index|
    Rails.logger.warn msg = "Thread #{index + 1} of #{threads.length}"
    Rails.logger.warn "-" * msg.length
    Rails.logger.warn item.view
  end
  
  Rails.logger.flush if Rails.logger.respond_to?(:flush)
end
networkety(*args) { |*args| ... } click to toggle source
# File lib/workety/extensions/thread.rb, line 39
def self.networkety(*args)
  workety do
    begin
      yield(*args)
    rescue *(Socket::NETWORK_EXEPTIONS) => exception
      Rails.logger.warn "Thread stopped due a network error listed in Socket::NETWORK_EXEPTIONS"
      Rails.logger.warn exception.view
      Rails.logger.flush if Rails.logger.respond_to?(:flush)

      # If thread is blocked by Socket#read and then are forced to unblock by using Socket#shutdown and then Socket#close methods,
      # that will raise an exception. That exception has no value to set Workety.aborted? flag.
      # If Workety.must_stop? is set we suggest that such technique was used.
      Workety.abort unless Workety.must_stop? 
    end
  end
end
rescue_exit() { || ... } click to toggle source
# File lib/workety/extensions/thread.rb, line 57
def self.rescue_exit
  new do
    Kernel.rescue_exit { yield }
  end
end
workety(*args) { |*args| ... } click to toggle source
# File lib/workety/extensions/thread.rb, line 21
def self.workety(*args)
  new do
    begin
      Workety.rescue_abort { yield(*args) }
      
    ensure
      # That block is executed on Thread#kill as well
      #
      # I think an error in clear_active_connections! is not the case
      # for panic and instant process shutdown but for normal shutdown procedure.
      #
      Workety.rescue_abort { ActiveRecord::Base.clear_active_connections! }
       
    end
  end
end

Public Instance Methods

backtrace_view() click to toggle source
# File lib/workety/extensions/thread.rb, line 83
def backtrace_view
  ((bt = backtrace) && bt.collect{|line| "\t#{line}\n"}.join("") || "\tBacktrace undefined")
end
log_join(message, limit = nil) click to toggle source
# File lib/workety/extensions/thread.rb, line 103
def log_join(message, limit = nil)
  join limit
  Rails.logger.info message
end
status_view() click to toggle source
# File lib/workety/extensions/thread.rb, line 72
def status_view
  st = status; (st == false) ? "terminated normally" : (st || "terminated with an exception")
end
summary_view() click to toggle source
# File lib/workety/extensions/thread.rb, line 68
def summary_view
  "#{self.class.name} 0x#{object_id.to_s(16)}: #{status_view}"
end
thread_local_vars_view() click to toggle source
# File lib/workety/extensions/thread.rb, line 76
def thread_local_vars_view
  if (ks = keys).any?
    vars = {}; ks.each {|k| vars[k] = self[k]}
    "Thread-local variables: #{vars.inspect}\n"
  end
end
view() click to toggle source
# File lib/workety/extensions/thread.rb, line 64
def view
  "#{summary_view}\n#{thread_local_vars_view}#{backtrace_view}"
end