Skip to content

Commit

Permalink
Allow to configure boot and connect timeout
Browse files Browse the repository at this point in the history
The hardcoded values might not be enough for all applications.
  • Loading branch information
byroot committed Apr 16, 2024
1 parent cf107a5 commit 3519986
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 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 + String.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
6 changes: 6 additions & 0 deletions lib/spring/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ class << self
attr_accessor :application_root
attr_writer :quiet

attr_accessor :connect_timeout
@connect_timeout = 5

attr_accessor :boot_timeout
@boot_timeout = 20

def gemfile
require "bundler"

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 3519986

Please sign in to comment.