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.



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

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.



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

def last_generation
  @last_generation
end

#moldObject (readonly)

Returns the value of attribute mold.



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

def mold
  @mold
end

Instance Method Details

#abandon(worker) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#each_worker(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @children.empty?
end

#fetch(pid) ⇒ Object



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

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

#fresh_workersObject



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

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

#hard_kill(sig, child) ⇒ Object



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

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

#hard_kill_all(sig) ⇒ Object



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

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

#moldsObject



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

def molds
  @molds.values
end

#nr_alive?(nr) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#pending_promotion?Boolean

Returns:

  • (Boolean)


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

def pending_promotion?
  !@pending_molds.empty?
end

#pending_workers?Boolean

Returns:

  • (Boolean)


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

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

#promote(worker) ⇒ Object



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

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

#reap(pid) ⇒ Object



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

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



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

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

#register(child) ⇒ Object



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

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



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

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

#restarting_workers_countObject



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

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

#soft_kill_all(sig) ⇒ Object



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

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

#total_pssObject



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

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



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/pitchfork/children.rb', line 38

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



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

def workers
  @workers.values
end

#workers_countObject



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

def workers_count
  @workers.size
end