zold 0.13.20 → 0.13.21

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7624ccdaec6f4098e5e024189d4345e8805d7095
4
- data.tar.gz: 585ad1cbafa0e8424d1017b3983156c94af543f5
3
+ metadata.gz: 9b54e147de6f13a87996515785862a09da82b1a5
4
+ data.tar.gz: 332d33f707a6ad855001b9d77738d2f497622ef7
5
5
  SHA512:
6
- metadata.gz: f0212c757e122fd6c2429959a42e73c4972022c2471f7e24e248d5f69c6e20cfb3d004813c36f64221a9f16b6e5d5de9d3e2a87a7a90fc99c085b02a648b27d2
7
- data.tar.gz: 320d3092ef14317b3566d92b98aa238bd56a82714e3fb54b850100bec75650259c266fac08c16f0fef8236bb378f0fd5f41a90dde0a3002ee3ab78a8c82675c2
6
+ metadata.gz: 6696d078025e97891007c9295fd2210de314c4dbbcef5e968fc4d7a9dd5b614c81903a4066ee4016074c7974321c1e3d656825f01d934ae0dcc55869aa693041
7
+ data.tar.gz: 4463d7a107bbb6acb7a89931ae745dcc05598005cccaca4caf56c2484b5376f3ee07b595fca407376432bf4fcf7461408ff6f88aae52718165e784bf24faaff0
@@ -38,7 +38,6 @@ module Zold
38
38
  end
39
39
  end
40
40
 
41
- attr_reader :best
42
41
  def initialize(invoice, cache, log: Log::Quiet.new)
43
42
  @log = log
44
43
  @cache = cache
@@ -46,14 +45,28 @@ module Zold
46
45
  @scores = []
47
46
  @threads = []
48
47
  @best = []
49
- @semaphore = Mutex.new
48
+ @mutex = Mutex.new
49
+ end
50
+
51
+ def best
52
+ @mutex.synchronize do
53
+ @best.to_a
54
+ end
55
+ end
56
+
57
+ def to_text
58
+ @threads.map do |t|
59
+ "#{t.name}: status=#{t.status}; alive=#{t.alive};\n #{t.backtrace.join("\n ")}"
60
+ end.join("\n")
50
61
  end
51
62
 
52
63
  def to_json
53
64
  {
54
- threads: @threads.map { |t| "#{t.name}/#{t.status}" }.join(', '),
65
+ threads: @threads.map do |t|
66
+ "#{t.name}/#{t.status}/{t.alive? ? 'A' : 'D'}"
67
+ end.join(', '),
55
68
  scores: @scores.size,
56
- best: @best.count,
69
+ best: @best.map(&:value).join(', '),
57
70
  history: history.count
58
71
  }
59
72
  end
@@ -67,17 +80,19 @@ module Zold
67
80
  @log.info("#{@scores.size} scores pre-loaded, the best is: #{@best[0]}")
68
81
  @threads = (1..threads).map do |t|
69
82
  Thread.new do
70
- VerboseThread.new(@log).run do
71
- Thread.current.name = "farm-#{t}"
72
- loop { cycle(host, port, strength, threads) }
83
+ Thread.current.name = "f#{t}"
84
+ loop do
85
+ VerboseThread.new(@log).run do
86
+ cycle(host, port, strength, threads)
87
+ end
73
88
  end
74
89
  end
75
90
  end
76
91
  @threads << Thread.new do
77
- VerboseThread.new(@log).run do
78
- Thread.current.name = 'farm-cleaner'
79
- loop do
80
- sleep(60) unless strength == 1 # which will only happen in tests
92
+ Thread.current.name = 'cleaner'
93
+ loop do
94
+ sleep(60) unless strength == 1 # which will only happen in tests
95
+ VerboseThread.new(@log).run do
81
96
  clean(host, port, strength, threads)
82
97
  end
83
98
  end
@@ -105,13 +120,18 @@ module Zold
105
120
  private
106
121
 
107
122
  def clean(host, port, strength, threads)
108
- if @scores.length < threads || @best.count < threads
109
- zero = Score.new(Time.now, host, port, @invoice, strength: strength)
110
- @scores << zero
111
- @best << zero
123
+ @mutex.synchronize do
124
+ before = @best.map(&:value).max
125
+ @best = @best.reject(&:expired?).sort_by(&:value).reverse
126
+ @best = @best.take(threads) unless threads.zero?
127
+ if @scores.length < threads || @best.count < threads
128
+ zero = Score.new(Time.now, host, port, @invoice, strength: strength)
129
+ @scores << zero
130
+ @best << zero
131
+ end
132
+ after = @best.map(&:value).max
133
+ @log.debug("#{Thread.current.name}: best score is #{@best[0]}") if before != after && !after.zero?
112
134
  end
113
- @best = @best.reject(&:expired?).sort_by(&:value).reverse
114
- @best = @best.take(threads) unless threads.zero?
115
135
  end
116
136
 
117
137
  def cycle(host, port, strength, threads)
@@ -122,14 +142,11 @@ module Zold
122
142
  return unless s.port == port
123
143
  return unless s.strength >= strength
124
144
  n = s.next
125
- @semaphore.synchronize do
126
- before = @best.map(&:value).max
145
+ @mutex.synchronize do
127
146
  save(n)
128
147
  @best << n
129
- clean(host, port, strength, threads)
130
- after = @best.map(&:value).max
131
- @log.debug("#{Thread.current.name}: best score is #{@best[0]}") if before != after && !after.zero?
132
148
  end
149
+ clean(host, port, strength, threads)
133
150
  @scores << n
134
151
  end
135
152
 
@@ -196,6 +196,11 @@ module Zold
196
196
  )
197
197
  end
198
198
 
199
+ get '/farm' do
200
+ content_type 'text/plain'
201
+ settings.farm.to_text
202
+ end
203
+
199
204
  not_found do
200
205
  status 404
201
206
  content_type 'text/plain'
data/lib/zold/version.rb CHANGED
@@ -23,5 +23,5 @@
23
23
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
24
24
  # License:: MIT
25
25
  module Zold
26
- VERSION = '0.13.20'.freeze
26
+ VERSION = '0.13.21'.freeze
27
27
  end
@@ -90,7 +90,9 @@ class FarmTest < Minitest::Test
90
90
  farm.start(score.host, score.port, threads: 1, strength: score.strength) do
91
91
  100.times do
92
92
  sleep(0.1)
93
- break if farm.best[0].value.zero?
93
+ b = farm.best[0]
94
+ assert(!b.nil?)
95
+ break if b.value.zero?
94
96
  end
95
97
  assert_equal(0, farm.best[0].value)
96
98
  end
@@ -36,6 +36,7 @@ class FrontTest < Minitest::Test
36
36
  '/',
37
37
  '/remotes',
38
38
  '/version',
39
+ '/farm',
39
40
  '/score'
40
41
  ],
41
42
  '404' => [
@@ -146,8 +147,8 @@ class FrontTest < Minitest::Test
146
147
  "Expected HTTP 200 OK: Found #{response.code}"
147
148
  )
148
149
  assert_operator(
149
- 500, :>, response['content-length'].to_i,
150
- 'Expected the content to be smaller than 500bytes for gzip'
150
+ 600, :>, response['content-length'].to_i,
151
+ 'Expected the content to be smaller than 600 bytes for gzip'
151
152
  )
152
153
  end
153
154
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.20
4
+ version: 0.13.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko