Module: Thin::FunDaemon

Defined in:
lib/thin/fun_daemon.rb

Class Method Summary collapse

Class Method Details

.add_conn(conn) ⇒ Object



5
6
7
8
# File 'lib/thin/fun_daemon.rb', line 5

def self.add_conn(conn)
  @conns ||= {}
  @conns[conn] = true
end

.del_conn(conn) ⇒ Object



10
11
12
13
14
15
# File 'lib/thin/fun_daemon.rb', line 10

def self.del_conn(conn)
  @conns.delete(conn)
  if @trap_called && @conns.empty?
    EM.next_tick{ EM.stop }
  end
end

.run_class(host, port, klass, *args, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/thin/fun_daemon.rb', line 55

def self.run_class(host, port, klass, *args, &block)
  EM.schedule do
    serv = EM.start_server host, port, klass, *args do |conn|
      yield conn
      conn.unbind_callback = method(:del_conn)
      add_conn conn
    end
    servers[serv] = true
  end
end

.run_rack(host, port, app = nil, &blk) ⇒ Object



66
67
68
69
70
71
# File 'lib/thin/fun_daemon.rb', line 66

def self.run_rack(host, port, app = nil, &blk)
  app ||= blk
  run_class host, port, FunRackLike do |conn|
    conn.app = app
  end
end

.serversObject



17
18
19
# File 'lib/thin/fun_daemon.rb', line 17

def self.servers
  @servers ||= {}
end

.trapObject



21
22
23
24
25
26
27
28
# File 'lib/thin/fun_daemon.rb', line 21

def self.trap
  return if @trap_set
  @trap_set = true
  @prev_traps = {}
  [:INT, :TERM, :QUIT].each do |name|
    @prev_traps[name] = ::Signal.trap(name){|sig| trap_call(name, sig) }
  end
end

.trap_call(name, sig) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/thin/fun_daemon.rb', line 30

def self.trap_call(name, sig)
  @trap_called = true
  Thread.new do
    EM.schedule do
      servers.each do |serv, _|
        EM.stop_tcp_server serv
      end
      if @conns && !@conns.empty?
        @conns.each do |conn, _|
          conn.force_close!
        end
      else
        EM.next_tick{ EM.stop }
      end
    end
  end
  @prev_traps.each do |nm, prev|
    if nm == name && prev.respond_to?(:call)
      prev.call(sig)
      next
    end
    ::Signal.trap(nm, prev)
  end
end