lib/pitchfork/worker.rb in pitchfork-0.2.0 vs lib/pitchfork/worker.rb in pitchfork-0.3.0

- old
+ new

@@ -5,11 +5,11 @@ # This class and its members can be considered a stable interface # and will not change in a backwards-incompatible fashion between # releases of pitchfork. Knowledge of this class is generally not # not needed for most users of pitchfork. # - # Some users may want to access it in the after_promotion/after_fork hooks. + # Some users may want to access it in the after_worker_fork/after_mold_fork hooks. # See the Pitchfork::Configurator RDoc for examples. class Worker # :stopdoc: EXIT_SIGNALS = [:QUIT, :TERM] attr_accessor :nr, :pid, :generation @@ -40,35 +40,41 @@ def exiting? @exiting end + def pending? + @master.nil? + end + def outdated? CURRENT_GENERATION_DROP[0] > @generation end def update(message) message.class.members.each do |member| send("#{member}=", message.public_send(member)) end - - case message - when Message::WorkerPromoted - promoted! - end end def register_to_master(control_socket) create_socketpair! - message = Message::WorkerSpawned.new(@nr, Process.pid, generation, @master) + message = Message::WorkerSpawned.new(@nr, @pid, generation, @master) control_socket.sendmsg(message) @master.close end - def declare_promotion(control_socket) - message = Message::WorkerPromoted.new(@nr, Process.pid, generation) + def start_promotion(control_socket) + create_socketpair! + message = Message::MoldSpawned.new(@nr, @pid, generation, @master) control_socket.sendmsg(message) + @master.close + end + + def finish_promotion(control_socket) + message = Message::MoldReady.new(@nr, @pid, generation) + control_socket.sendmsg(message) CURRENT_GENERATION_DROP[0] = @generation end def promote(generation) send_message_nonblock(Message::PromoteWorker.new(generation)) @@ -196,11 +202,11 @@ def create_socketpair! @to_io, @master = Pitchfork.socketpair end def after_fork_in_child - @master.close + @master&.close end private def pipe=(socket) @@ -213,10 +219,10 @@ case @master.sendmsg_nonblock(message, exception: false) when :wait_writable else success = true end - rescue Errno::EPIPE + rescue Errno::EPIPE, Errno::ECONNRESET # worker will be reaped soon end success end