In: |
lib/slave.rb
|
Parent: | Object |
the Heartbeat class is essentially wrapper over an IPC channel that sends a ping on the channel indicating process health. if either end of the channel is detached the ping will fail and an error will be raised. in this was it is ensured that Slave object cannot continue to live without their parent being alive.
# File lib/slave.rb, line 206 206: def initialize pulse_rate = 4.2, debug = false 207: #--{{{ 208: @pulse_rate = Float pulse_rate 209: @debug = debug 210: @r, @w = IO::pipe 211: @pid = Process::pid 212: @ppid = Process::ppid 213: @cid = nil 214: @thread = nil 215: @ppid = nil 216: @whoami = nil 217: @beating = nil 218: @pipe = nil 219: #--}}} 220: end
# File lib/slave.rb, line 254 254: def child_start 255: #--{{{ 256: @whoami = 'child' 257: @pid = Process::pid 258: @ppid = Process::ppid 259: @thread = 260: Thread::new(Thread::current) do |cur| 261: begin 262: loop do 263: trace{ "<#{ @whoami }> <#{ @pid }> puts <#{ @pid }>" } 264: @pipe.puts @pid 265: Process::kill 0, @ppid 266: sleep @pulse_rate 267: end 268: rescue => e 269: cur.raise e 270: ensure 271: @pipe.close rescue nil 272: end 273: end 274: #--}}} 275: end
# File lib/slave.rb, line 235 235: def parent_start 236: #--{{{ 237: @whoami = 'parent' 238: @thread = 239: Thread::new(Thread::current) do |cur| 240: begin 241: loop do 242: buf = @pipe.gets 243: trace{ "<#{ @whoami }> <#{ @pid }> gets <#{ buf.inspect }>" } 244: @cid = Integer buf.strip if @cid.nil? and buf =~ %/^\s*\d+\s*$/ 245: end 246: rescue => e 247: cur.raise e 248: ensure 249: @pipe.close rescue nil 250: end 251: end 252: #--}}} 253: end
# File lib/slave.rb, line 221 221: def start 222: #--{{{ 223: if Process::pid == @pid 224: @w.close 225: @pipe = @r 226: parent_start 227: else 228: @r.close 229: @pipe = @w 230: child_start 231: end 232: @beating = true 233: #--}}} 234: end
# File lib/slave.rb, line 276 276: def stop 277: #--{{{ 278: raise "not beating" unless @beating 279: @thread.kill 280: @pipe.close rescue nil 281: @beating = false 282: #--}}} 283: end