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 2380451 commit d73d1db
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 d73d1db

Please sign in to comment.