lib/zold/node/farm.rb in zold-0.13.18 vs lib/zold/node/farm.rb in zold-0.13.19
- old
+ new
@@ -71,11 +71,20 @@
Thread.current.name = "farm-#{t}"
loop { cycle(host, port, strength, threads) }
end
end
end
- @log.info("Farm started with #{threads} threads at #{host}:#{port}, strength is #{strength}")
+ @threads << Thread.new do
+ VerboseThread.new(@log).run do
+ Thread.current.name = 'farm-cleaner'
+ loop do
+ sleep(60)
+ clean(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
ensure
stop
@@ -93,16 +102,21 @@
@log.info("Farm stopped in #{(Time.now - start).round(2)}s")
end
private
- def cycle(host, port, strength, threads)
- if @scores.length < threads
+ def clean(host, port, strength, threads)
+ if @scores.length < threads || @best.count < threads
zero = Score.new(Time.now, host, port, @invoice, strength: strength)
@scores << zero
@best << zero
end
+ @best = @best.reject(&:expired?).sort_by(&:value).reverse.take(threads)
+ end
+
+ def cycle(host, port, strength, threads)
+ clean(host, port, strength, threads)
s = @scores.pop
return unless s.valid?
return unless s.host == host
return unless s.port == port
return unless s.strength >= strength
@@ -110,10 +124,10 @@
@semaphore.synchronize do
before = @best.map(&:value).max
save(n)
@best << n
after = @best.map(&:value).max
- @best = @best.reject(&:expired?).sort_by(&:value).reverse.take(threads)
+ clean(host, port, strength, threads)
@log.debug("#{Thread.current.name}: best score is #{@best[0]}") if before != after && !after.zero?
end
@scores << n
end