Class: Spring::Client::Run

Inherits:
Command
  • Object
show all
Defined in:
lib/spring/client/run.rb

Constant Summary collapse

SERVER_COMMAND =
[
  File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME')),
  "-I", File.expand_path("../../..", __FILE__),
  "-r", "spring/server",
  "-r", "bundler/setup",
  "-e", "Spring::Server.boot"
]
FORWARDED_SIGNALS =
%w(INT QUIT USR1 USR2 INFO)

Instance Attribute Summary

Attributes inherited from Command

#args, #env

Instance Method Summary collapse

Methods inherited from Command

call, #initialize

Constructor Details

This class inherits a constructor from Spring::Client::Command

Instance Method Details

#boot_serverObject



63
64
65
66
67
# File 'lib/spring/client/run.rb', line 63

def boot_server
  env.socket_path.unlink if env.socket_path.exist?
  Process.spawn(*SERVER_COMMAND)
  sleep 0.1 until env.socket_path.exist?
end

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spring/client/run.rb', line 19

def call
  Spring.verify_environment!
  boot_server unless env.server_running?

  application, client = UNIXSocket.pair

  server = UNIXSocket.open(env.socket_name)

  verify_server_version(server)
  server.send_io client
  server.puts rails_env_for(args.first)

  application.send_io STDOUT
  application.send_io STDERR
  application.send_io STDIN

  application.puts args.length

  args.each do |arg|
    application.puts  arg.length
    application.write arg
  end

  pid = server.gets
  pid = pid.chomp if pid

  # We must not close the client socket until we are sure that the application has
  # received the FD. Otherwise the FD can end up getting closed while it's in the server
  # socket buffer on OS X. This doesn't happen on Linux.
  client.close

  if pid && !pid.empty?
    forward_signals(pid.to_i)
    exit application.read.to_i
  else
    exit 1
  end
rescue Errno::ECONNRESET
  exit 1
ensure
  application.close if application
  server.close if server
end

#forward_signals(pid) ⇒ Object



92
93
94
95
96
# File 'lib/spring/client/run.rb', line 92

def forward_signals(pid)
  (FORWARDED_SIGNALS & Signal.list.keys).each do |sig|
    trap(sig) { Process.kill(sig, -Process.getpgid(pid)) }
  end
end

#rails_env_for(command_name) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/spring/client/run.rb', line 82

def rails_env_for(command_name)
  command = Spring.command(command_name)

  if command.respond_to?(:env)
    command.env
  else
    ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
  end
end

#verify_server_version(server) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spring/client/run.rb', line 69

def verify_server_version(server)
  server_version = server.gets.chomp
  if server_version != env.version
    STDERR.puts <<-ERROR
There is a version mismatch beween the spring client and the server.
You should restart the server and make sure to use the same version.

CLIENT: #{env.version}, SERVER: #{server_version}
ERROR
    exit 1
  end
end