Class: Pastry

Inherits:
Object
  • Object
show all
Defined in:
lib/pastry.rb

Defined Under Namespace

Modules: PastryServer Classes: Backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, app, options = {}) ⇒ Pastry

Returns a new instance of Pastry.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pastry.rb', line 9

def initialize pool, app, options = {}
  @pool    = pool
  @app     = app
  @host    = options.fetch :host,       '127.0.0.1'
  @unix    = options.fetch :socket,     nil
  @logfile = options.fetch :logfile,    nil
  @daemon  = options.fetch :daemonize,  false
  @pidfile = options.fetch :pidfile,    '/tmp/pastry.pid'
  @name    = options.fetch :name,       nil

  @port    = options.fetch(:port,    3000).to_i
  @queue   = options.fetch(:queue,   1024).to_i
  @maxconn = options.fetch(:maxconn, 1024).to_i
  @timeout = options.fetch(:timeout,   30).to_i
end

Instance Attribute Details

#daemonObject (readonly)

Returns the value of attribute daemon.



7
8
9
# File 'lib/pastry.rb', line 7

def daemon
  @daemon
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/pastry.rb', line 7

def host
  @host
end

#logfileObject (readonly)

Returns the value of attribute logfile.



7
8
9
# File 'lib/pastry.rb', line 7

def logfile
  @logfile
end

#pidfileObject (readonly)

Returns the value of attribute pidfile.



7
8
9
# File 'lib/pastry.rb', line 7

def pidfile
  @pidfile
end

#poolObject (readonly)

Returns the value of attribute pool.



7
8
9
# File 'lib/pastry.rb', line 7

def pool
  @pool
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/pastry.rb', line 7

def port
  @port
end

#unixObject (readonly)

Returns the value of attribute unix.



7
8
9
# File 'lib/pastry.rb', line 7

def unix
  @unix
end

Instance Method Details

#create_pidfileObject



47
48
49
# File 'lib/pastry.rb', line 47

def create_pidfile
  File.open(pidfile, 'w') {|fh| fh.write(Process.pid)}
end

#ensure_not_running!Object



39
40
41
42
43
44
45
# File 'lib/pastry.rb', line 39

def ensure_not_running!
  if File.exists?(pidfile) && pid = File.read(pidfile).to_i
    running = Process.kill(0, pid) rescue nil
    raise "already running with pid #{pid}" if running
    FileUtils.rm_f(pidfile)
  end
end

#motdObject



55
56
57
# File 'lib/pastry.rb', line 55

def motd
  "starting #{name} with #{pool} minions listening on #{unix ? 'socket %s ' % unix : 'port %d' % port}"
end

#nameObject



51
52
53
# File 'lib/pastry.rb', line 51

def name
  '%s master' % (@name || 'pastry')
end

#run(server, options, worker) ⇒ Object



103
104
105
106
107
108
# File 'lib/pastry.rb', line 103

def run server, options, worker
  fork do
    $0 = "#{@name ? "%s worker" % @name : "pastry chef"} #{worker} (started: #{Time.now})"
    EM.run { Backend.new(options).start(server) }
  end
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pastry.rb', line 25

def start
  ensure_not_running!
  Process.daemon if daemon

  if daemon || logfile
    STDOUT.reopen(logfile || '/tmp/pastry.log', 'a')
    STDERR.reopen(logfile || '/tmp/pastry.log', 'a')
    STDOUT.sync = true
    STDERR.sync = true
  end

  start!
end

#start!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pastry.rb', line 59

def start!
  create_pidfile
  server = unix ? UNIXServer.new(unix) : TCPServer.new(host, port)

  server.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) unless unix
  server.fcntl(Fcntl::F_SETFL, server.fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)

  server.listen(@queue)
  server.extend(PastryServer)

  server.app = @app
  logger     = Logger.new(logfile || (daemon ? '/tmp/pastry.log' : $stdout), 0)
  options    = {timeout: @timeout, maximum_connections: @maxconn}

  logger.info motd

  $0         = name if @name
  @running   = true
  pids       = pool.times.map {|n| run(server, options, n) }

  Signal.trap('CHLD') do
    if @running
      died  = pids.reject {|pid| Process.kill(0, pid) rescue nil}
      died.each do |pid|
        logger.info "process #{pid} died, starting a new one"
        idx       = pids.index(pid)
        pids[idx] = run(server, options, idx)
      end
    end
  end

  %w(INT TERM HUP).each do |signal|
    Signal.trap(signal) do
      @running = false
      logger.info "caught #{signal}, closing time for the bakery -- no more pastries!"
      pids.each {|pid| Process.kill(signal, pid) rescue nil}
      exit
    end
  end

  at_exit { FileUtils.rm_f(pidfile); FileUtils.rm_f(unix.to_s) }
  Process.waitall rescue nil
end