Class: Puma::Cluster::WorkerHandle

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

Overview

This class represents a worker process from the perspective of the puma master process. It contains information about the process and its health and it exposes methods to control the process via IPC. It does not include the actual logic executed by the worker process itself. For that, see Puma::Cluster::Worker.

Constant Summary collapse

WORKER_MAX_KEYS =

array of stat ‘max’ keys

[:backlog_max, :reactor_max]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(idx, pid, phase, options) ⇒ WorkerHandle

Returns a new instance of WorkerHandle.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puma/cluster/worker_handle.rb', line 16

def initialize(idx, pid, phase, options)
  @index = idx
  @pid = pid
  @phase = phase
  @stage = :started
  @signal = "TERM"
  @options = options
  @first_term_sent = nil
  @started_at = Time.now
  @last_checkin = Time.now
  @last_status = {}
  @term = false
  @worker_max = Array.new WORKER_MAX_KEYS.length, 0
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



31
32
33
# File 'lib/puma/cluster/worker_handle.rb', line 31

def index
  @index
end

#last_checkinObject (readonly)

Returns the value of attribute last_checkin.



31
32
33
# File 'lib/puma/cluster/worker_handle.rb', line 31

def last_checkin
  @last_checkin
end

#last_statusObject (readonly)

Returns the value of attribute last_status.



31
32
33
# File 'lib/puma/cluster/worker_handle.rb', line 31

def last_status
  @last_status
end

#phaseObject

Returns the value of attribute phase.



31
32
33
# File 'lib/puma/cluster/worker_handle.rb', line 31

def phase
  @phase
end

#pidObject

Returns the value of attribute pid.



31
32
33
# File 'lib/puma/cluster/worker_handle.rb', line 31

def pid
  @pid
end

#signalObject (readonly)

Returns the value of attribute signal.



31
32
33
# File 'lib/puma/cluster/worker_handle.rb', line 31

def signal
  @signal
end

#started_atObject (readonly)

Returns the value of attribute started_at.



31
32
33
# File 'lib/puma/cluster/worker_handle.rb', line 31

def started_at
  @started_at
end

Instance Method Details

#boot!Object



44
45
46
47
# File 'lib/puma/cluster/worker_handle.rb', line 44

def boot!
  @last_checkin = Time.now
  @stage = :booted
end

#booted?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/puma/cluster/worker_handle.rb', line 36

def booted?
  @stage == :booted
end

#hupObject



121
122
123
124
# File 'lib/puma/cluster/worker_handle.rb', line 121

def hup
  Process.kill "HUP", @pid
rescue Errno::ESRCH
end

#killObject



116
117
118
119
# File 'lib/puma/cluster/worker_handle.rb', line 116

def kill
  @signal = 'KILL'
  term
end

#ping!(status) ⇒ Object



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
# File 'lib/puma/cluster/worker_handle.rb', line 57

def ping!(status)
  hsh = {}
  k, v = nil, nil
  status.tr('}{"', '').strip.split(", ") do |kv|
    cntr = 0
    kv.split(':') do |t|
      if cntr == 0
        k = t
        cntr = 1
      else
        v = t
      end
    end
    hsh[k.to_sym] = v.to_i
  end

  # check stat max values, we can't signal workers to reset the max values,
  # so we do so here
  WORKER_MAX_KEYS.each_with_index do |key, idx|
    next unless hsh[key]

    if hsh[key] < @worker_max[idx]
      hsh[key] = @worker_max[idx]
    else
      @worker_max[idx] = hsh[key]
    end
  end
  @last_checkin = Time.now
  @last_status = hsh
end

#ping_timeoutObject

See Also:

Version:

  • 5.0.0



95
96
97
98
99
100
101
# File 'lib/puma/cluster/worker_handle.rb', line 95

def ping_timeout
  @last_checkin +
    (booted? ?
      @options[:worker_timeout] :
      @options[:worker_boot_timeout]
    )
end

#reset_maxObject

Resets max values to zero. Called whenever ‘Cluster#stats` is called



89
90
91
# File 'lib/puma/cluster/worker_handle.rb', line 89

def reset_max
  WORKER_MAX_KEYS.length.times { |idx| @worker_max[idx] = 0 }
end

#termObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puma/cluster/worker_handle.rb', line 103

def term
  begin
    if @first_term_sent && (Time.now - @first_term_sent) > @options[:worker_shutdown_timeout]
      @signal = "KILL"
    else
      @term ||= true
      @first_term_sent ||= Time.now
    end
    Process.kill @signal, @pid if @pid
  rescue Errno::ESRCH
  end
end

#term!Object



49
50
51
# File 'lib/puma/cluster/worker_handle.rb', line 49

def term!
  @term = true
end

#term?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/puma/cluster/worker_handle.rb', line 53

def term?
  @term
end

#uptimeObject



40
41
42
# File 'lib/puma/cluster/worker_handle.rb', line 40

def uptime
  Time.now - started_at
end