Skip to content

Commit 55572e0

Browse files
committed
Use middleware to check if server is running instead of checking PID
1 parent ffb894d commit 55572e0

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed

lib/ruby_lsp/ruby_lsp_rails/rails_client.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ def initialize
3838
MESSAGE
3939
end
4040

41-
base_uri = File.read(app_uri_path).chomp
42-
@uri = T.let("#{base_uri}/ruby_lsp_rails", String)
41+
url = File.read(app_uri_path).chomp
42+
43+
scheme, rest = url.split("://")
44+
uri, port = T.must(rest).split(":")
45+
46+
@ssl = T.let(scheme == "https", T::Boolean)
47+
@uri = T.let(T.must(uri), String)
48+
@port = T.let(T.must(port).to_i, Integer)
4349
end
4450

4551
sig { params(name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
@@ -54,28 +60,22 @@ def model(name)
5460

5561
sig { void }
5662
def check_if_server_is_running!
57-
# Check if the Rails server is running. Warn the user to boot it for Rails features
58-
pid_file = ENV.fetch("PIDFILE") { File.join(@root, "tmp", "pids", "server.pid") }
59-
60-
# If the PID file doesn't exist, then the server hasn't been booted
61-
raise ServerNotRunningError, SERVER_NOT_RUNNING_MESSAGE unless File.exist?(pid_file)
62-
63-
pid = File.read(pid_file).to_i
64-
65-
begin
66-
# Issuing an EXIT signal to an existing process actually doesn't make the server shutdown. But if this
67-
# call succeeds, then the server is running. If the PID doesn't exist, Errno::ESRCH is raised
68-
Process.kill(T.must(Signal.list["EXIT"]), pid)
69-
rescue Errno::ESRCH
70-
raise ServerNotRunningError, SERVER_NOT_RUNNING_MESSAGE
71-
end
63+
request("activate", 0.2)
64+
rescue Errno::ECONNREFUSED
65+
raise ServerNotRunningError, SERVER_NOT_RUNNING_MESSAGE
66+
rescue Net::ReadTimeout
67+
# If the server is running, but the initial request is taking too long, we don't want to block the
68+
# initialization of the Ruby LSP
7269
end
7370

7471
private
7572

76-
sig { params(path: String).returns(Net::HTTPResponse) }
77-
def request(path)
78-
Net::HTTP.get_response(URI("#{@uri}/#{path}"))
73+
sig { params(path: String, timeout: T.nilable(Float)).returns(Net::HTTPResponse) }
74+
def request(path, timeout = nil)
75+
http = Net::HTTP.new(@uri, @port)
76+
http.use_ssl = @ssl
77+
http.read_timeout = timeout if timeout
78+
http.get("/ruby_lsp_rails/#{path}")
7979
end
8080
end
8181
end

lib/ruby_lsp_rails/middleware.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Rails
66
class Middleware
77
extend T::Sig
88

9-
PATH_REGEXP = %r{/ruby_lsp_rails/models/(?<model_name>.+)}
9+
BASE_PATH = "/ruby_lsp_rails/"
1010

1111
sig { params(app: T.untyped).void }
1212
def initialize(app)
@@ -16,13 +16,16 @@ def initialize(app)
1616
sig { params(env: T::Hash[T.untyped, T.untyped]).returns(T::Array[T.untyped]) }
1717
def call(env)
1818
request = ActionDispatch::Request.new(env)
19-
# TODO: improve the model name regex
20-
match = request.path.match(PATH_REGEXP)
19+
path = request.path
20+
return @app.call(env) unless path.start_with?(BASE_PATH)
2121

22-
if match
23-
resolve_database_info_from_model(match[:model_name])
22+
route, argument = path.delete_prefix(BASE_PATH).split("/")
23+
24+
case route
25+
when "models"
26+
resolve_database_info_from_model(argument)
2427
else
25-
@app.call(env)
28+
[200, { "Content-Type" => "text/plain" }, []]
2629
end
2730
rescue
2831
@app.call(env)

test/ruby_lsp_rails/middleware_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class ModelsControllerTest < ActionDispatch::IntegrationTest
3434
get "/ruby_lsp_rails/models/ApplicationJob"
3535
assert_response(:not_found)
3636
end
37+
38+
test "GET activate returns success to display that server is running" do
39+
get "/ruby_lsp_rails/activate"
40+
assert_response(:success)
41+
end
3742
end
3843
end
3944
end

test/ruby_lsp_rails/rails_client_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class RailsClientTest < ActiveSupport::TestCase
5353
ensure
5454
ENV["BUNDLE_GEMFILE"] = previous_bundle_gemfile
5555
end
56+
57+
test "check_if_server_is_running! raises if no server is found" do
58+
Net::HTTP.any_instance.expects(:get).raises(Errno::ECONNREFUSED)
59+
60+
assert_raises(RailsClient::ServerNotRunningError) do
61+
RailsClient.instance.check_if_server_is_running!
62+
end
63+
end
5664
end
5765
end
5866
end

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def stub_http_request(code, body)
2525
response.expects(:code).returns("200")
2626
response.expects(:body).returns(body)
2727

28-
Net::HTTP.stubs(:get_response).returns(response)
28+
Net::HTTP.any_instance.expects(:get).returns(response)
2929
end
3030

3131
def setup

0 commit comments

Comments
 (0)