Skip to content

Commit

Permalink
Merge pull request #712 from rails/configurable-timeouts
Browse files Browse the repository at this point in the history
Allow to configure boot and connect timeout
  • Loading branch information
byroot committed Apr 22, 2024
2 parents 2380451 + d73d1db commit 47ccb11
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
10 changes: 4 additions & 6 deletions lib/spring/client/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module Spring
module Client
class Run < Command
FORWARDED_SIGNALS = %w(INT QUIT USR1 USR2 INFO WINCH) & Signal.list.keys
CONNECT_TIMEOUT = 1
BOOT_TIMEOUT = 20

attr_reader :server

Expand Down Expand Up @@ -74,7 +72,7 @@ def boot_server
env.socket_path.unlink if env.socket_path.exist?

pid = Process.spawn(server_process_env, env.server_command, out: File::NULL)
timeout = Time.now + BOOT_TIMEOUT
timeout = Time.now + Spring.boot_timeout

@server_booted = true

Expand All @@ -85,7 +83,7 @@ def boot_server
exit status.exitstatus
elsif Time.now > timeout
$stderr.puts "Starting Spring server with `#{env.server_command}` " \
"timed out after #{BOOT_TIMEOUT} seconds"
"timed out after #{Spring.boot_timeout} seconds"
exit 1
end

Expand Down Expand Up @@ -122,7 +120,7 @@ def stop_server
end

def verify_server_version
unless IO.select([server], [], [], CONNECT_TIMEOUT)
unless IO.select([server], [], [], Spring.connect_timeout)
raise "Error connecting to Spring server"
end

Expand Down Expand Up @@ -151,7 +149,7 @@ def connect_to_application(client)
server.send_io client
send_json server, "args" => args, "default_rails_env" => default_rails_env, "spawn_env" => spawn_env, "reset_env" => reset_env

if IO.select([server], [], [], CONNECT_TIMEOUT)
if IO.select([server], [], [], Spring.connect_timeout)
server.gets or raise CommandNotFound
else
raise "Error connecting to Spring server"
Expand Down
5 changes: 4 additions & 1 deletion lib/spring/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require "spring/errors"

module Spring
@connect_timeout = 5
@boot_timeout = 20

class << self
attr_accessor :application_root
attr_accessor :application_root, :connect_timeout, :boot_timeout
attr_writer :quiet

def gemfile
Expand Down
2 changes: 1 addition & 1 deletion test/support/acceptance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def exec_name
test "server boot timeout" do
app.env["SPRING_SERVER_COMMAND"] = "sleep 1"
File.write("#{app.spring_client_config}", %(
Spring::Client::Run.const_set(:BOOT_TIMEOUT, 0.1)
Spring.boot_timeout = 0.1
))

assert_failure "bin/rails runner ''", stderr: "timed out"
Expand Down

0 comments on commit 47ccb11

Please sign in to comment.