Class: Expedite::Server::Controller
- Inherits:
-
Object
- Object
- Expedite::Server::Controller
show all
- Includes:
- Expedite::Signals
- Defined in:
- lib/expedite/server/controller.rb
Overview
Controls the ‘expedite server`.
Constant Summary
Expedite::Signals::IGNORE_SIGNALS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(foreground: true, env: nil) ⇒ Controller
Returns a new instance of Controller.
22
23
24
25
26
27
|
# File 'lib/expedite/server/controller.rb', line 22
def initialize(foreground: true, env: nil)
@foreground = foreground
@env = env || default_env
@pidfile = @env.pidfile_path.open('a')
@mutex = Mutex.new
end
|
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
20
21
22
|
# File 'lib/expedite/server/controller.rb', line 20
def env
@env
end
|
Class Method Details
.boot(options = {}) ⇒ Object
16
17
18
|
# File 'lib/expedite/server/controller.rb', line 16
def self.boot(options = {})
new(options).boot
end
|
Instance Method Details
#boot ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/expedite/server/controller.rb', line 37
def boot
env.load_helper
write_pidfile
set_pgid unless foreground?
ignore_signals unless foreground?
set_exit_hook
set_process_title
start_server
exit 0
end
|
#foreground? ⇒ Boolean
29
30
31
|
# File 'lib/expedite/server/controller.rb', line 29
def foreground?
@foreground
end
|
#ignore_signals ⇒ Object
Ignore SIGINT and SIGQUIT otherwise the user typing ^C or ^\ on the command line will kill the server/application.
177
178
179
|
# File 'lib/expedite/server/controller.rb', line 177
def ignore_signals
IGNORE_SIGNALS.each { |sig| trap(sig, "IGNORE") }
end
|
#kill(sig) ⇒ Object
85
86
87
88
89
90
|
# File 'lib/expedite/server/controller.rb', line 85
def kill(sig)
pid = self.pid
Process.kill(sig, pid) if pid
rescue Errno::ESRCH
end
|
#log(message) ⇒ Object
33
34
35
|
# File 'lib/expedite/server/controller.rb', line 33
def log(message)
env.log "[server] #{message}"
end
|
#pid ⇒ Object
49
50
51
52
53
|
# File 'lib/expedite/server/controller.rb', line 49
def pid
@env.pidfile_path.read.to_i
rescue Errno::ENOENT
nil
end
|
#redirect_output ⇒ Object
We need to redirect STDOUT and STDERR, otherwise the server will keep the original FDs open which would break piping. (e.g. ‘spring rake -T | grep db` would hang forever because the server would keep the stdout FD open.)
220
221
222
|
# File 'lib/expedite/server/controller.rb', line 220
def redirect_output
[STDOUT, STDERR].each { |stream| stream.reopen(env.log_file) }
end
|
#running? ⇒ Boolean
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/expedite/server/controller.rb', line 55
def running?
pidfile = @env.pidfile_path.open('r+')
!pidfile.flock(File::LOCK_EX | File::LOCK_NB)
rescue Errno::ENOENT
false
ensure
if pidfile
pidfile.flock(File::LOCK_UN)
pidfile.close
end
end
|
#serve(client) ⇒ Object
99
100
101
102
103
104
105
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
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
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/expedite/server/controller.rb', line 99
def serve(client)
log "accepted client"
client.puts env.version
app_client = client.recv_io
command = client.recv_object(env)
args, agent = command.values_at("args", "agent")
cmd = args.first
if agent == "__server__"
case cmd
when "application_pids"
client.puts
unix_socket = UNIXSocket.for_fd(app_client.fileno)
_ = unix_socket.recv_setup(env)
client.puts Process.pid
application_pids = []
env.applications.pools.each do |k, pool|
application_pids.concat(pool.all.map(&:pid))
end
unix_socket.send_return(application_pids, env)
unix_socket.close
client.close
else
end
elsif Expedite::Actions.lookup(cmd)
log "running command #{cmd}: #{args}"
client.puts
begin
env.applications.with(agent) do |target|
client.puts target.run(app_client)
end
rescue AgentNotFoundError => e
unix_socket = UNIXSocket.for_fd(app_client.fileno)
_ = unix_socket.recv_setup(env)
args, env = unix_socket.recv_object(env).values_at("args", "env")
client.puts Process.pid
unix_socket.send_exception(e, env)
unix_socket.close
client.close
end
else
log "command not found #{cmd}"
client.close
end
rescue AgentNotFoundError => e
rescue SocketError => e
raise e unless client.eof?
ensure
redirect_output
end
|
#set_exit_hook ⇒ Object
181
182
183
184
185
186
|
# File 'lib/expedite/server/controller.rb', line 181
def set_exit_hook
server_pid = Process.pid
at_exit { shutdown if Process.pid == server_pid }
end
|
#set_pgid ⇒ Object
Boot the server into the process group of the current session. This will cause it to be automatically killed once the session ends (i.e. when the user closes their terminal).
171
172
173
|
# File 'lib/expedite/server/controller.rb', line 171
def set_pgid
end
|
#set_process_title ⇒ Object
224
225
226
|
# File 'lib/expedite/server/controller.rb', line 224
def set_process_title
$0 = "expedite server | #{env.app_name}"
end
|
#shutdown ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/expedite/server/controller.rb', line 188
def shutdown
log "shutting down"
[env.socket_path, env.pidfile_path].each do |path|
if path.exist?
path.unlink rescue nil
end
end
thrs = []
env.applications.pools.each do |k, pool|
pool.all.each do |a|
thrs << Expedite.failsafe_thread { a.stop }
end
end
thrs.map(&:join)
end
|
#start_server ⇒ Object
92
93
94
95
96
97
|
# File 'lib/expedite/server/controller.rb', line 92
def start_server
server = UNIXServer.open(env.socket_path)
log "started on #{env.socket_path}"
loop { serve server.accept }
rescue Interrupt
end
|
#stop ⇒ Object
timeout: Defaults to 2 seconds
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/expedite/server/controller.rb', line 68
def stop
if running?
timeout = Time.now + @env.graceful_termination_timeout
kill 'TERM'
sleep 0.1 until !running? || Time.now >= timeout
if running?
kill 'KILL'
:killed
else
:stopped
end
else
:not_running
end
end
|
#write_pidfile ⇒ Object
206
207
208
209
210
211
212
213
214
|
# File 'lib/expedite/server/controller.rb', line 206
def write_pidfile
if @pidfile.flock(File::LOCK_EX | File::LOCK_NB)
@pidfile.truncate(0)
@pidfile.write("#{Process.pid}\n")
@pidfile.fsync
else
raise "Failed to lock #{@env.pidfile_path}"
end
end
|