Class: Puma::Runner

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

Overview

Generic class that is used by ‘Puma::Cluster` and `Puma::Single` to serve requests. This class spawns a new instance of `Puma::Server` via a call to `start_server`.

Direct Known Subclasses

Cluster, Cluster::Worker, Single

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli, events) ⇒ Runner

Returns a new instance of Runner.



11
12
13
14
15
16
17
18
# File 'lib/puma/runner.rb', line 11

def initialize(cli, events)
  @launcher = cli
  @events = events
  @options = cli.options
  @app = nil
  @control = nil
  @started_at = Time.now
end

Instance Attribute Details

#appObject (readonly)



153
154
155
# File 'lib/puma/runner.rb', line 153

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

#ruby_engineObject (readonly)



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

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

Instance Method Details

#close_control_listenersObject

Version:

  • 5.0.0



67
68
69
# File 'lib/puma/runner.rb', line 67

def close_control_listeners
  @control.binder.close_listeners if @control
end

#debug(str) ⇒ Object



41
42
43
# File 'lib/puma/runner.rb', line 41

def debug(str)
  @events.log "- #{str}" if @options[:debug]
end

#development?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/puma/runner.rb', line 20

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

#error(str) ⇒ Object



37
38
39
# File 'lib/puma/runner.rb', line 37

def error(str)
  @events.error str
end

#load_and_bindObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/puma/runner.rb', line 136

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

  begin
    @app = @launcher.config.app
  rescue Exception => e
    log "! Unable to load application: #{e.class}: #{e.message}"
    raise e
  end

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

#log(str) ⇒ Object



28
29
30
# File 'lib/puma/runner.rb', line 28

def log(str)
  @events.log str
end

#output_header(mode) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puma/runner.rb', line 84

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

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

  if mode == "cluster"
    log "*   Master PID: #{Process.pid}"
  else
    log "*          PID: #{Process.pid}"
  end
end

#redirect_ioObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puma/runner.rb', line 105

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

  if stdout
    unless Dir.exist?(File.dirname(stdout))
      raise "Cannot redirect STDOUT to #{stdout}"
    end

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

  if stderr
    unless Dir.exist?(File.dirname(stderr))
      raise "Cannot redirect STDERR to #{stderr}"
    end

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

  if @options[:mutate_stdout_and_stderr_to_sync_on_write]
    STDOUT.sync = true
    STDERR.sync = true
  end
end

#redirected_io?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/puma/runner.rb', line 101

def redirected_io?
  @options[:redirect_stdout] || @options[:redirect_stderr]
end

#start_controlObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puma/runner.rb', line 45

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

  require 'puma/app/status'

  if token = @options[:control_auth_token]
    token = nil if token.empty? || token == 'none'
  end

  app = Puma::App::Status.new @launcher, token

  control = Puma::Server.new app, @launcher.events,
    { min_threads: 0, max_threads: 1, queue_requests: false }

  control.binder.parse [str], self, 'Starting control server'

  control.run thread_name: 'control'
  @control = control
end

#start_serverObject



157
158
159
160
161
# File 'lib/puma/runner.rb', line 157

def start_server
  server = Puma::Server.new app, @launcher.events, @options
  server.inherit_binder @launcher.binder
  server
end

#stop_controlObject

Version:

  • 5.0.0



33
34
35
# File 'lib/puma/runner.rb', line 33

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

#test?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/puma/runner.rb', line 24

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