class Spring::Client::Run

Constants

FORWARDED_SIGNALS
TIMEOUT

Public Class Methods

new(args) click to toggle source
Calls superclass method Spring::Client::Command.new
# File lib/spring/client/run.rb, line 10
def initialize(args)
  super
  @signal_queue = []
end

Public Instance Methods

boot_server() click to toggle source
# File lib/spring/client/run.rb, line 38
def boot_server
  env.socket_path.unlink if env.socket_path.exist?

  pid = fork {
    require "spring/server"
    Spring::Server.boot
  }

  until env.socket_path.exist?
    _, status = Process.waitpid2(pid, Process::WNOHANG)
    exit status.exitstatus if status
    sleep 0.1
  end
end
call() click to toggle source
# File lib/spring/client/run.rb, line 23
def call
  boot_server unless env.server_running?
  verify_server_version

  application, client = UNIXSocket.pair

  queue_signals
  connect_to_application(client)
  run_command(client, application)
rescue Errno::ECONNRESET
  exit 1
ensure
  server.close if @server
end
connect_to_application(client) click to toggle source
# File lib/spring/client/run.rb, line 66
def connect_to_application(client)
  server.send_io client
  send_json server, "args" => args, "default_rails_env" => default_rails_env

  if IO.select([server], [], [], TIMEOUT)
    server.gets or raise CommandNotFound
  else
    raise "Error connecting to Spring server"
  end
end
default_rails_env() click to toggle source
# File lib/spring/client/run.rb, line 146
def default_rails_env
  ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
forward_signal(sig, pid) click to toggle source
# File lib/spring/client/run.rb, line 126
def forward_signal(sig, pid)
  kill(sig, pid)
rescue Errno::ESRCH
  # If the application process is gone, then don't block the
  # signal on this process.
  trap(sig, 'DEFAULT')
  Process.kill(sig, Process.pid)
end
forward_signals(pid) click to toggle source
# File lib/spring/client/run.rb, line 117
def forward_signals(pid)
  @signal_queue.each { |sig| kill sig, pid }

  FORWARDED_SIGNALS.each do |sig|
    trap(sig) { forward_signal sig, pid }
  end
rescue Errno::ESRCH
end
kill(sig, pid) click to toggle source
# File lib/spring/client/run.rb, line 135
def kill(sig, pid)
  Process.kill(sig, -Process.getpgid(pid))
end
log(message) click to toggle source
# File lib/spring/client/run.rb, line 15
def log(message)
  env.log "[client] #{message}"
end
queue_signals() click to toggle source
# File lib/spring/client/run.rb, line 111
def queue_signals
  FORWARDED_SIGNALS.each do |sig|
    trap(sig) { @signal_queue << sig }
  end
end
run_command(client, application) click to toggle source
# File lib/spring/client/run.rb, line 77
def run_command(client, application)
  log "sending command"

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

  send_json application, "args" => args, "env" => ENV.to_hash

  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?
    log "got pid: #{pid}"

    forward_signals(pid.to_i)
    status = application.read.to_i

    log "got exit status #{status}"

    exit status
  else
    log "got no pid"
    exit 1
  end
ensure
  application.close
end
send_json(socket, data) click to toggle source
# File lib/spring/client/run.rb, line 139
def send_json(socket, data)
  data = JSON.dump(data)

  socket.puts  data.bytesize
  socket.write data
end
server() click to toggle source
# File lib/spring/client/run.rb, line 19
def server
  @server ||= UNIXSocket.open(env.socket_name)
end
verify_server_version() click to toggle source
# File lib/spring/client/run.rb, line 53
      def verify_server_version
        server_version = server.gets.chomp
        if server_version != env.version
          $stderr.puts <<-ERROR
There is a version mismatch between 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