Class: Thrifty::HTTP::Server::PumaServer

Inherits:
Object
  • Object
show all
Defined in:
lib/thrifty/http_server/puma_server.rb

Constant Summary collapse

DEFAULT_PORT =
ENV['PORT'].to_i > 0 ? ENV['PORT'] : 8080
DEFAULT_IP =
'0.0.0.0'
DEFAULT_MIN_TH =
20
DEFAULT_MAX_TH =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PumaServer

Returns a new instance of PumaServer.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/thrifty/http_server/puma_server.rb', line 13

def initialize(options={})
  port  = options[:port] || DEFAULT_PORT
  ip    = options[:ip]   || DEFAULT_IP
  min   = options[:min]  || DEFAULT_MIN_TH
  max   = options[:max]  || DEFAULT_MAX_TH
  name  = options[:name] || 'Puma'

  @log  = Thrifty.get_logger(name)
  @bind = "#{ip}:#{port}"
  @lock = Mutex.new

  app = ::Rack::Builder.new do
    if options[:err] != false
      use ErrMiddleware, name
    end

    if options[:log] != false
      use LogMiddleware, name
    end

    use Rack::Lint
    yield self
  end

  @server = Puma::Server.new(app)
  @server.add_tcp_listener ip, port
  @server.min_threads = min
  @server.max_threads = max

  Thrifty::Signals.register(method(:stop))
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



6
7
8
# File 'lib/thrifty/http_server/puma_server.rb', line 6

def log
  @log
end

Instance Method Details

#startObject



45
46
47
48
49
50
51
52
# File 'lib/thrifty/http_server/puma_server.rb', line 45

def start
  @lock.synchronize do
    unless @thread
      log.info "starting", version: Puma::Server::VERSION, bind: @bind, threads: "#{@server.min_threads}:#{@server.max_threads}"
      @thread = @server.run
    end
  end
end

#stopObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/thrifty/http_server/puma_server.rb', line 54

def stop
  @lock.synchronize do
    if @thread
      log.info "stopping"
      @server.stop(true)
      @thread.join
      @thread = nil
      log.info "stopped"
    end
  end
end