Class: Puma::Single

Inherits:
Runner show all
Defined in:
lib/puma/single.rb

Overview

This class is instantiated by the ‘Puma::Launcher` and used to boot and serve a Ruby application when no puma “workers” are needed i.e. only using “threaded” mode. For example `$ puma -t 1:5`

At the core of this class is running an instance of ‘Puma::Server` which gets created via the `start_server` method from the `Puma::Runner` class that this inherits from.

Instance Method Summary collapse

Methods inherited from Runner

#app, #before_restart, #daemon?, #debug, #development?, #error, #initialize, #load_and_bind, #log, #output_header, #redirect_io, #redirected_io?, #ruby_engine, #start_control, #start_server, #test?

Constructor Details

This class inherits a constructor from Puma::Runner

Instance Method Details

#haltObject



32
33
34
# File 'lib/puma/single.rb', line 32

def halt
  @server.halt
end

#jruby_daemon?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/puma/single.rb', line 42

def jruby_daemon?
  daemon? and Puma.jruby?
end

#jruby_daemon_startObject



46
47
48
49
# File 'lib/puma/single.rb', line 46

def jruby_daemon_start
  require 'puma/jruby_restart'
  JRubyRestart.daemon_start(@restart_dir, @launcher.restart_args)
end

#restartObject



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

def restart
  @server.begin_restart
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puma/single.rb', line 51

def run
  already_daemon = false

  if jruby_daemon?
    require 'puma/jruby_restart'

    if JRubyRestart.daemon?
      # load and bind before redirecting IO so errors show up on stdout/stderr
      load_and_bind
      redirect_io
    end

    already_daemon = JRubyRestart.daemon_init
  end

  output_header "single"

  if jruby_daemon?
    if already_daemon
      JRubyRestart.perm_daemonize
    else
      pid = nil

      Signal.trap "SIGUSR2" do
        log "* Started new process #{pid} as daemon..."

        # Must use exit! so we don't unwind and run the ensures
        # that will be run by the new child (such as deleting the
        # pidfile)
        exit!(true)
      end

      Signal.trap "SIGCHLD" do
        log "! Error starting new process as daemon, exiting"
        exit 1
      end

      jruby_daemon_start
      sleep
    end
  else
    if daemon?
      log "* Daemonizing..."
      Process.daemon(true)
      redirect_io
    end

    load_and_bind
  end

  Plugins.fire_background

  @launcher.write_state

  start_control

  @server = server = start_server

  unless daemon?
    log "Use Ctrl-C to stop"
    redirect_io
  end

  @launcher.events.fire_on_booted!

  begin
    server.run.join
  rescue Interrupt
    # Swallow it
  end
end

#statsObject



16
17
18
19
20
21
22
# File 'lib/puma/single.rb', line 16

def stats
  b = @server.backlog || 0
  r = @server.running || 0
  t = @server.pool_capacity || 0
  m = @server.max_threads || 0
  %Q!{ "backlog": #{b}, "running": #{r}, "pool_capacity": #{t}, "max_threads": #{m} }!
end

#stopObject



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

def stop
  @server.stop false
end

#stop_blockedObject



36
37
38
39
40
# File 'lib/puma/single.rb', line 36

def stop_blocked
  log "- Gracefully stopping, waiting for requests to finish"
  @control.stop(true) if @control
  @server.stop(true)
end