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
-
#initialize(env = Env.new) ⇒ Server
constructor
A new instance of Server.
- #serve(client) ⇒ Object
- #set_exit_hook ⇒ Object
- #write_pidfile ⇒ Object
Constructor Details
#initialize(env = Env.new) ⇒ Server
Returns a new instance of Server.
14 15 16 17 18 |
# File 'lib/spring/server.rb', line 14 def initialize(env = Env.new) @env = env @applications = Hash.new { |h, k| h[k] = ApplicationManager.new(k) } @pidfile = env.pidfile_path.open('a') end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
12 13 14 |
# File 'lib/spring/server.rb', line 12 def env @env end |
Class Method Details
.boot ⇒ Object
8 9 10 |
# File 'lib/spring/server.rb', line 8 def self.boot new.boot end |
Instance Method Details
#boot ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/spring/server.rb', line 20 def boot # Ignore SIGINT and SIGQUIT otherwise the user typing ^C or ^\ on the command line # will kill the server/application. IGNORE_SIGNALS.each { |sig| trap(sig, "IGNORE") } set_exit_hook write_pidfile server = UNIXServer.open(env.socket_name) loop { serve server.accept } end |
#serve(client) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/spring/server.rb', line 32 def serve(client) app_client = client.recv_io rails_env = client.gets.chomp client.puts @applications[rails_env].run(app_client) end |
#set_exit_hook ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/spring/server.rb', line 39 def set_exit_hook server_pid = Process.pid at_exit do # We don't want this hook to run in any forks of the current process if Process.pid == server_pid [env.socket_path, env.pidfile_path].each do |path| path.unlink if path.exist? end end end end |
#write_pidfile ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/spring/server.rb', line 52 def write_pidfile if @pidfile.flock(File::LOCK_EX | File::LOCK_NB) @pidfile.truncate(0) @pidfile.write("#{Process.pid}\n") @pidfile.fsync else STDERR.puts "#{file.path} is locked; it looks like a server is already running" exit(1) end end |