Class: Pitchfork::Children
- Inherits:
-
Object
- Object
- Pitchfork::Children
- Defined in:
- lib/pitchfork/children.rb
Overview
This class keep tracks of the state of all the master children.
Instance Attribute Summary collapse
-
#last_generation ⇒ Object
Returns the value of attribute last_generation.
-
#mold ⇒ Object
readonly
Returns the value of attribute mold.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #abandon(worker) ⇒ Object
- #each(&block) ⇒ Object
- #each_worker(&block) ⇒ Object
- #empty? ⇒ Boolean
- #fetch(pid) ⇒ Object
- #fresh_workers ⇒ Object
- #hard_kill(sig, child) ⇒ Object
- #hard_kill_all(sig) ⇒ Object
-
#initialize ⇒ Children
constructor
A new instance of Children.
- #molds ⇒ Object
- #nr_alive?(nr) ⇒ Boolean
- #pending_promotion? ⇒ Boolean
- #pending_workers? ⇒ Boolean
- #promote(worker) ⇒ Object
- #reap(pid) ⇒ Object
- #register(child) ⇒ Object
- #register_mold(mold) ⇒ Object
- #register_service(service) ⇒ Object
- #restarting_workers_count ⇒ Object
- #soft_kill_all(sig) ⇒ Object
- #update(message) ⇒ Object
- #workers ⇒ Object
- #workers_count ⇒ Object
Constructor Details
#initialize ⇒ Children
Returns a new instance of Children.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/pitchfork/children.rb', line 10 def initialize @last_generation = 0 @children = {} # All children, including molds and services, indexed by PID. @workers = {} # Workers indexed by their `nr`. @molds = {} # Molds, index by PID. @mold = nil # The latest mold, if any. @service = nil @pending_workers = {} # Pending workers indexed by their `nr`. @pending_molds = {} # Worker promoted to mold, not yet acknowledged end |
Instance Attribute Details
#last_generation ⇒ Object
Returns the value of attribute last_generation.
8 9 10 |
# File 'lib/pitchfork/children.rb', line 8 def last_generation @last_generation end |
#mold ⇒ Object (readonly)
Returns the value of attribute mold.
7 8 9 |
# File 'lib/pitchfork/children.rb', line 7 def mold @mold end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
7 8 9 |
# File 'lib/pitchfork/children.rb', line 7 def service @service end |
Instance Method Details
#abandon(worker) ⇒ Object
78 79 80 81 |
# File 'lib/pitchfork/children.rb', line 78 def abandon(worker) @workers.delete(worker.nr) @pending_workers.delete(worker.nr) end |
#each(&block) ⇒ Object
128 129 130 |
# File 'lib/pitchfork/children.rb', line 128 def each(&block) @children.each_value(&block) end |
#each_worker(&block) ⇒ Object
132 133 134 |
# File 'lib/pitchfork/children.rb', line 132 def each_worker(&block) @workers.each_value(&block) end |
#empty? ⇒ Boolean
124 125 126 |
# File 'lib/pitchfork/children.rb', line 124 def empty? @children.empty? end |
#fetch(pid) ⇒ Object
38 39 40 |
# File 'lib/pitchfork/children.rb', line 38 def fetch(pid) @children.fetch(pid) end |
#fresh_workers ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/pitchfork/children.rb', line 159 def fresh_workers if @mold workers.select { |w| w.generation >= @mold.generation } else workers end end |
#hard_kill(sig, child) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/pitchfork/children.rb', line 142 def hard_kill(sig, child) child.hard_kill(sig) rescue Errno::ESRCH reap(child.pid) child.close end |
#hard_kill_all(sig) ⇒ Object
149 150 151 152 153 |
# File 'lib/pitchfork/children.rb', line 149 def hard_kill_all(sig) each do |child| hard_kill(sig, child) end end |
#molds ⇒ Object
120 121 122 |
# File 'lib/pitchfork/children.rb', line 120 def molds @molds.values end |
#nr_alive?(nr) ⇒ Boolean
74 75 76 |
# File 'lib/pitchfork/children.rb', line 74 def nr_alive?(nr) @workers.key?(nr) end |
#pending_promotion? ⇒ Boolean
116 117 118 |
# File 'lib/pitchfork/children.rb', line 116 def pending_promotion? !@pending_molds.empty? end |
#pending_workers? ⇒ Boolean
108 109 110 |
# File 'lib/pitchfork/children.rb', line 108 def pending_workers? !(@pending_workers.empty? && @pending_molds.empty?) end |
#promote(worker) ⇒ Object
104 105 106 |
# File 'lib/pitchfork/children.rb', line 104 def promote(worker) worker.promote(self.last_generation += 1) end |
#reap(pid) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pitchfork/children.rb', line 83 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 if @service == child @service = nil end end child end |
#register(child) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/pitchfork/children.rb', line 21 def register(child) # Children always start as workers, never molds, so we know they have a `#nr`. unless child.nr raise "[BUG] Trying to register a child without an `nr`: #{child.inspect}" end @pending_workers[child.nr] = @workers[child.nr] = child end |
#register_mold(mold) ⇒ Object
33 34 35 36 |
# File 'lib/pitchfork/children.rb', line 33 def register_mold(mold) @pending_molds[mold.pid] = mold @children[mold.pid] = mold end |
#register_service(service) ⇒ Object
29 30 31 |
# File 'lib/pitchfork/children.rb', line 29 def register_service(service) @service = service end |
#restarting_workers_count ⇒ Object
112 113 114 |
# File 'lib/pitchfork/children.rb', line 112 def restarting_workers_count @pending_workers.size + @workers.count { |_, w| w.exiting? } end |
#soft_kill_all(sig) ⇒ Object
136 137 138 139 140 |
# File 'lib/pitchfork/children.rb', line 136 def soft_kill_all(sig) each do |child| child.soft_kill(sig) end end |
#update(message) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pitchfork/children.rb', line 42 def update() case when Message::MoldSpawned mold = Worker.new(nil) mold.update() @pending_molds[mold.pid] = mold @children[mold.pid] = mold return mold when Message::ServiceSpawned service = @service service.update() @children[service.pid] = service return service end child = @children[.pid] || (.nr && @workers[.nr]) child.update() 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 |
#workers ⇒ Object
155 156 157 |
# File 'lib/pitchfork/children.rb', line 155 def workers @workers.values end |
#workers_count ⇒ Object
167 168 169 |
# File 'lib/pitchfork/children.rb', line 167 def workers_count @workers.size end |