Class: Spring::Client::Run
Constant Summary collapse
- FORWARDED_SIGNALS =
%w(INT QUIT USR1 USR2 INFO WINCH) & Signal.list.keys
- TIMEOUT =
1
Instance Attribute Summary
Attributes inherited from Command
Instance Method Summary collapse
- #boot_server ⇒ Object
- #call ⇒ Object
- #cold_run ⇒ Object
- #connect_to_application(client) ⇒ Object
- #default_rails_env ⇒ Object
- #forward_signal(sig, pid) ⇒ Object
- #forward_signals(pid) ⇒ Object
- #gem_env ⇒ Object
-
#initialize(args) ⇒ Run
constructor
A new instance of Run.
- #kill(sig, pid) ⇒ Object
- #log(message) ⇒ Object
- #queue_signals ⇒ Object
- #run ⇒ Object
- #run_command(client, application) ⇒ Object
- #send_json(socket, data) ⇒ Object
- #server ⇒ Object
- #stop_server ⇒ Object
- #verify_server_version ⇒ Object
- #warm_run ⇒ Object
Methods inherited from Command
Constructor Details
#initialize(args) ⇒ Run
Returns a new instance of Run.
12 13 14 15 |
# File 'lib/spring/client/run.rb', line 12 def initialize(args) super @signal_queue = [] end |
Instance Method Details
#boot_server ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/spring/client/run.rb', line 66 def boot_server env.socket_path.unlink if env.socket_path.exist? pid = Process.spawn( gem_env, "ruby", "-e", "gem 'spring', '#{Spring::VERSION}'; require 'spring/server'; Spring::Server.boot" ) until env.socket_path.exist? _, status = Process.waitpid2(pid, Process::WNOHANG) exit status.exitstatus if status sleep 0.1 end end |
#call ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/spring/client/run.rb', line 25 def call if env.server_running? warm_run else cold_run end rescue Errno::ECONNRESET exit 1 ensure server.close if @server end |
#cold_run ⇒ Object
51 52 53 54 |
# File 'lib/spring/client/run.rb', line 51 def cold_run boot_server run end |
#connect_to_application(client) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/spring/client/run.rb', line 111 def connect_to_application(client) server.send_io client send_json server, "args" => args, "default_rails_env" => default_rails_env if IO.select([server], [], [], TIMEOUT) server.gets or raise CommandNotFound else raise "Error connecting to Spring server" end end |
#default_rails_env ⇒ Object
193 194 195 |
# File 'lib/spring/client/run.rb', line 193 def default_rails_env ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' end |
#forward_signal(sig, pid) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/spring/client/run.rb', line 173 def forward_signal(sig, pid) kill(sig, pid) rescue Errno::ESRCH # If the application process is gone, then don't block the # signal on this process. trap(sig, 'DEFAULT') Process.kill(sig, Process.pid) end |
#forward_signals(pid) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/spring/client/run.rb', line 164 def forward_signals(pid) @signal_queue.each { |sig| kill sig, pid } FORWARDED_SIGNALS.each do |sig| trap(sig) { forward_signal sig, pid } end rescue Errno::ESRCH end |
#gem_env ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/spring/client/run.rb', line 82 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, pid) ⇒ Object
182 183 184 |
# File 'lib/spring/client/run.rb', line 182 def kill(sig, pid) Process.kill(sig, -Process.getpgid(pid)) end |
#log(message) ⇒ Object
17 18 19 |
# File 'lib/spring/client/run.rb', line 17 def log() env.log "[client] #{}" end |
#queue_signals ⇒ Object
158 159 160 161 162 |
# File 'lib/spring/client/run.rb', line 158 def queue_signals FORWARDED_SIGNALS.each do |sig| trap(sig) { @signal_queue << sig } end end |
#run ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/spring/client/run.rb', line 56 def run verify_server_version application, client = UNIXSocket.pair queue_signals connect_to_application(client) run_command(client, application) end |
#run_command(client, application) ⇒ Object
122 123 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 149 150 151 152 153 154 155 156 |
# File 'lib/spring/client/run.rb', line 122 def run_command(client, application) log "sending command" application.send_io STDOUT application.send_io STDERR application.send_io STDIN 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}" puts "Running via Spring preloader in process #{pid}" unless Spring.quiet forward_signals(pid.to_i) 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
186 187 188 189 190 191 |
# File 'lib/spring/client/run.rb', line 186 def send_json(socket, data) data = JSON.dump(data) socket.puts data.bytesize socket.write data end |
#server ⇒ Object
21 22 23 |
# File 'lib/spring/client/run.rb', line 21 def server @server ||= UNIXSocket.open(env.socket_name) end |
#stop_server ⇒ Object
92 93 94 95 96 |
# File 'lib/spring/client/run.rb', line 92 def stop_server server.close @server = nil env.stop end |
#verify_server_version ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/spring/client/run.rb', line 98 def verify_server_version server_version = server.gets.chomp if server_version != env.version $stderr.puts <<-ERROR There is a version mismatch between the spring client and the server. You should restart the server and make sure to use the same version. CLIENT: #{env.version}, SERVER: #{server_version} ERROR exit 1 end end |
#warm_run ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/spring/client/run.rb', line 37 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 |