Class: Zold::Farmers::Fork

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/node/farmers.rb

Overview

In a child process using fork

Instance Method Summary collapse

Constructor Details

#initialize(log: Log::NULL) ⇒ Fork

Returns a new instance of Fork.



58
59
60
# File 'lib/zold/node/farmers.rb', line 58

def initialize(log: Log::NULL)
  @log = log
end

Instance Method Details

#up(score) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zold/node/farmers.rb', line 62

def up(score)
  start = Time.now
  stdout, stdin = IO.pipe
  pid = Process.fork do
    stdin.puts(score.next)
  end
  at_exit { Farmers.kill(@log, pid, start) }
  Process.wait
  stdin.close
  text = stdout.read.strip
  stdout.close
  raise "No score was calculated in the process ##{pid} in #{Age.new(start)}" if text.empty?
  after = Score.parse(text)
  @log.debug("Next score #{after.value}/#{after.strength} found in proc ##{pid} \
for #{after.host}:#{after.port} in #{Age.new(start)}: #{after.suffixes}")
  after
end