Module: Crabfarm::Support::CustomPuma

Defined in:
lib/crabfarm/support/custom_puma.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :Host => '0.0.0.0',
  :Port => 3100,
  :Threads => '0:16',
  :Verbose => false
}

Class Method Summary collapse

Class Method Details

.run(_app, _options = {}) ⇒ Object



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
44
45
46
47
48
49
# File 'lib/crabfarm/support/custom_puma.rb', line 15

def self.run(_app, _options = {})

  _options  = DEFAULT_OPTIONS.merge(_options)

  _app = Rack::CommonLogger.new(_app, STDOUT) if _options[:Verbose]

  ENV['RACK_ENV'] = _options[:environment].to_s if _options[:environment]

  server   = Puma::Server.new(_app)
  min, max = _options[:Threads].split(':', 2)

  puts "Puma #{::Puma::Const::PUMA_VERSION} starting..."
  puts "* Min threads: #{min}, max threads: #{max}"
  puts "* Environment: #{ENV['RACK_ENV']}"

  if _options[:Host].start_with? 'unix://'
    puts "* Listening on #{_options[:Host]}"
    server.add_unix_listener _options[:Host][7..-1]
  else
    puts "* Listening on tcp://#{_options[:Host]}:#{_options[:Port]}"
    server.add_tcp_listener _options[:Host], _options[:Port]
  end

  server.min_threads = min
  server.max_threads = max

  begin
    server.run.join
  rescue Interrupt
    puts "* Gracefully stopping, waiting for requests to finish"
    server.stop(true)
    puts "* Goodbye!"
  end

end

.valid_optionsObject



51
52
53
54
55
56
57
58
# File 'lib/crabfarm/support/custom_puma.rb', line 51

def self.valid_options
  {
    "Host=HOST"       => "Hostname to listen on (default: localhost), also supports unix sockets if prefixed with unix://",
    "Port=PORT"       => "Port to listen on (default: 8080)",
    "Threads=MIN:MAX" => "min:max threads to use (default 0:16)",
    "Quiet"           => "Don't report each request"
  }
end