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(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

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

Class Method Details

.bootObject



8
9
10
# File 'lib/spring/server.rb', line 8

def self.boot
  new.boot
end

Instance Method Details

#bootObject



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_hookObject



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_pidfileObject



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