Class: Plum::Rack::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/plum/rack/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, config) ⇒ Server

Returns a new instance of Server.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/plum/rack/server.rb', line 7

def initialize(app, config)
  @config = config
  @state = :null
  @app = config[:debug] ? ::Rack::CommonLogger.new(app) : app
  @logger = Logger.new(config[:log] || $stdout).tap { |l| l.level = config[:debug] ? Logger::DEBUG : Logger::INFO }
  @listeners = config[:listeners].map { |lc| lc[:listener].new(lc) }
  @threadpool = ThreadPool.new(@config[:threadpool_size]) if @config[:threadpool_size] > 1

  @logger.info("Plum #{::Plum::VERSION}")
  @logger.info("Config: #{config}")

  if @config[:user]
    drop_privileges
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/plum/rack/server.rb', line 5

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/plum/rack/server.rb', line 5

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/plum/rack/server.rb', line 5

def logger
  @logger
end

#threadpoolObject (readonly)

Returns the value of attribute threadpool.



5
6
7
# File 'lib/plum/rack/server.rb', line 5

def threadpool
  @threadpool
end

Instance Method Details

#log_exception(e) ⇒ Object



54
55
56
# File 'lib/plum/rack/server.rb', line 54

def log_exception(e)
  @logger.error("#{e.class}: #{e.message}\n#{e.backtrace.map { |b| "\t#{b}" }.join("\n")}")
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plum/rack/server.rb', line 23

def start
  #trap(:INT) { @state = :ee }
  #require "lineprof"
  #Lineprof.profile(//){
  @state = :running
  while @state == :running && !@listeners.empty?
    begin
      if ss = IO.select(@listeners, nil, nil, 2.0)
        ss[0].each { |svr|
          begin
            svr.accept(self)
          rescue Errno::ECONNRESET, Errno::ECONNABORTED # closed
          rescue => e
            log_exception(e)
          end
        }
      end
    rescue Errno::EBADF # closed
    rescue => e
      log_exception(e)
    end
  end
  #}
end

#stopObject



48
49
50
51
52
# File 'lib/plum/rack/server.rb', line 48

def stop
  @state = :stop
  @listeners.map(&:stop)
  # TODO: gracefully shutdown connections (wait threadpool?)
end