Class: Puma::Runner

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

Direct Known Subclasses

Cluster, Single

Instance Method Summary collapse

Constructor Details

#initialize(cli) ⇒ Runner

Returns a new instance of Runner



3
4
5
6
7
8
# File 'lib/puma/runner.rb', line 3

def initialize(cli)
  @cli = cli
  @options = cli.options
  @app = nil
  @control = nil
end

Instance Method Details

#appObject



122
123
124
# File 'lib/puma/runner.rb', line 122

def app
  @app ||= @cli.config.app
end

#before_restartObject



26
27
28
# File 'lib/puma/runner.rb', line 26

def before_restart
  @control.stop(true) if @control
end

#daemon?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/puma/runner.rb', line 10

def daemon?
  @options[:daemon]
end

#development?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/puma/runner.rb', line 14

def development?
  @options[:environment] == "development"
end

#error(str) ⇒ Object



22
23
24
# File 'lib/puma/runner.rb', line 22

def error(str)
  @cli.error str
end

#load_and_bindObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/puma/runner.rb', line 105

def load_and_bind
  unless @cli.config.app_configured?
    error "No application configured, nothing to run"
    exit 1
  end

  # Load the app before we daemonize.
  begin
    @app = @cli.config.app
  rescue Exception => e
    log "! Unable to load application: #{e.class}: #{e.message}"
    raise e
  end

  @cli.binder.parse @options[:binds], self
end

#log(str) ⇒ Object



18
19
20
# File 'lib/puma/runner.rb', line 18

def log(str)
  @cli.log str
end

#output_header(mode) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puma/runner.rb', line 73

def output_header(mode)
  min_t = @options[:min_threads]
  max_t = @options[:max_threads]

  log "Puma starting in #{mode} mode..."
  log "* Version #{Puma::Const::PUMA_VERSION} (#{ruby_engine}), codename: #{Puma::Const::CODE_NAME}"
  log "* Min threads: #{min_t}, max threads: #{max_t}"
  log "* Environment: #{ENV['RACK_ENV']}"

  if @options[:mode] == :tcp
    log "* Mode: Lopez Express (tcp)"
  end
end

#redirect_ioObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/puma/runner.rb', line 87

def redirect_io
  stdout = @options[:redirect_stdout]
  stderr = @options[:redirect_stderr]
  append = @options[:redirect_append]

  if stdout
    STDOUT.reopen stdout, (append ? "a" : "w")
    STDOUT.sync = true
    STDOUT.puts "=== puma startup: #{Time.now} ==="
  end

  if stderr
    STDERR.reopen stderr, (append ? "a" : "w")
    STDERR.sync = true
    STDERR.puts "=== puma startup: #{Time.now} ==="
  end
end

#ruby_engineObject



65
66
67
68
69
70
71
# File 'lib/puma/runner.rb', line 65

def ruby_engine
  if !defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby"
    "ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
  else
    "#{RUBY_ENGINE} #{RUBY_VERSION}"
  end
end

#start_controlObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puma/runner.rb', line 30

def start_control
  str = @options[:control_url]
  return unless str

  require 'puma/app/status'

  uri = URI.parse str

  app = Puma::App::Status.new @cli

  if token = @options[:control_auth_token]
    app.auth_token = token unless token.empty? or token == :none
  end

  control = Puma::Server.new app, @cli.events
  control.min_threads = 0
  control.max_threads = 1

  case uri.scheme
  when "tcp"
    log "* Starting control server on #{str}"
    control.add_tcp_listener uri.host, uri.port
  when "unix"
    log "* Starting control server on #{str}"
    path = "#{uri.host}#{uri.path}"

    control.add_unix_listener path
  else
    error "Invalid control URI: #{str}"
  end

  control.run
  @control = control
end

#start_serverObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/puma/runner.rb', line 126

def start_server
  min_t = @options[:min_threads]
  max_t = @options[:max_threads]

  server = Puma::Server.new app, @cli.events, @options
  server.min_threads = min_t
  server.max_threads = max_t
  server.inherit_binder @cli.binder

  if @options[:mode] == :tcp
    server.tcp_mode!
  end

  unless development?
    server.leak_stack_on_error = false
  end

  server
end