Class: Spring::Client::Run
Constant Summary collapse
- FORWARDED_SIGNALS =
%w(INT QUIT USR1 USR2 INFO WINCH) & Signal.list.keys
- CONNECT_TIMEOUT =
1
- BOOT_TIMEOUT =
20
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Attributes inherited from Command
Instance Method Summary collapse
- #boot_server ⇒ Object
- #call ⇒ Object
- #cold_run ⇒ Object
- #connect ⇒ Object
- #connect_to_application(client) ⇒ Object
- #default_rails_env ⇒ Object
- #forward_signal(sig, application) ⇒ Object
- #forward_signals(application) ⇒ Object
- #gem_env ⇒ Object
-
#initialize(args) ⇒ Run
constructor
A new instance of Run.
- #kill(sig, application) ⇒ Object
- #log(message) ⇒ Object
- #queue_signals ⇒ Object
- #reset_env ⇒ Object
- #run ⇒ Object
- #run_command(client, application) ⇒ Object
- #send_json(socket, data) ⇒ Object
- #server_booted? ⇒ Boolean
- #server_process_env ⇒ Object
- #spawn_env ⇒ Object
- #stop_server ⇒ Object
- #suspend_resume_on_tstp_cont(pid) ⇒ Object
- #verify_server_version ⇒ Object
- #warm_run ⇒ Object
Methods inherited from Command
Constructor Details
#initialize(args) ⇒ Run
Returns a new instance of Run.
14 15 16 17 18 19 |
# File 'lib/spring/client/run.rb', line 14 def initialize(args) super @signal_queue = [] @server_booted = false end |
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
12 13 14 |
# File 'lib/spring/client/run.rb', line 12 def server @server end |
Instance Method Details
#boot_server ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/spring/client/run.rb', line 73 def boot_server env.socket_path.unlink if env.socket_path.exist? pid = Process.spawn(server_process_env, env.server_command, out: File::NULL) timeout = Time.now + BOOT_TIMEOUT @server_booted = true until env.socket_path.exist? _, status = Process.waitpid2(pid, Process::WNOHANG) if status exit status.exitstatus elsif Time.now > timeout $stderr.puts "Starting Spring server with `#{env.server_command}` " \ "timed out after #{BOOT_TIMEOUT} seconds" exit 1 end sleep 0.1 end end |
#call ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/spring/client/run.rb', line 29 def call begin connect rescue Errno::ENOENT, Errno::ECONNRESET, Errno::ECONNREFUSED cold_run else warm_run end ensure server.close if server end |
#cold_run ⇒ Object
55 56 57 58 59 |
# File 'lib/spring/client/run.rb', line 55 def cold_run boot_server connect run end |
#connect ⇒ Object
25 26 27 |
# File 'lib/spring/client/run.rb', line 25 def connect @server = UNIXSocket.open(env.socket_name) end |
#connect_to_application(client) ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/spring/client/run.rb', line 150 def connect_to_application(client) server.send_io client send_json server, "args" => args, "default_rails_env" => default_rails_env, "spawn_env" => spawn_env, "reset_env" => reset_env if IO.select([server], [], [], CONNECT_TIMEOUT) server.gets or raise CommandNotFound else raise "Error connecting to Spring server" end end |
#default_rails_env ⇒ Object
249 250 251 |
# File 'lib/spring/client/run.rb', line 249 def default_rails_env ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' end |
#forward_signal(sig, application) ⇒ Object
228 229 230 231 232 233 234 235 |
# File 'lib/spring/client/run.rb', line 228 def forward_signal(sig, application) if kill(sig, application) != 0 # If the application process is gone, then don't block the # signal on this process. trap(sig, 'DEFAULT') Process.kill(sig, Process.pid) end end |
#forward_signals(application) ⇒ Object
220 221 222 223 224 225 226 |
# File 'lib/spring/client/run.rb', line 220 def forward_signals(application) @signal_queue.each { |sig| kill sig, application } FORWARDED_SIGNALS.each do |sig| trap(sig) { forward_signal sig, application } end end |
#gem_env ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/spring/client/run.rb', line 100 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 |
#kill(sig, application) ⇒ Object
237 238 239 240 |
# File 'lib/spring/client/run.rb', line 237 def kill(sig, application) application.puts(sig) application.gets.to_i end |
#log(message) ⇒ Object
21 22 23 |
# File 'lib/spring/client/run.rb', line 21 def log() env.log "[client] #{}" end |
#queue_signals ⇒ Object
202 203 204 205 206 |
# File 'lib/spring/client/run.rb', line 202 def queue_signals FORWARDED_SIGNALS.each do |sig| trap(sig) { @signal_queue << sig } end end |
#reset_env ⇒ Object
110 111 112 |
# File 'lib/spring/client/run.rb', line 110 def reset_env ENV.slice(*Spring.reset_on_env) end |
#run ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/spring/client/run.rb', line 61 def run verify_server_version application, client = UNIXSocket.pair queue_signals connect_to_application(client) run_command(client, application) rescue Errno::ECONNRESET exit 1 end |
#run_command(client, application) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/spring/client/run.rb', line 161 def run_command(client, application) application.send_io STDOUT application.send_io STDERR application.send_io STDIN log "waiting for the application to be preloaded" preload_status = application.gets preload_status = preload_status.chomp if preload_status log "app preload status: #{preload_status}" exit 1 if preload_status == "1" log "sending command" send_json application, "args" => args, "env" => ENV.to_hash 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) status = application.read.to_i log "got exit status #{status}" exit status else log "got no pid" exit 1 end ensure application.close end |
#send_json(socket, data) ⇒ Object
242 243 244 245 246 247 |
# File 'lib/spring/client/run.rb', line 242 def send_json(socket, data) data = JSON.dump(data) socket.puts data.bytesize socket.write data end |
#server_booted? ⇒ Boolean
96 97 98 |
# File 'lib/spring/client/run.rb', line 96 def server_booted? @server_booted end |
#server_process_env ⇒ Object
114 115 116 |
# File 'lib/spring/client/run.rb', line 114 def server_process_env reset_env.merge(gem_env) end |
#spawn_env ⇒ Object
253 254 255 |
# File 'lib/spring/client/run.rb', line 253 def spawn_env ENV.slice(*Spring.spawn_on_env) end |
#stop_server ⇒ Object
118 119 120 121 122 |
# File 'lib/spring/client/run.rb', line 118 def stop_server server.close @server = nil env.stop end |
#suspend_resume_on_tstp_cont(pid) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/spring/client/run.rb', line 208 def suspend_resume_on_tstp_cont(pid) trap("TSTP") { log "suspended" Process.kill("STOP", pid.to_i) Process.kill("STOP", Process.pid) } trap("CONT") { log "resumed" Process.kill("CONT", pid.to_i) } end |
#verify_server_version ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/spring/client/run.rb', line 124 def verify_server_version unless IO.select([server], [], [], CONNECT_TIMEOUT) raise "Error connecting to Spring server" end line = server.gets unless line raise "Error connecting to Spring server" end server_version = line.chomp if server_version != env.version $stderr.puts "There is a version mismatch between the Spring client " \ "(#{env.version}) and the server (#{server_version})." if server_booted? $stderr.puts "We already tried to reboot the server, but the mismatch is still present." exit 1 else $stderr.puts "Restarting to resolve." stop_server cold_run end end end |
#warm_run ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/spring/client/run.rb', line 41 def warm_run run rescue CommandNotFound require "spring/commands" if Spring.command?(args.first) # Command installed since Spring started stop_server cold_run else raise end end |