Class: Pacproxy::Runtimes::Node
- Includes:
- Loggable
- Defined in:
- lib/pacproxy/runtimes/node/node.rb
Overview
Pacproxy::Runtimes::Node represet node js runtime
Constant Summary collapse
- TIMEOUT_JS_CALL =
0.5
- TIMEOUT_JS_SERVER =
5
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #find(url) ⇒ Object
-
#initialize ⇒ Node
constructor
A new instance of Node.
- #shutdown ⇒ Object
- #update(file_location) ⇒ Object
Methods included from Loggable
#access_logger, #accesslog, #debug, #error, #fatal, #general_logger, #info, #lwarn
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
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 57 58 59 |
# File 'lib/pacproxy/runtimes/node/node.rb', line 29 def initialize js = File.join(File.dirname(__FILE__), 'find.js') retries = 3 begin Timeout.timeout(TIMEOUT_JS_SERVER) do server = TCPServer.new('127.0.0.1', 0) @port = server.addr[1] server.close if OS.windows? @server_pid = start_server else @server_pid = fork { exec('node', js, @port.to_s) } Process.detach(@server_pid) end sleep 0.01 until port_open? initialize_client end rescue Timeout::Error shutdown if retries > 0 retries -= 1 lwarn('Timeout. Initialize Node.js server.') retry else error('Gave up to retry Initialize Node.js server.') raise 'Gave up to retry Initialize Node.js server.' end end end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
18 19 20 |
# File 'lib/pacproxy/runtimes/node/node.rb', line 18 def source @source end |
Class Method Details
Instance Method Details
#find(url) ⇒ Object
76 77 78 79 80 |
# File 'lib/pacproxy/runtimes/node/node.rb', line 76 def find(url) return 'DIRECT' unless @source uri = URI.parse(url) call_find(uri) end |
#shutdown ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/pacproxy/runtimes/node/node.rb', line 61 def shutdown @client_thread.kill if @client_thread if OS.windows? stop_server(@server_pid) else Process.kill(:INT, @server_pid) end end |
#update(file_location) ⇒ Object
70 71 72 73 74 |
# File 'lib/pacproxy/runtimes/node/node.rb', line 70 def update(file_location) @source = open(file_location, proxy: false).read rescue @source = nil end |