railwatch-sidecar 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +105 -0
- data/NOTICE +25 -0
- data/README.md +447 -0
- data/app/controllers/railwatch/sidecar/proxy_controller.rb +363 -0
- data/lib/puma/plugin/railwatch_server.rb +12 -0
- data/lib/puma/plugin/railwatch_sidecar.rb +110 -0
- data/lib/railwatch/server.rb +5 -0
- data/lib/railwatch/sidecar/binary.rb +46 -0
- data/lib/railwatch/sidecar/configuration.rb +150 -0
- data/lib/railwatch/sidecar/data_dir.rb +155 -0
- data/lib/railwatch/sidecar/engine.rb +58 -0
- data/lib/railwatch/sidecar/process.rb +372 -0
- data/lib/railwatch/sidecar/proxy_auth.rb +33 -0
- data/lib/railwatch/sidecar/proxy_handoff.rb +55 -0
- data/lib/railwatch/sidecar/relay.rb +286 -0
- data/lib/railwatch/sidecar/settings.rb +178 -0
- data/lib/railwatch/sidecar/version.rb +10 -0
- data/lib/railwatch/sidecar.rb +172 -0
- data/lib/tasks/railwatch_sidecar.rake +43 -0
- metadata +101 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "socket"
|
|
5
|
+
|
|
6
|
+
module Railwatch
|
|
7
|
+
module Sidecar
|
|
8
|
+
# /railwatch/* in the app, forwarded to the bundled server on loopback.
|
|
9
|
+
#
|
|
10
|
+
# The gate is the railwatch gem's, so one setting covers the dashboard
|
|
11
|
+
# whichever gem serves it: HTTP Basic, on and closed by default
|
|
12
|
+
# (Railwatch.config.http_basic_auth_*), or the app's own controller
|
|
13
|
+
# (Railwatch.config.base_controller_class, with Basic off), whose
|
|
14
|
+
# before_actions run first. Past the gate, each request carries a signed,
|
|
15
|
+
# 60-second X-Railwatch-Proxy-Auth naming the visitor, which the server
|
|
16
|
+
# trusts (railwatch-rs trusted proxy auth); nobody signs in twice.
|
|
17
|
+
#
|
|
18
|
+
# The server is served under the same path (RAILWATCH_BASE_PATH), so
|
|
19
|
+
# paths, redirects, cookies, asset URLs and the live channel pass through
|
|
20
|
+
# unrewritten. CSRF is the server's: it checks Origin and its own token
|
|
21
|
+
# on every write, so Rails' check is skipped here.
|
|
22
|
+
#
|
|
23
|
+
# No Puma thread waits for the server. Past the gate, the request (which
|
|
24
|
+
# Puma has read in full) is left to ProxyHandoff, which takes the socket
|
|
25
|
+
# from Puma and gives it to the process's one Relay thread; the Relay
|
|
26
|
+
# writes the request to the server and the response back, then closes
|
|
27
|
+
# the connection (Connection: close: the browser, or the proxy in front
|
|
28
|
+
# of the app, opens a new one for the next request). Where the socket
|
|
29
|
+
# can't be taken (a server without Rack hijack, Puma's own TLS) the
|
|
30
|
+
# response is streamed from this thread instead (#stream).
|
|
31
|
+
class ProxyController < Railwatch.config.base_controller_class.constantize
|
|
32
|
+
# One upstream request whose response body is read as Puma writes it.
|
|
33
|
+
# Net::HTTP reads a body only inside the block of #request; a Fiber
|
|
34
|
+
# runs that block, handing back the response first and then each
|
|
35
|
+
# chunk, so headers can be returned before the body is read. Puma
|
|
36
|
+
# iterates the body on the thread that called the controller, which
|
|
37
|
+
# is the thread the Fiber was made on.
|
|
38
|
+
class Upstream
|
|
39
|
+
def initialize(request, headers, port:)
|
|
40
|
+
@http = Net::HTTP.new("127.0.0.1", port)
|
|
41
|
+
@http.open_timeout = Railwatch::Sidecar.config.proxy_connect_timeout
|
|
42
|
+
@http.read_timeout = Railwatch::Sidecar.config.proxy_first_byte_timeout
|
|
43
|
+
@http.max_retries = 0
|
|
44
|
+
# Byte-transparent: the browser's Accept-Encoding, or identity. A
|
|
45
|
+
# request built without one gets Net::HTTP's gzip default and its
|
|
46
|
+
# body decoded under the server's Content-Length.
|
|
47
|
+
headers = headers.merge("Accept-Encoding" => request.headers["Accept-Encoding"].presence || "identity")
|
|
48
|
+
@upstream = Net::HTTPGenericRequest.new(request.request_method, !request.content_length.to_i.zero? || chunked_input?(request),
|
|
49
|
+
!request.head?, request.fullpath, headers)
|
|
50
|
+
return unless @upstream.request_body_permitted?
|
|
51
|
+
|
|
52
|
+
input = request.body
|
|
53
|
+
input.rewind if input.respond_to?(:rewind)
|
|
54
|
+
@upstream.body_stream = input
|
|
55
|
+
if request.content_length
|
|
56
|
+
@upstream["Content-Length"] = request.content_length.to_s
|
|
57
|
+
else
|
|
58
|
+
@upstream["Transfer-Encoding"] = "chunked"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def start
|
|
63
|
+
@fiber = Fiber.new do
|
|
64
|
+
@http.start
|
|
65
|
+
@http.request(@upstream) do |response|
|
|
66
|
+
Fiber.yield response
|
|
67
|
+
response.read_body { |chunk| Fiber.yield chunk }
|
|
68
|
+
end
|
|
69
|
+
nil
|
|
70
|
+
ensure
|
|
71
|
+
@http.finish if @http.started?
|
|
72
|
+
end
|
|
73
|
+
@fiber.resume
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def each
|
|
77
|
+
while (chunk = @fiber.resume)
|
|
78
|
+
yield chunk
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Called by Rack when Puma is done with the body, read to the end or
|
|
83
|
+
# not (the browser went away): the connection is closed either way.
|
|
84
|
+
def close
|
|
85
|
+
@http.finish if @http.started?
|
|
86
|
+
rescue IOError
|
|
87
|
+
nil
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def chunked_input?(request)
|
|
93
|
+
request.get_header("HTTP_TRANSFER_ENCODING").to_s.include?("chunked")
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The server's cookies only; the app's own (its session) stay here.
|
|
98
|
+
FORWARDED_COOKIES = %w[_railwatch_session session_token account_id XSRF-TOKEN].freeze
|
|
99
|
+
FORWARDED_HEADERS = %w[
|
|
100
|
+
Accept Accept-Encoding Accept-Language Content-Type Origin Referer User-Agent
|
|
101
|
+
X-Inertia X-Inertia-Version X-Inertia-Partial-Component X-Inertia-Partial-Data X-Inertia-Partial-Except
|
|
102
|
+
X-Inertia-Reset X-Inertia-Error-Bag X-Inertia-Except-Once-Props X-Requested-With X-XSRF-TOKEN X-CSRF-Token
|
|
103
|
+
Purpose Sec-Purpose Sec-Fetch-Site Sec-Fetch-Mode Sec-Fetch-Dest If-None-Match If-Modified-Since
|
|
104
|
+
].freeze
|
|
105
|
+
HOP_BY_HOP = %w[connection keep-alive transfer-encoding content-length te trailer upgrade proxy-connection].freeze
|
|
106
|
+
# Host names that only reach this machine (agent_access?).
|
|
107
|
+
LOCAL_HOSTS = %w[localhost 127.0.0.1 ::1 [::1]].freeze
|
|
108
|
+
|
|
109
|
+
skip_forgery_protection if respond_to?(:skip_forgery_protection)
|
|
110
|
+
before_action :authenticate_by_http_basic
|
|
111
|
+
|
|
112
|
+
# An app on inertia_rails has its controller module in every
|
|
113
|
+
# ActionController::Base, this one included, and two of its behaviours
|
|
114
|
+
# break the dashboard, whose Inertia protocol is the server's end to
|
|
115
|
+
# end. Its middleware compares the dashboard's X-Inertia-Version with
|
|
116
|
+
# the app's and answers 409 to every Inertia visit, so each click was
|
|
117
|
+
# a full page load. Once this controller has answered (forwarded, or
|
|
118
|
+
# refused at the gate), the request's X-Inertia is removed, so the
|
|
119
|
+
# middleware, on its way out, sees no Inertia visit (every
|
|
120
|
+
# inertia_rails version checks that header first). Its after_action
|
|
121
|
+
# wrote the app's own XSRF-TOKEN (Path=/) on every response, so the
|
|
122
|
+
# browser held two and sent the app's to the server, which refused
|
|
123
|
+
# every write; forgery protection is off here (the server checks its
|
|
124
|
+
# own token), and that after_action skips a controller without it.
|
|
125
|
+
def process_action(...)
|
|
126
|
+
super
|
|
127
|
+
ensure
|
|
128
|
+
request.delete_header("HTTP_X_INERTIA")
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def protect_against_forgery? = false
|
|
132
|
+
|
|
133
|
+
def forward
|
|
134
|
+
if (error = Railwatch::Sidecar.provision_error) && !Railwatch::Sidecar.data_dir.provisioned?
|
|
135
|
+
return unavailable("not provisioned: #{error.message}")
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
secret = Railwatch::Sidecar.data_dir.proxy_secret
|
|
139
|
+
return unavailable("not provisioned yet (no #{Railwatch::Sidecar.data_dir.proxy_secret_file})") unless secret
|
|
140
|
+
|
|
141
|
+
if websocket?
|
|
142
|
+
tunnel(secret)
|
|
143
|
+
elsif hijackable?
|
|
144
|
+
hand_off(secret)
|
|
145
|
+
else
|
|
146
|
+
stream(secret)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
private
|
|
151
|
+
|
|
152
|
+
# An agent's Bearer token takes the one Authorization header Basic would
|
|
153
|
+
# need, so a request to MCP or the API with one is left to the server,
|
|
154
|
+
# which checks the token (401 without a valid one) as a standalone
|
|
155
|
+
# railwatch-rs does. The app's own before_actions (base_controller_class)
|
|
156
|
+
# still run.
|
|
157
|
+
def authenticate_by_http_basic
|
|
158
|
+
config = Railwatch.config
|
|
159
|
+
return unless config.http_basic_auth_enabled
|
|
160
|
+
return if agent_bearer
|
|
161
|
+
return if config.http_basic_auth_ok?(request)
|
|
162
|
+
|
|
163
|
+
request_http_basic_authentication("Railwatch")
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Who the server should see: Railwatch::Sidecar.config.user when the app
|
|
167
|
+
# names its user, else the HTTP Basic user, else one operator.
|
|
168
|
+
def visitor
|
|
169
|
+
given = Railwatch::Sidecar.config.user&.call(self)
|
|
170
|
+
return given.to_h.transform_keys(&:to_sym).reverse_merge(role: "admin") if given
|
|
171
|
+
|
|
172
|
+
basic_user = ActionController::HttpAuthentication::Basic.user_name_and_password(request).first if request.authorization.to_s.start_with?("Basic ")
|
|
173
|
+
# Decoded base64 comes back binary; parameterize needs a real encoding.
|
|
174
|
+
name = basic_user.to_s.dup.force_encoding(Encoding::UTF_8).scrub.presence || "Operator"
|
|
175
|
+
{ email: "#{name.parameterize.presence || 'operator'}@railwatch.invalid", name: name, role: "admin" }
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def signed_header(secret)
|
|
179
|
+
v = visitor
|
|
180
|
+
ProxyAuth.sign(secret, email: v[:email], name: v[:name], role: v[:role])
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# The server's MCP endpoint and JSON API: what an AI agent calls.
|
|
184
|
+
def agent_request?
|
|
185
|
+
mount = Railwatch::Sidecar.config.normalized_mount_path
|
|
186
|
+
rest = mount == "/" ? request.path : request.path.delete_prefix(mount)
|
|
187
|
+
rest == "/mcp" || rest.start_with?("/api/")
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Whether an agent request is signed for the visitor (config.agent_access).
|
|
191
|
+
# By default only in development, and only for a request from this
|
|
192
|
+
# machine (request.local?: a dev server bound to 0.0.0.0 still refuses
|
|
193
|
+
# the LAN) to a local host name (a page on another site that rebinds
|
|
194
|
+
# its own name to 127.0.0.1 arrives with that name as Host).
|
|
195
|
+
def agent_access?
|
|
196
|
+
setting = Railwatch::Sidecar.config.agent_access
|
|
197
|
+
return setting unless setting.nil?
|
|
198
|
+
|
|
199
|
+
Railwatch::Sidecar.config.environment == "development" && request.local? && LOCAL_HOSTS.include?(request.host)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# An agent's own token, which the server checks: sent on as it came,
|
|
203
|
+
# and the visitor is not signed (the server would ignore them anyway).
|
|
204
|
+
def agent_bearer
|
|
205
|
+
value = request.authorization.to_s
|
|
206
|
+
value if agent_request? && value.start_with?("Bearer ")
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def upstream_headers(secret)
|
|
210
|
+
headers = FORWARDED_HEADERS.each_with_object({}) do |name, h|
|
|
211
|
+
value = request.headers[name]
|
|
212
|
+
h[name] = value if value
|
|
213
|
+
end
|
|
214
|
+
cookies = request.cookies.slice(*FORWARDED_COOKIES)
|
|
215
|
+
headers["Cookie"] = cookies.map { |k, v| "#{k}=#{v}" }.join("; ") if cookies.any?
|
|
216
|
+
# The server compares Origin with Host, and builds absolute URLs (an
|
|
217
|
+
# Inertia version-mismatch reload) from Host and X-Forwarded-Proto.
|
|
218
|
+
headers["Host"] = request.host_with_port
|
|
219
|
+
headers["X-Forwarded-Proto"] = request.scheme
|
|
220
|
+
# The visitor, as the app sees it (RemoteIp: the proxies in front of
|
|
221
|
+
# the app are trusted, the browser's own X-Forwarded-For is not),
|
|
222
|
+
# and only that: the server (RAILWATCH_TRUST_PROXY) keys its per-IP
|
|
223
|
+
# limits on the last address here.
|
|
224
|
+
headers["X-Forwarded-For"] = request.remote_ip
|
|
225
|
+
# An agent request without access goes on unsigned: the server's 401
|
|
226
|
+
# tells the agent to send a token.
|
|
227
|
+
if (bearer = agent_bearer)
|
|
228
|
+
headers["Authorization"] = bearer
|
|
229
|
+
elsif !agent_request? || agent_access?
|
|
230
|
+
headers[ProxyAuth::HEADER] = signed_header(secret)
|
|
231
|
+
end
|
|
232
|
+
headers
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# The browser's socket can be taken from the app server and moved with
|
|
236
|
+
# IO.select: Rack hijack, and a plain socket (not Puma's own TLS, whose
|
|
237
|
+
# socket wrapper keeps decrypted bytes the relay could not see).
|
|
238
|
+
def hijackable?
|
|
239
|
+
return false unless request.env["rack.hijack"]
|
|
240
|
+
|
|
241
|
+
socket = request.env["puma.socket"]
|
|
242
|
+
socket.nil? || socket.is_a?(::BasicSocket)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Connects to the server and leaves the request to ProxyHandoff; the
|
|
246
|
+
# empty 200 here is never sent. Past max_proxied_requests the visitor
|
|
247
|
+
# gets a 503 at once.
|
|
248
|
+
def hand_off(secret)
|
|
249
|
+
relay = Relay.current
|
|
250
|
+
if relay.full?(:proxied)
|
|
251
|
+
Rails.logger.warn("[railwatch-sidecar] #{relay.max(:proxied)} dashboard requests in flight (max_proxied_requests); refusing another")
|
|
252
|
+
response.headers["Retry-After"] = "1"
|
|
253
|
+
return render(plain: "The dashboard is busy (#{relay.max(:proxied)} requests in flight in this process). Try again in a moment.",
|
|
254
|
+
status: :service_unavailable)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
upstream = connect
|
|
258
|
+
request_head, body = upstream_request(secret)
|
|
259
|
+
request.env[ProxyHandoff::ENV_KEY] = ProxyHandoff::Handoff.new(upstream, request_head, body)
|
|
260
|
+
head :ok
|
|
261
|
+
rescue SystemCallError, IOError => e
|
|
262
|
+
upstream&.close rescue nil
|
|
263
|
+
unavailable("#{e.class}: #{e.message}")
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def connect
|
|
267
|
+
TCPSocket.new("127.0.0.1", Railwatch::Sidecar.config.port, connect_timeout: Railwatch::Sidecar.config.proxy_connect_timeout)
|
|
268
|
+
rescue IO::TimeoutError => e
|
|
269
|
+
raise Errno::ETIMEDOUT, e.message
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# The request as the server gets it: its head (and its body, when Puma
|
|
273
|
+
# kept that in memory), and the body Puma spooled to a tempfile, as a
|
|
274
|
+
# second descriptor of it (Puma closes its own when this thread
|
|
275
|
+
# returns). Puma has read the body in full and decoded a chunked one,
|
|
276
|
+
# so it always has a length.
|
|
277
|
+
def upstream_request(secret)
|
|
278
|
+
input = request.body
|
|
279
|
+
input.rewind if input.respond_to?(:rewind)
|
|
280
|
+
length = request.content_length.to_i
|
|
281
|
+
body = input.is_a?(::File) ? input.dup : nil
|
|
282
|
+
# The browser's HTTP version: an HTTP/1.0 client (nginx's default
|
|
283
|
+
# upstream protocol) can't read a chunked response, and the server
|
|
284
|
+
# doesn't send one to an HTTP/1.0 request.
|
|
285
|
+
version = request.get_header("SERVER_PROTOCOL") == "HTTP/1.0" ? "HTTP/1.0" : "HTTP/1.1"
|
|
286
|
+
headers = upstream_headers(secret)
|
|
287
|
+
headers["Accept-Encoding"] = request.headers["Accept-Encoding"].presence || "identity"
|
|
288
|
+
headers["Content-Length"] = length.to_s if length.positive? || request.content_length
|
|
289
|
+
headers["Connection"] = "close"
|
|
290
|
+
head = "#{request.request_method} #{request.fullpath} #{version}\r\n" \
|
|
291
|
+
"#{headers.map { |k, v| "#{k}: #{v.to_s.delete("\r\n")}\r\n" }.join}\r\n".b
|
|
292
|
+
head << input.read.to_s.b if body.nil? && length.positive?
|
|
293
|
+
[ head, body ]
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# Without hijack: the response is streamed from this thread, status and
|
|
297
|
+
# headers as soon as the server sends them, then the body as the app
|
|
298
|
+
# server writes it (Upstream#each), so a large export never sits whole
|
|
299
|
+
# in this process. The request body is streamed too, from rack.input.
|
|
300
|
+
def stream(secret)
|
|
301
|
+
upstream = Upstream.new(request, upstream_headers(secret), port: Railwatch::Sidecar.config.port)
|
|
302
|
+
response = upstream.start
|
|
303
|
+
response.each_header do |name, _|
|
|
304
|
+
next if HOP_BY_HOP.include?(name) || Relay::APP_OWNED.include?(name)
|
|
305
|
+
|
|
306
|
+
values = response.get_fields(name)
|
|
307
|
+
self.response.headers[name] = name == "set-cookie" ? values : values.join(", ")
|
|
308
|
+
end
|
|
309
|
+
# The server's length is the body's: Upstream asks for it unencoded
|
|
310
|
+
# unless the browser accepts an encoding, and never decodes it.
|
|
311
|
+
self.response.headers["content-length"] = response["content-length"] if response["content-length"] && !response.chunked?
|
|
312
|
+
self.status = response.code.to_i
|
|
313
|
+
self.response_body = request.head? || !response.class.body_permitted? ? (upstream.close; []) : upstream
|
|
314
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Net::OpenTimeout, EOFError => e
|
|
315
|
+
unavailable("#{e.class}: #{e.message}")
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def websocket?
|
|
319
|
+
request.get? && request.headers["Upgrade"].to_s.casecmp?("websocket")
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
# The live channel: the handshake is sent on with the signed header and
|
|
323
|
+
# the connection is spliced to the server's by the process's one relay
|
|
324
|
+
# thread (Relay), at most config.max_live_connections at a time. Needs
|
|
325
|
+
# a server with full Rack hijack (Puma).
|
|
326
|
+
def tunnel(secret)
|
|
327
|
+
hijack = request.env["rack.hijack"]
|
|
328
|
+
return head(:not_implemented) unless hijack
|
|
329
|
+
|
|
330
|
+
relay = Relay.current
|
|
331
|
+
if relay.full?(:live)
|
|
332
|
+
Rails.logger.warn("[railwatch-sidecar] #{relay.max(:live)} live-channel connections open (max_live_connections); refusing another")
|
|
333
|
+
return head(:service_unavailable)
|
|
334
|
+
end
|
|
335
|
+
upstream = connect
|
|
336
|
+
handshake = upstream_headers(secret).merge(
|
|
337
|
+
"Connection" => "Upgrade", "Upgrade" => "websocket",
|
|
338
|
+
"Sec-WebSocket-Key" => request.headers["Sec-WebSocket-Key"],
|
|
339
|
+
"Sec-WebSocket-Version" => request.headers["Sec-WebSocket-Version"],
|
|
340
|
+
"Sec-WebSocket-Protocol" => request.headers["Sec-WebSocket-Protocol"],
|
|
341
|
+
"Sec-WebSocket-Extensions" => request.headers["Sec-WebSocket-Extensions"]
|
|
342
|
+
).compact
|
|
343
|
+
upstream.write("GET #{request.fullpath} HTTP/1.1\r\n#{handshake.map { |k, v| "#{k}: #{v}\r\n" }.join}\r\n")
|
|
344
|
+
client = hijack.call
|
|
345
|
+
unless relay.add(client, upstream)
|
|
346
|
+
[ client, upstream ].each { |io| io.close rescue nil }
|
|
347
|
+
end
|
|
348
|
+
self.status = -1
|
|
349
|
+
self.response_body = []
|
|
350
|
+
rescue SystemCallError => e
|
|
351
|
+
unavailable("#{e.class}: #{e.message}")
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def unavailable(why)
|
|
355
|
+
message = "The Railwatch server is not answering on 127.0.0.1:#{Railwatch::Sidecar.config.port} (#{why}). " \
|
|
356
|
+
"It is started by `plugin :railwatch_sidecar` in config/puma.rb or `bin/rails railwatch_sidecar:start`; " \
|
|
357
|
+
"its log is #{Railwatch::Sidecar.data_dir.log_file}."
|
|
358
|
+
Rails.logger.error("[railwatch-sidecar] #{message}")
|
|
359
|
+
render plain: message, status: :service_unavailable
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "puma/plugin"
|
|
4
|
+
|
|
5
|
+
# `plugin :railwatch_server`, the name before railwatch-server became
|
|
6
|
+
# railwatch-sidecar (0.2.0): the same plugin. Use `plugin :railwatch_sidecar`;
|
|
7
|
+
# this one works until 1.0.
|
|
8
|
+
require_relative "railwatch_sidecar"
|
|
9
|
+
|
|
10
|
+
warn("[railwatch-sidecar] DEPRECATED: `plugin :railwatch_server` is now `plugin :railwatch_sidecar` in config/puma.rb " \
|
|
11
|
+
"(railwatch-server was renamed railwatch-sidecar; the old name works until 1.0)")
|
|
12
|
+
Puma::Plugins.register("railwatch_server", Puma::Plugins.find("railwatch_sidecar"))
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "puma/plugin"
|
|
4
|
+
|
|
5
|
+
# `plugin :railwatch_sidecar` in config/puma.rb: runs the bundled Railwatch
|
|
6
|
+
# server as a child of Puma's master (single or cluster mode; never in a
|
|
7
|
+
# worker), restarts it with backoff when it dies, and stops it after Puma
|
|
8
|
+
# has drained its requests and flushed its reporter.
|
|
9
|
+
#
|
|
10
|
+
# The shape is the railwatch gem's own writer plugin
|
|
11
|
+
# (lib/puma/plugin/railwatch.rb). A bare `puma` fires plugin starts before
|
|
12
|
+
# it loads the Rack app, so there a background supervisor waits for
|
|
13
|
+
# after_booted. Under `bin/rails server` the app is loaded first, and the
|
|
14
|
+
# server is started in `start` itself, before Puma accepts a request: an
|
|
15
|
+
# agent whose first delivery is refused counts it as backpressure and
|
|
16
|
+
# samples fewer requests for the next second or two. Stopping is in at_exit,
|
|
17
|
+
# not after_stopped: Puma fires after_stopped from its TERM trap BEFORE it
|
|
18
|
+
# drains in-flight requests, and those requests' records still have to
|
|
19
|
+
# reach this server.
|
|
20
|
+
Puma::Plugin.create do
|
|
21
|
+
POLL = 1
|
|
22
|
+
|
|
23
|
+
def start(launcher)
|
|
24
|
+
@log_writer = launcher.log_writer
|
|
25
|
+
@puma_pid = $$
|
|
26
|
+
@booted = false
|
|
27
|
+
@shutting_down = false
|
|
28
|
+
|
|
29
|
+
start_server_now if app_loaded?
|
|
30
|
+
in_background { supervise }
|
|
31
|
+
launcher.events.after_booted { @booted = true }
|
|
32
|
+
# A hot restart (USR2) re-execs this process: the server is stopped
|
|
33
|
+
# first and the new master starts its own.
|
|
34
|
+
launcher.events.before_restart { stop_server if Process.pid == @puma_pid }
|
|
35
|
+
launcher.events.after_stopped { @shutting_down = true }
|
|
36
|
+
at_exit { shutdown if Process.pid == @puma_pid }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def active?
|
|
42
|
+
defined?(::Railwatch::Sidecar) && ::Railwatch::Sidecar.enabled?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def app_loaded?
|
|
46
|
+
defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application&.initialized?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def start_server_now
|
|
50
|
+
return unless active?
|
|
51
|
+
|
|
52
|
+
build_server
|
|
53
|
+
@server.ensure_running
|
|
54
|
+
rescue StandardError => e
|
|
55
|
+
log "Railwatch server NOT started: #{e.class}: #{e.message}"
|
|
56
|
+
@server = nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def build_server
|
|
60
|
+
::Railwatch::Sidecar.provision!(log: method(:log))
|
|
61
|
+
@server = ::Railwatch::Sidecar::Process.new(
|
|
62
|
+
config: ::Railwatch::Sidecar.config, data_dir: ::Railwatch::Sidecar.data_dir,
|
|
63
|
+
binary: ::Railwatch::Sidecar::Binary.path, log: method(:log)
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def supervise
|
|
68
|
+
sleep 0.05 until @server || @booted || @shutting_down
|
|
69
|
+
return unless active?
|
|
70
|
+
|
|
71
|
+
build_server unless @server
|
|
72
|
+
until @shutting_down
|
|
73
|
+
@server.ensure_running
|
|
74
|
+
sleep POLL
|
|
75
|
+
end
|
|
76
|
+
rescue StandardError => e
|
|
77
|
+
log "Railwatch server NOT started: #{e.class}: #{e.message}"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The app's own reporter first, while the server still listens: the last
|
|
81
|
+
# requests' records (and in single mode, every request's) go out now.
|
|
82
|
+
#
|
|
83
|
+
# Under `bin/rails server` the app loads before Puma starts this plugin,
|
|
84
|
+
# so this at_exit was registered after the railwatch gem's and runs
|
|
85
|
+
# BEFORE them (at_exit is LIFO). Those stop the health sampler and flush
|
|
86
|
+
# the session tracker, which emits records; run them here first, so their
|
|
87
|
+
# records are in the final flush rather than produced after the server is
|
|
88
|
+
# gone. Each stop! is idempotent; the gem's own at_exit then does nothing.
|
|
89
|
+
def shutdown
|
|
90
|
+
@shutting_down = true
|
|
91
|
+
if defined?(::Railwatch) && ::Railwatch.respond_to?(:enabled?) && ::Railwatch.enabled?
|
|
92
|
+
[ "Health", "Sessions" ].each do |name|
|
|
93
|
+
mod = ::Railwatch.const_get(name) if ::Railwatch.const_defined?(name)
|
|
94
|
+
mod.stop! if mod.respond_to?(:stop!)
|
|
95
|
+
end
|
|
96
|
+
::Railwatch.reporter.shutdown
|
|
97
|
+
end
|
|
98
|
+
stop_server
|
|
99
|
+
rescue StandardError => e
|
|
100
|
+
log "Railwatch server shutdown failed (#{e.class}: #{e.message})"
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def stop_server
|
|
104
|
+
@server&.stop
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def log(message)
|
|
108
|
+
@log_writer.log("[railwatch-sidecar] #{message}")
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Railwatch
|
|
4
|
+
module Sidecar
|
|
5
|
+
# The bundled `railwatch` executable, the way tailwindcss-ruby finds its
|
|
6
|
+
# own: one directory per platform under exe/, and the platform gem for
|
|
7
|
+
# this machine carries exactly one of them. The plain "ruby" gem has
|
|
8
|
+
# none and says so.
|
|
9
|
+
module Binary
|
|
10
|
+
PLATFORMS = %w[x86_64-linux aarch64-linux x86_64-darwin arm64-darwin].freeze
|
|
11
|
+
DIR = File.expand_path("../../../exe", __dir__)
|
|
12
|
+
GEM_NAME = "railwatch-sidecar"
|
|
13
|
+
|
|
14
|
+
class NotFound < StandardError; end
|
|
15
|
+
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def path
|
|
19
|
+
own = Railwatch::Sidecar.config.binary
|
|
20
|
+
if own
|
|
21
|
+
raise NotFound, "RAILWATCH_SIDECAR_BINARY=#{own} is not an executable file" unless File.executable?(own)
|
|
22
|
+
|
|
23
|
+
return own
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
found = Dir.glob(File.join(DIR, "*", "railwatch")).find do |file|
|
|
27
|
+
Gem::Platform.match_gem?(Gem::Platform.new(File.basename(File.dirname(file))), GEM_NAME)
|
|
28
|
+
end
|
|
29
|
+
return found if found && File.executable?(found)
|
|
30
|
+
|
|
31
|
+
raise NotFound, <<~MESSAGE.strip
|
|
32
|
+
railwatch-sidecar has no Railwatch server binary for #{local_platform}.
|
|
33
|
+
Prebuilt binaries ship in the #{PLATFORMS.join(', ')} gems; this is the plain one.
|
|
34
|
+
Either let Bundler pick the platform gem (`bundle lock --add-platform #{local_platform}`,
|
|
35
|
+
and check `bundle config force_ruby_platform` is not true), or install the server
|
|
36
|
+
yourself (https://github.com/Rebulk/railwatch-rs, docs/SELF-HOST.md) and set
|
|
37
|
+
RAILWATCH_SIDECAR_BINARY=/path/to/railwatch.
|
|
38
|
+
MESSAGE
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def local_platform
|
|
42
|
+
[ Gem::Platform.local.cpu, Gem::Platform.local.os ].join("-")
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|