Class: Spring::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/spring/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



19
20
21
22
23
24
25
26
27
# File 'lib/spring/server.rb', line 19

def initialize(options = {})
  @foreground   = options.fetch(:foreground, false)
  @env          = options[:env] || default_env
  @applications = Hash.new do |hash, key|
    hash[key] = ApplicationManager.new(*key, env)
  end
  @pidfile      = env.pidfile_path.open('a')
  @mutex        = Mutex.new
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



17
18
19
# File 'lib/spring/server.rb', line 17

def env
  @env
end

Class Method Details

.boot(options = {}) ⇒ Object



13
14
15
# File 'lib/spring/server.rb', line 13

def self.boot(options = {})
  new(options).boot
end

Instance Method Details

#bootObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/spring/server.rb', line 37

def boot
  Spring.verify_environment

  write_pidfile
  set_pgid unless foreground?
  ignore_signals unless foreground?
  set_exit_hook
  set_process_title
  start_server
end

#foreground?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/spring/server.rb', line 29

def foreground?
  @foreground
end

#ignore_signalsObject

Ignore SIGINT and SIGQUIT otherwise the user typing ^C or ^\ on the command line will kill the server/application.



95
96
97
# File 'lib/spring/server.rb', line 95

def ignore_signals
  IGNORE_SIGNALS.each { |sig| trap(sig, "IGNORE") }
end

#log(message) ⇒ Object



33
34
35
# File 'lib/spring/server.rb', line 33

def log(message)
  env.log "[server] #{message}"
end

#rails_env_for(args, default_rails_env, spawn_env) ⇒ Object



81
82
83
# File 'lib/spring/server.rb', line 81

def rails_env_for(args, default_rails_env, spawn_env)
  [Spring.command(args.first).env(args.drop(1)) || default_rails_env, spawn_env]
end

#redirect_outputObject

We need to redirect STDOUT and STDERR, otherwise the server will keep the original FDs open which would break piping. (e.g. ‘spring rake -T | grep db` would hang forever because the server would keep the stdout FD open.)



132
133
134
# File 'lib/spring/server.rb', line 132

def redirect_output
  [STDOUT, STDERR].each { |stream| stream.reopen(env.log_file) }
end

#serve(client) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/spring/server.rb', line 55

def serve(client)
  log "accepted client"
  client.puts env.version

  app_client = client.recv_io
  command    = JSON.load(client.read(client.gets.to_i))

  args, default_rails_env, spawn_env, reset_env = command.values_at('args', 'default_rails_env', 'spawn_env', 'reset_env')

  if Spring.command?(args.first)
    application = @applications[rails_env_for(args, default_rails_env, spawn_env)]
    reset_if_env_changed(application, reset_env)

    log "running command #{args.first}"
    client.puts
    client.puts application.run(app_client)
  else
    log "command not found #{args.first}"
    client.close
  end
rescue SocketError => e
  raise e unless client.eof?
ensure
  redirect_output
end

#set_exit_hookObject



99
100
101
102
103
104
# File 'lib/spring/server.rb', line 99

def set_exit_hook
  server_pid = Process.pid

  # We don't want this hook to run in any forks of the current process
  at_exit { shutdown if Process.pid == server_pid }
end

#set_pgidObject

Boot the server into the process group of the current session. This will cause it to be automatically killed once the session ends (i.e. when the user closes their terminal).



88
89
90
91
# File 'lib/spring/server.rb', line 88

def set_pgid
  pgid = Process.getpgid(Process.getsid)
  Process.setpgid(0, pgid)
end

#set_process_titleObject



136
137
138
139
140
# File 'lib/spring/server.rb', line 136

def set_process_title
  ProcessTitleUpdater.run { |distance|
    "spring server | #{env.app_name} | started #{distance} ago"
  }
end

#shutdownObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/spring/server.rb', line 106

def shutdown
  log "shutting down"

  [env.socket_path, env.pidfile_path].each do |path|
    if path.exist?
      path.unlink rescue nil
    end
  end

  @applications.values.map { |a| Spring.failsafe_thread { a.stop } }.map(&:join)
end

#start_serverObject



48
49
50
51
52
53
# File 'lib/spring/server.rb', line 48

def start_server
  server = UNIXServer.open(env.socket_name)
  log "started on #{env.socket_name}"
  loop { serve server.accept }
rescue Interrupt
end

#write_pidfileObject



118
119
120
121
122
123
124
125
126
# File 'lib/spring/server.rb', line 118

def write_pidfile
  if @pidfile.flock(File::LOCK_EX | File::LOCK_NB)
    @pidfile.truncate(0)
    @pidfile.write("#{Process.pid}\n")
    @pidfile.fsync
  else
    exit 1
  end
end