Class: Zold::Farm
- Inherits:
-
Object
- Object
- Zold::Farm
- Defined in:
- lib/zold/node/farm.rb
Overview
Farm
Defined Under Namespace
Classes: Empty
Instance Method Summary collapse
- #best ⇒ Object
-
#initialize(invoice, cache = File.join(Dir.pwd, 'farm'), log: Log::Quiet.new) ⇒ Farm
constructor
A new instance of Farm.
- #start(host, port, strength: 8, threads: 8) ⇒ Object
- #to_json ⇒ Object
- #to_text ⇒ Object
Constructor Details
#initialize(invoice, cache = File.join(Dir.pwd, 'farm'), log: Log::Quiet.new) ⇒ Farm
Returns a new instance of Farm.
46 47 48 49 50 51 52 53 |
# File 'lib/zold/node/farm.rb', line 46 def initialize(invoice, cache = File.join(Dir.pwd, 'farm'), log: Log::Quiet.new) @log = log @cache = cache @invoice = invoice @pipeline = Queue.new @threads = [] @mutex = Mutex.new end |
Instance Method Details
#best ⇒ Object
55 56 57 |
# File 'lib/zold/node/farm.rb', line 55 def best load end |
#start(host, port, strength: 8, threads: 8) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/zold/node/farm.rb', line 78 def start(host, port, strength: 8, threads: 8) @log.info('Zero-threads farm won\'t score anything!') if threads.zero? cleanup(host, port, strength, threads) @log.info("#{@pipeline.size} scores pre-loaded, the best is: #{best[0]}") @alive = true @threads = (1..threads).map do |t| Thread.new do Thread.current.abort_on_exception = true Thread.current.name = "f#{t}" loop do VerboseThread.new(@log).run do cycle(host, port, strength, threads) end break unless @alive end end end @cleanup = Thread.new do Thread.current.abort_on_exception = true Thread.current.name = 'cleanup' loop do max = 600 a = (0..max).take_while do sleep 0.1 @alive end unless a.count == max @log.info("It's time to stop the cleanup thread (#{a.count} != #{max}, alive=#{@alive})...") break end VerboseThread.new(@log).run(true) do cleanup(host, port, strength, threads) end end end @log.info("Farm started with #{@threads.count} threads at #{host}:#{port}, strength is #{strength}") return unless block_given? begin yield(self) ensure @log.info("Terminating the farm with #{@threads.count} threads...") start = Time.now finish(@cleanup) @threads.each { |t| finish(t) } @log.info("Farm stopped in #{Age.new(start)}") end end |
#to_json ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/zold/node/farm.rb', line 66 def to_json { threads: @threads.map do |t| "#{t.name}/#{t.status}/#{t.alive? ? 'A' : 'D'}" end.join(', '), cleanup: @cleanup.status, pipeline: @pipeline.size, best: best.map(&:to_mnemo).join(', '), alive: @alive } end |
#to_text ⇒ Object
59 60 61 62 63 64 |
# File 'lib/zold/node/farm.rb', line 59 def to_text @threads.map do |t| trace = t.backtrace || [] "#{t.name}: status=#{t.status}; alive=#{t.alive?};\n #{trace.join("\n ")}" end.join("\n") end |