Class: Spring::Client::Run
Constant Summary
collapse
- FORWARDED_SIGNALS =
%w(INT QUIT USR1 USR2 INFO) & Signal.list.keys
Instance Attribute Summary
Attributes inherited from Command
#args, #env
Instance Method Summary
collapse
Methods inherited from Command
call, #initialize
Instance Method Details
#boot_server ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/spring/client/run.rb', line 31
def boot_server
env.socket_path.unlink if env.socket_path.exist?
pid = fork {
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
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/spring/client/run.rb', line 17
def call
boot_server unless env.server_running?
verify_server_version
application, client = UNIXSocket.pair
connect_to_application(client)
run_command(client, application)
rescue Errno::ECONNRESET
exit 1
ensure
server.close if @server
end
|
#connect_to_application(client) ⇒ Object
59
60
61
62
63
|
# File 'lib/spring/client/run.rb', line 59
def connect_to_application(client)
server.send_io client
send_json server, "args" => args, "default_rails_env" => default_rails_env
server.gets or raise CommandNotFound
end
|
#default_rails_env ⇒ Object
121
122
123
|
# File 'lib/spring/client/run.rb', line 121
def default_rails_env
ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
|
#forward_signal(sig, pid) ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/spring/client/run.rb', line 105
def forward_signal(sig, pid)
Process.kill(sig, -Process.getpgid(pid))
rescue Errno::ESRCH
trap(sig, 'DEFAULT')
Process.kill(sig, Process.pid)
end
|
#forward_signals(pid) ⇒ Object
99
100
101
102
103
|
# File 'lib/spring/client/run.rb', line 99
def forward_signals(pid)
FORWARDED_SIGNALS.each do |sig|
trap(sig) { forward_signal sig, pid }
end
end
|
#log(message) ⇒ Object
9
10
11
|
# File 'lib/spring/client/run.rb', line 9
def log(message)
env.log "[client] #{message}"
end
|
#run_command(client, application) ⇒ Object
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
|
# File 'lib/spring/client/run.rb', line 65
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
client.close
if pid && !pid.empty?
log "got pid: #{pid}"
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
114
115
116
117
118
119
|
# File 'lib/spring/client/run.rb', line 114
def send_json(socket, data)
data = JSON.dump(data)
socket.puts data.bytesize
socket.write data
end
|
#server ⇒ Object
13
14
15
|
# File 'lib/spring/client/run.rb', line 13
def server
@server ||= UNIXSocket.open(env.socket_name)
end
|
#verify_server_version ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/spring/client/run.rb', line 46
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
|