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, 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, log: Log::Quiet.new) ⇒ Farm
Returns a new instance of Farm.
41 42 43 44 45 46 47 48 |
# File 'lib/zold/node/farm.rb', line 41 def initialize(invoice, cache, log: Log::Quiet.new) @log = log @cache = cache @invoice = invoice @pipeline = Queue.new @threads = [] @mutex = Mutex.new end |
Instance Method Details
#best ⇒ Object
50 51 52 |
# File 'lib/zold/node/farm.rb', line 50 def best load end |
#start(host, port, strength: 8, threads: 8) ⇒ Object
71 72 73 74 75 76 77 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 |
# File 'lib/zold/node/farm.rb', line 71 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]}") @threads = (1..threads).map do |t| Thread.new do Thread.current.name = "f#{t}" loop do VerboseThread.new(@log).run do cycle(host, port, strength, threads) end end end end alive = true @cleanup = Thread.new do Thread.current.name = 'cleanup' while alive sleep(60) unless strength == 1 # which will only happen in tests VerboseThread.new(@log).run 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 alive = false if strength == 1 @cleanup.join @log.info("Cleanup thread finished in #{(Time.now - start).round(2)}s") else @cleanup.exit @log.info("Cleanup thread killed in #{(Time.now - start).round(2)}s") end @threads.each do |t| tstart = Time.now t.exit @log.info("Thread #{t.name} terminated in #{(Time.now - tstart).round(2)}s") end @log.info("Farm stopped in #{(Time.now - start).round(2)}s") end end |
#to_json ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/zold/node/farm.rb', line 60 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(', ') } end |
#to_text ⇒ Object
54 55 56 57 58 |
# File 'lib/zold/node/farm.rb', line 54 def to_text @threads.map do |t| "#{t.name}: status=#{t.status}; alive=#{t.alive?};\n #{t.backtrace.join("\n ")}" end.join("\n") end |