Class: Pastry
- Inherits:
-
Object
- Object
- Pastry
- Defined in:
- lib/pastry.rb
Defined Under Namespace
Modules: PastryServer Classes: Backend
Instance Attribute Summary collapse
-
#daemon ⇒ Object
readonly
Returns the value of attribute daemon.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#logfile ⇒ Object
readonly
Returns the value of attribute logfile.
-
#pidfile ⇒ Object
readonly
Returns the value of attribute pidfile.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#unix ⇒ Object
readonly
Returns the value of attribute unix.
Instance Method Summary collapse
- #create_pidfile ⇒ Object
- #ensure_not_running! ⇒ Object
-
#initialize(pool, app, options = {}) ⇒ Pastry
constructor
A new instance of Pastry.
- #motd ⇒ Object
- #name ⇒ Object
- #run(server, options, worker) ⇒ Object
- #start ⇒ Object
- #start! ⇒ Object
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, = {} @pool = pool @app = app @host = .fetch :host, '127.0.0.1' @unix = .fetch :socket, nil @logfile = .fetch :logfile, nil @daemon = .fetch :daemonize, false @pidfile = .fetch :pidfile, '/tmp/pastry.pid' @name = .fetch :name, nil @port = .fetch(:port, 3000).to_i @queue = .fetch(:queue, 1024).to_i @maxconn = .fetch(:maxconn, 1024).to_i @timeout = .fetch(:timeout, 30).to_i end |
Instance Attribute Details
#daemon ⇒ Object (readonly)
Returns the value of attribute daemon.
7 8 9 |
# File 'lib/pastry.rb', line 7 def daemon @daemon end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/pastry.rb', line 7 def host @host end |
#logfile ⇒ Object (readonly)
Returns the value of attribute logfile.
7 8 9 |
# File 'lib/pastry.rb', line 7 def logfile @logfile end |
#pidfile ⇒ Object (readonly)
Returns the value of attribute pidfile.
7 8 9 |
# File 'lib/pastry.rb', line 7 def pidfile @pidfile end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
7 8 9 |
# File 'lib/pastry.rb', line 7 def pool @pool end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/pastry.rb', line 7 def port @port end |
#unix ⇒ Object (readonly)
Returns the value of attribute unix.
7 8 9 |
# File 'lib/pastry.rb', line 7 def unix @unix end |
Instance Method Details
#create_pidfile ⇒ Object
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 |
#motd ⇒ Object
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 |
#name ⇒ Object
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, , worker fork do $0 = "#{@name ? "%s worker" % @name : "pastry chef"} #{worker} (started: #{Time.now})" EM.run { Backend.new().start(server) } end end |
#start ⇒ Object
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) = {timeout: @timeout, maximum_connections: @maxconn} logger.info motd $0 = name if @name @running = true pids = pool.times.map {|n| run(server, , 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, , 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 |