lib/zold/node/farmers.rb in zold-0.17.0 vs lib/zold/node/farmers.rb in zold-0.17.1

- old
+ new

@@ -33,10 +33,18 @@ # Copyright:: Copyright (c) 2018 Yegor Bugayenko # License:: MIT module Zold # Farmer module Farmers + # Kill a process + def self.kill(log, pid, start) + Process.kill('KILL', pid) + log.debug("Process ##{pid} killed after #{Age.new(start)} of activity") + rescue StandardError => e + log.debug("No need to kill process ##{pid} since it's dead already: #{e.message}") + end + # Plain and simple class Plain def initialize(log: Log::NULL) @log = log end @@ -69,11 +77,11 @@ 'next', Shellwords.escape(score) ].join(' ') Open3.popen2e(cmd) do |stdin, stdout, thr| Thread.current.thread_variable_set(:pid, thr.pid.to_s) - at_exit { kill(thr.pid, start) } + at_exit { Farmers.kill(@log, thr.pid, start) } @log.debug("Scoring started in proc ##{thr.pid} \ for #{score.value}/#{score.strength} at #{score.host}:#{score.port}") begin stdin.close buffer = +'' @@ -99,23 +107,14 @@ after = Score.parse(buffer.strip) @log.debug("Next score #{after.value}/#{after.strength} found in proc ##{thr.pid} \ for #{after.host}:#{after.port} in #{Age.new(start)}: #{after.suffixes}") after ensure - kill(thr.pid, start) + Farmers.kill(@log, thr.pid, start) end end end - - private - - def kill(pid, start) - Process.kill('KILL', pid) - @log.debug("Process ##{pid} killed after #{Age.new(start)} of activity") - rescue StandardError => e - @log.debug("No need to kill process ##{pid} since it's dead already: #{e.message}") - end end # In a child process using fork class Fork def initialize(log: Log::NULL) @@ -126,10 +125,10 @@ start = Time.now stdin, stdout = IO.pipe pid = Process.fork do stdout.puts(score.next) end - at_exit { Process.kill('KILL', pid) } + at_exit { Farmers.kill(@log, pid, start) } Process.wait stdout.close after = Score.parse(stdin.read.strip) stdin.close @log.debug("Next score #{after.value}/#{after.strength} found in proc ##{pid} \