Class: Thread

Inherits:
Object
  • Object
show all
Defined in:
lib/workety/extensions/thread.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.log(message = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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_exitObject



57
58
59
60
61
# File 'lib/workety/extensions/thread.rb', line 57

def self.rescue_exit
  new do
    Kernel.rescue_exit { yield }
  end
end

.workety(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#backtrace_viewObject



83
84
85
# 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) ⇒ Object



103
104
105
106
# File 'lib/workety/extensions/thread.rb', line 103

def log_join(message, limit = nil)
  join limit
  Rails.logger.info message
end

#status_viewObject



72
73
74
# File 'lib/workety/extensions/thread.rb', line 72

def status_view
  st = status; (st == false) ? "terminated normally" : (st || "terminated with an exception")
end

#summary_viewObject



68
69
70
# 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_viewObject



76
77
78
79
80
81
# 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

#viewObject



64
65
66
# File 'lib/workety/extensions/thread.rb', line 64

def view
  "#{summary_view}\n#{thread_local_vars_view}#{backtrace_view}"
end