lib/zold/node/farm.rb in zold-0.15.0 vs lib/zold/node/farm.rb in zold-0.16.0
- old
+ new
@@ -21,10 +21,11 @@
# SOFTWARE.
require 'time'
require 'open3'
require 'backtrace'
+require 'futex'
require_relative '../log'
require_relative '../score'
require_relative '../age'
require_relative '../verbose_thread'
@@ -46,11 +47,10 @@
@log = log
@cache = cache
@invoice = invoice
@pipeline = Queue.new
@threads = []
- @mutex = Mutex.new
end
def best
load
end
@@ -125,18 +125,18 @@
private
def finish(thread)
start = Time.now
@alive = false
- @log.info("Attempting to terminate the thread \"#{thread.name}\"...")
+ @log.info("Attempting to terminate the thread \"#{thread.name}\" of the farm...")
loop do
delay = Time.now - start
if thread.join(0.1)
@log.info("Thread \"#{thread.name}\" finished in #{Age.new(start)}")
break
end
- if delay > 10
+ if delay > 1
thread.exit
@log.error("Thread \"#{thread.name}\" forcefully terminated after #{Age.new(start)}")
end
end
end
@@ -218,13 +218,13 @@
end
def save(threads, list = [])
scores = load + list
period = 24 * 60 * 60 / [threads, 1].max
- @mutex.synchronize do
- File.write(
- @cache,
+ Futex.new(@cache, log: @log).open do |f|
+ IO.write(
+ f,
scores.select(&:valid?)
.reject(&:expired?)
.sort_by(&:value)
.reverse
.uniq(&:time)
@@ -235,12 +235,12 @@
)
end
end
def load
- @mutex.synchronize do
- if File.exist?(@cache)
- File.read(@cache).split(/\n/)
+ Futex.new(@cache, log: @log).open do |f|
+ if File.exist?(f)
+ IO.read(f).split(/\n/)
.map { |t| parse_score_line(t) }
.reject(&:zero?)
else
[]
end