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
# File 'lib/spring/server.rb', line 14

def initialize(env = Env.new)
  @env          = env
  @applications = Hash.new { |h, k| h[k] = ApplicationManager.new(k) }
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



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

def boot
  # Ignore SIGINT, otherwise the user typing ^C on the command line
  # will kill the background server.
  trap("INT", "IGNORE")

  set_exit_hook
  write_pidfile

  server = UNIXServer.open(env.socket_name)
  loop { @applications[server.accept.read].run server.accept }
end

#set_exit_hookObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spring/server.rb', line 31

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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spring/server.rb', line 44

def write_pidfile
  file = env.pidfile_path.open('a')

  if file.flock(File::LOCK_EX | File::LOCK_NB)
    file.truncate(0)
    file.write("#{Process.pid}\n")
    file.fsync
  else
    STDERR.puts "#{file.path} is locked; it looks like a server is already running"
    exit(1)
  end
end