Class: Pitchfork::Children

Inherits:
Object
  • Object
show all
Defined in:
lib/pitchfork/children.rb

Overview

This class keep tracks of the state of all the master children.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChildren

Returns a new instance of Children.



10
11
12
13
14
15
16
17
18
# File 'lib/pitchfork/children.rb', line 10

def initialize
  @last_generation = 0
  @children = {} # All children, including molds, indexed by PID.
  @workers = {} # Workers indexed by their `nr`.
  @molds = {} # Molds, index by PID.
  @mold = nil # The latest mold, if any.
  @pending_workers = {} # Pending workers indexed by their `nr`.
  @pending_molds = {} # Worker promoted to mold, not yet acknowledged
end

Instance Attribute Details

#last_generationObject

Returns the value of attribute last_generation.



8
9
10
# File 'lib/pitchfork/children.rb', line 8

def last_generation
  @last_generation
end

#moldObject (readonly)

Returns the value of attribute mold.



7
8
9
# File 'lib/pitchfork/children.rb', line 7

def mold
  @mold
end

Instance Method Details

#abandon(worker) ⇒ Object



70
71
72
73
# File 'lib/pitchfork/children.rb', line 70

def abandon(worker)
  @workers.delete(worker.nr)
  @pending_workers.delete(worker.nr)
end

#each(&block) ⇒ Object



115
116
117
# File 'lib/pitchfork/children.rb', line 115

def each(&block)
  @children.each_value(&block)
end

#each_worker(&block) ⇒ Object



119
120
121
# File 'lib/pitchfork/children.rb', line 119

def each_worker(&block)
  @workers.each_value(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/pitchfork/children.rb', line 111

def empty?
  @children.empty?
end

#fetch(pid) ⇒ Object



35
36
37
# File 'lib/pitchfork/children.rb', line 35

def fetch(pid)
  @children.fetch(pid)
end

#fresh_workersObject



146
147
148
149
150
151
152
# File 'lib/pitchfork/children.rb', line 146

def fresh_workers
  if @mold
    workers.select { |w| w.generation >= @mold.generation }
  else
    workers
  end
end

#hard_kill(sig, child) ⇒ Object



129
130
131
132
133
134
# File 'lib/pitchfork/children.rb', line 129

def hard_kill(sig, child)
  child.hard_kill(sig)
rescue Errno::ESRCH
  reap(child.pid)
  child.close
end

#hard_kill_all(sig) ⇒ Object



136
137
138
139
140
# File 'lib/pitchfork/children.rb', line 136

def hard_kill_all(sig)
  each do |child|
    hard_kill(sig, child)
  end
end

#moldsObject



107
108
109
# File 'lib/pitchfork/children.rb', line 107

def molds
  @molds.values
end

#nr_alive?(nr) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/pitchfork/children.rb', line 66

def nr_alive?(nr)
  @workers.key?(nr)
end

#pending_promotion?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/pitchfork/children.rb', line 103

def pending_promotion?
  !@pending_molds.empty?
end

#pending_workers?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/pitchfork/children.rb', line 95

def pending_workers?
  !(@pending_workers.empty? && @pending_molds.empty?)
end

#promote(worker) ⇒ Object



91
92
93
# File 'lib/pitchfork/children.rb', line 91

def promote(worker)
  worker.promote(self.last_generation += 1)
end

#reap(pid) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pitchfork/children.rb', line 75

def reap(pid)
  if child = @children.delete(pid)
    @pending_workers.delete(child.nr)
    @pending_molds.delete(child.pid)
    @molds.delete(child.pid)
    @workers.delete(child.nr)
    if @mold == child
      @pending_workers.reject! do |nr, worker|
        worker.generation == @mold.generation
      end
      @mold = nil
    end
  end
  child
end

#refreshObject



20
21
22
23
# File 'lib/pitchfork/children.rb', line 20

def refresh
  @workers.each_value(&:refresh)
  @molds.each_value(&:refresh)
end

#register(child) ⇒ Object



25
26
27
28
# File 'lib/pitchfork/children.rb', line 25

def register(child)
  # Children always start as workers, never molds, so we know they have a `#nr`.
  @pending_workers[child.nr] = @workers[child.nr] = child
end

#register_mold(mold) ⇒ Object



30
31
32
33
# File 'lib/pitchfork/children.rb', line 30

def register_mold(mold)
  @pending_molds[mold.pid] = mold
  @children[mold.pid] = mold
end

#restarting_workers_countObject



99
100
101
# File 'lib/pitchfork/children.rb', line 99

def restarting_workers_count
  @pending_workers.size + @workers.count { |_, w| w.exiting? }
end

#soft_kill_all(sig) ⇒ Object



123
124
125
126
127
# File 'lib/pitchfork/children.rb', line 123

def soft_kill_all(sig)
  each do |child|
    child.soft_kill(sig)
  end
end

#total_pssObject



158
159
160
161
162
163
164
# File 'lib/pitchfork/children.rb', line 158

def total_pss
  total_pss = MemInfo.new(Process.pid).pss
  @children.each do |_, worker|
    total_pss += worker.meminfo.pss if worker.meminfo
  end
  total_pss
end

#update(message) ⇒ Object



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
64
# File 'lib/pitchfork/children.rb', line 39

def update(message)
  case message
  when Message::MoldSpawned
    mold = Worker.new(nil)
    mold.update(message)
    @pending_molds[mold.pid] = mold
    @children[mold.pid] = mold
    return mold
  end

  child = @children[message.pid] || (message.nr && @workers[message.nr])
  child.update(message)

  if child.mold?
    @pending_molds.delete(child.pid)
    @molds[child.pid] = child
    @mold = child
  end

  if child.pid
    @children[child.pid] = child
    @pending_workers.delete(child.nr)
  end

  child
end

#workersObject



142
143
144
# File 'lib/pitchfork/children.rb', line 142

def workers
  @workers.values
end

#workers_countObject



154
155
156
# File 'lib/pitchfork/children.rb', line 154

def workers_count
  @workers.size
end