Class: Expedite::Client::Base
- Inherits:
-
Object
- Object
- Expedite::Client::Base
- Defined in:
- lib/expedite/client/base.rb
Constant Summary collapse
- CONNECT_TIMEOUT =
1- BOOT_TIMEOUT =
20
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #boot_server ⇒ Object
- #call(*args) ⇒ Object
- #connect ⇒ Object
- #connect_to_agent(client, args) ⇒ Object
- #gem_env ⇒ Object
-
#initialize(env: nil, agent: nil) ⇒ Base
constructor
A new instance of Base.
- #log(message) ⇒ Object
- #perform(*args) ⇒ Object
- #run_command(client, agent, args) ⇒ Object
- #server_booted? ⇒ Boolean
- #stop_server ⇒ Object
- #verify_server_version ⇒ Object
Constructor Details
#initialize(env: nil, agent: nil) ⇒ Base
Returns a new instance of Base.
16 17 18 19 20 21 |
# File 'lib/expedite/client/base.rb', line 16 def initialize(env: nil, agent: nil) @env = env || Env.new @agent = agent @server_booted = false end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
13 14 15 |
# File 'lib/expedite/client/base.rb', line 13 def agent @agent end |
#args ⇒ Object (readonly)
Returns the value of attribute args.
13 14 15 |
# File 'lib/expedite/client/base.rb', line 13 def args @args end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
13 14 15 |
# File 'lib/expedite/client/base.rb', line 13 def env @env end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
14 15 16 |
# File 'lib/expedite/client/base.rb', line 14 def server @server end |
Instance Method Details
#boot_server ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/expedite/client/base.rb', line 106 def boot_server env.socket_path.unlink if env.socket_path.exist? pid = Bundler.with_original_env do Process.spawn(gem_env, env.server_command, out: File::NULL, chdir: env.root) end timeout = Time.now + BOOT_TIMEOUT @server_booted = true until env.socket_path.exist? _, status = Process.waitpid2(pid, Process::WNOHANG) if status # Server did not start raise ArgumentError, "Server exited: #{status.exitstatus}" elsif Time.now > timeout raise "Starting Expedite server with `#{env.server_command}` " \ "timed out after #{BOOT_TIMEOUT} seconds. Was waiting for #{env.socket_path} to appear." #exit 1 end sleep 0.1 end end |
#call(*args) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/expedite/client/base.rb', line 23 def call(*args) begin connect rescue Errno::ENOENT, Errno::ECONNRESET, Errno::ECONNREFUSED boot_server connect end perform(*args) ensure server.close if server end |
#connect ⇒ Object
146 147 148 |
# File 'lib/expedite/client/base.rb', line 146 def connect @server = UNIXSocket.open(env.socket_path) end |
#connect_to_agent(client, args) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/expedite/client/base.rb', line 49 def connect_to_agent(client, args) server.send_io client server.send_object({"args" => args, "agent" => agent}, env) if IO.select([server], [], [], CONNECT_TIMEOUT) server.gets or raise CommandNotFound else raise "Error connecting to Expedite server" end end |
#gem_env ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/expedite/client/base.rb', line 150 def gem_env bundle = Bundler.bundle_path.to_s paths = Gem.path + ENV["GEM_PATH"].to_s.split(File::PATH_SEPARATOR) { "GEM_PATH" => [bundle, *paths].uniq.join(File::PATH_SEPARATOR), "GEM_HOME" => bundle } end |
#log(message) ⇒ Object
142 143 144 |
# File 'lib/expedite/client/base.rb', line 142 def log() env.log "[client] #{message}" end |
#perform(*args) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/expedite/client/base.rb', line 36 def perform(*args) verify_server_version agent, client = UNIXSocket.pair connect_to_agent(client, args) run_command(client, agent, args) end |
#run_command(client, agent, args) ⇒ Object
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 102 103 104 |
# File 'lib/expedite/client/base.rb', line 61 def run_command(client, agent, args) @null_socket ||= File.open(File::NULL, "a") log "sending command" # No child socket agent.send_setup(@null_socket, env) agent.send_object({ "args" => args, "env" => ENV.to_hash, "method" => run_command_method, }, env) pid = server.gets pid = pid.chomp if pid # We must not close the client socket until we are sure that the application has # received the FD. Otherwise the FD can end up getting closed while it's in the server # socket buffer on OS X. This doesn't happen on Linux. client.close if pid && !pid.empty? log "got pid: #{pid}" ## suspend_resume_on_tstp_cont(pid) ## forward_signals(application) result = agent.recv_object(env) if result.key?("exception") e = result["exception"] log "got exception #{e}" raise e end ret = result["return"] log "got return value #{ret}" return ret else log "got no pid" raise UnknownError, "got no pid" end ensure agent.close end |
#server_booted? ⇒ Boolean
132 133 134 |
# File 'lib/expedite/client/base.rb', line 132 def server_booted? @server_booted end |
#stop_server ⇒ Object
136 137 138 139 140 |
# File 'lib/expedite/client/base.rb', line 136 def stop_server server.close @server = nil env.stop end |
#verify_server_version ⇒ Object
44 45 46 47 |
# File 'lib/expedite/client/base.rb', line 44 def verify_server_version server_version = server.gets.chomp raise ArgumentError, "Server mismatch. Expected #{env.version}, got #{server_version}." if server_version != env.version end |