Class: DangoTesterClient

Inherits:
Object show all
Defined in:
lib/dango/tester/dango_tester_client.rb

Overview

テスタークラス

Constant Summary collapse

ConnectionRetryTimes =

接続リトライ回数

10
ConnectionRetryIntervalSec =

接続リトライ時の間隔病数

3
GCIntervalSec =

GCの発生タイミング

5.0
ReceiveCacheAutoDeleteSec =

receive_cacheを自動で消すタイミング

30.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DangoTesterClient

テスターのイニシャライズ



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dango/tester/dango_tester_client.rb', line 19

def initialize(options = {})
  options = {:debug => true} if options.class != Hash
  
  @debug = options[:debug]
  
  original_print("Dango Version=#{Dango::VERSION::STRING}\n") if @debug
  
  @client_conns = {}
  Thread.abort_on_exception = true
  
  # GC発生タイミングを決定
  @gc_interval_sec = GCIntervalSec
  gc_thread_start()                     # GCスレッドの開始
  
  def self.die(str)
    original_print "============ die ============ #{Time.now_to_s}\n"
    original_print caller().pretty_inspect + "\n"
    original_print str.to_s + "\n"
    exit 1
  end
  
#    def self.print(str)
#      original_print("#{str}")
#    end
  
  def self.puts(str)
    original_print("#{str}\n")
  end
  
  def self.p(obj)
    original_print("#{obj.inspect}\n")
  end
  
  def self.pp(obj)
    original_print("#{obj.pretty_inspect}\n")
  end

end

Instance Attribute Details

#client_connsObject (readonly)

Returns the value of attribute client_conns.



103
104
105
# File 'lib/dango/tester/dango_tester_client.rb', line 103

def client_conns
  @client_conns
end

Class Method Details

.die(str) ⇒ Object



33
34
35
36
37
38
# File 'lib/dango/tester/dango_tester_client.rb', line 33

def self.die(str)
  original_print "============ die ============ #{Time.now_to_s}\n"
  original_print caller().pretty_inspect + "\n"
  original_print str.to_s + "\n"
  exit 1
end

.p(obj) ⇒ Object



48
49
50
# File 'lib/dango/tester/dango_tester_client.rb', line 48

def self.p(obj)
  original_print("#{obj.inspect}\n")
end

.pp(obj) ⇒ Object



52
53
54
# File 'lib/dango/tester/dango_tester_client.rb', line 52

def self.pp(obj)
  original_print("#{obj.pretty_inspect}\n")
end

.puts(str) ⇒ Object

def self.print(str)

  original_print("#{str}")
end


44
45
46
# File 'lib/dango/tester/dango_tester_client.rb', line 44

def self.puts(str)
  original_print("#{str}\n")
end

Instance Method Details

#gc_thread_startObject

GCスレッドの開始



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dango/tester/dango_tester_client.rb', line 123

def gc_thread_start
  gc_thread = Thread.start do
    loop do
      begin
        sleep @gc_interval_sec
        
        gc_start_time = Time.now
        GC.enable
        GC.start 
        GC.disable
        puts "GC #{Time.now - gc_start_time}sec #{Time.now_to_s}" if @debug
        
      rescue
        puts "Exception gc_thread_start #{Time.now_to_s} #{error_message($!, 'u')}"
      end
    end
  end
  gc_thread.priority = -1
  
  @gc_thread = gc_thread
end

#gc_thread_stopObject

GCスレッドの終了



146
147
148
# File 'lib/dango/tester/dango_tester_client.rb', line 146

def gc_thread_stop
  @gc_thread.kill
end

#new_client(c_name, serv_info) ⇒ Object

クライアントを1個接続



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dango/tester/dango_tester_client.rb', line 59

def new_client(c_name, serv_info)
  config = {
    "network" => {
      "host"=>serv_info["host"],
      "port"=>serv_info["port"],
    }, 
    "tester" => {
      "receive_cache_auto_delete_sec" => serv_info["receive_cache_auto_delete_sec"] || ReceiveCacheAutoDeleteSec,
    },
    "log" => {
      "log_file"      => serv_info["log_file"] || "log/tester_#{Process.pid}_#{c_name}.log",
      "log_level"     => serv_info["log_level"] || Logger::INFO,
      "log_max_size"  => serv_info["log_max_size"] || 10000000,
      "log_shift_age" => serv_info["log_shift_age"] || 99,
    },
  }
  env = ENV['RAILS_ENV'] || 'development'
  
  # 接続
  client_conn = nil
  ConnectionRetryTimes.times do |i|
    begin
      client_conn = TestClient.new(env, config, c_name)
      break
    rescue
      client_conn = nil
      raise("connection error for retry times over.") if i == ConnectionRetryTimes - 1
      puts "#{$!.class} #{$!.message} #{$!.backtrace.inspect}" if @debug
      puts "connection failed. sleep #{ConnectionRetryIntervalSec}. #{$!.class}"
      sleep ConnectionRetryIntervalSec
    end
  end
  raise("connection error.") if ! client_conn
  
  # client_nameメソッド作成とclient_nameを定義
  client_conn.client_name = c_name
  
  # 接続を入れて
  @client_conns[c_name] = client_conn
  
  # 接続インスタンスを返す
  client_conn
end

#original_print(str) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dango/tester/dango_tester_client.rb', line 105

def original_print(str)
  disp_str = ""
  if ENV['LANG'] =~ /euc/i || ENV['LANG'] =~ /ujis/i
    disp_str = str.to_s.toeuc
  elsif ENV['LANG'] =~ /sjis/i
    disp_str = str.to_s.tosjis
  elsif ENV['LANG'] =~ /UTF\-8/i
    disp_str = str.to_s.toutf8
  elsif RUBY_PLATFORM == 'i386-mswin32' || RUBY_PLATFORM == 'i386-cygwin'
    disp_str = str.to_s.tosjis
  else
    disp_str = str.to_s
  end
  
  print(disp_str)
end