Class: RubyProxy::DRbClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_proxy/client.rb

Constant Summary collapse

@@logger =
Logger.new(STDOUT)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ipObject

Returns the value of attribute ip.



162
163
164
# File 'lib/ruby_proxy/client.rb', line 162

def ip
  @ip
end

.portObject

Returns the value of attribute port.



162
163
164
# File 'lib/ruby_proxy/client.rb', line 162

def port
  @port
end

Class Method Details

.alive?Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
# File 'lib/ruby_proxy/client.rb', line 155

def alive?
  @client.respond_to?("any_thing")
  @client
rescue DRb::DRbConnError
  raise
end

.clientObject



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby_proxy/client.rb', line 142

def client
  begin
    stop_service if @client.nil?
    @client ||= DRbObject.new(nil,"druby://#{@ip}:#{@port}")
    alive?
    @client
  rescue Exception
    start_service
    @client ||= DRbObject.new(nil,"druby://#{@ip}:#{@port}")
    @client
  end
end

.proxy_load(dir_or_file) ⇒ Object



164
165
166
# File 'lib/ruby_proxy/client.rb', line 164

def proxy_load(dir_or_file)
  client.proxy_load(dir_or_file)
end

.start_service(t = 5) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ruby_proxy/client.rb', line 168

def start_service(t=5)
  message = nil
  @start_service_log_path = File.join(File.dirname(__FILE__),'start_service.log')
  
  server_thread =  Thread.new do
      @@logger.info "start jruby proxy server..."
      org_path = Dir.pwd
      Dir.chdir(File.join(File.dirname(__FILE__),'..')) do
      # we can start jruby proxy or ruby
      # just change here command
			temp = ENV["RUBYOPT"]
      ENV["RUBYOPT"] = "-rubygems"
      system("start /I /B jruby -J-Dfile.encoding=UTF-8 ruby_proxy/server.rb #{@ip} #{@port} \"#{org_path}\"  ") #> #{@start_service_log_path} 2>&1")
      ENV["RUBYOPT"] = temp
			end
  end
  wait_until_server_start_time(t)
end

.stop_service(t = 5) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ruby_proxy/client.rb', line 187

def stop_service(t=5)
  TCPSocket.new(@ip,@port)
  @client ||= DRbObject.new(nil,"druby://#{@ip}:#{@port}")
  @client.stop_proxy
  sleep 1
rescue
  @@logger.debug "service not start,stop fail!"
  @@logger.debug "#{$!}"
ensure
  @client = nil
end

.wait_until_server_start_time(t) ⇒ Object

Raises:

  • (RuntimeError)


199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ruby_proxy/client.rb', line 199

def wait_until_server_start_time(t)
  t.times do |tt|
    begin
      TCPSocket.new(@ip,@port)
      @@logger.info "server is starting"
      return true
    rescue Exception
      sleep 1
    end
  end
  raise RuntimeError,"start drbserver fail, reason: #{File.read(@start_service_log_path) rescue nil}"
end