Class: Spring::Server
- Inherits:
-
Object
- Object
- Spring::Server
- Defined in:
- lib/spring/server.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
Instance Method Summary collapse
- #boot ⇒ Object
-
#ignore_signals ⇒ Object
Ignore SIGINT and SIGQUIT otherwise the user typing ^C or ^\ on the command line will kill the server/application.
-
#initialize(env = Env.new) ⇒ Server
constructor
A new instance of Server.
- #log(message) ⇒ Object
- #rails_env_for(args, default_rails_env) ⇒ Object
-
#redirect_output ⇒ Object
We need to redirect STDOUT and STDERR, otherwise the server will keep the original FDs open which would break piping.
- #serve(client) ⇒ Object
- #set_exit_hook ⇒ Object
-
#set_pgid ⇒ Object
Boot the server into the process group of the current session.
- #set_process_title ⇒ Object
- #shutdown ⇒ Object
- #start_server ⇒ Object
- #write_pidfile ⇒ Object
Constructor Details
#initialize(env = Env.new) ⇒ Server
Returns a new instance of Server.
19 20 21 22 23 24 |
# File 'lib/spring/server.rb', line 19 def initialize(env = Env.new) @env = env @applications = Hash.new { |h, k| h[k] = ApplicationManager.new(k) } @pidfile = env.pidfile_path.open('a') @mutex = Mutex.new end |
Instance Attribute Details
#env ⇒ Object (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 ⇒ Object
13 14 15 |
# File 'lib/spring/server.rb', line 13 def self.boot new.boot end |
Instance Method Details
#boot ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/spring/server.rb', line 30 def boot Spring.verify_environment write_pidfile set_pgid ignore_signals set_exit_hook set_process_title start_server end |
#ignore_signals ⇒ Object
Ignore SIGINT and SIGQUIT otherwise the user typing ^C or ^\ on the command line will kill the server/application.
83 84 85 |
# File 'lib/spring/server.rb', line 83 def ignore_signals IGNORE_SIGNALS.each { |sig| trap(sig, "IGNORE") } end |
#log(message) ⇒ Object
26 27 28 |
# File 'lib/spring/server.rb', line 26 def log() env.log "[server] #{}" end |
#rails_env_for(args, default_rails_env) ⇒ Object
70 71 72 |
# File 'lib/spring/server.rb', line 70 def rails_env_for(args, default_rails_env) Spring.command(args.first).env(args.drop(1)) || default_rails_env end |
#redirect_output ⇒ Object
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.)
118 119 120 |
# File 'lib/spring/server.rb', line 118 def redirect_output [STDOUT, STDERR].each { |stream| stream.reopen(env.log_file) } end |
#serve(client) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/spring/server.rb', line 47 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 = command.values_at('args', 'default_rails_env') if Spring.command?(args.first) log "running command #{args.first}" client.puts client.puts @applications[rails_env_for(args, default_rails_env)].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_hook ⇒ Object
87 88 89 90 91 92 |
# File 'lib/spring/server.rb', line 87 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_pgid ⇒ Object
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).
77 78 79 |
# File 'lib/spring/server.rb', line 77 def set_pgid Process.setpgid(0, SID.pgid) end |
#set_process_title ⇒ Object
122 123 124 125 126 |
# File 'lib/spring/server.rb', line 122 def set_process_title ProcessTitleUpdater.run { |distance| "spring server | #{env.app_name} | started #{distance} ago" } end |
#shutdown ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/spring/server.rb', line 94 def shutdown [env.socket_path, env.pidfile_path].each do |path| if path.exist? path.unlink rescue nil end end @applications.values.map { |a| Thread.new { a.stop } }.map(&:join) end |
#start_server ⇒ Object
41 42 43 44 45 |
# File 'lib/spring/server.rb', line 41 def start_server server = UNIXServer.open(env.socket_name) log "started on #{env.socket_name}" loop { serve server.accept } end |
#write_pidfile ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/spring/server.rb', line 104 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 |