Class: Zold::Remotes
- Inherits:
-
Object
- Object
- Zold::Remotes
- Defined in:
- lib/zold/remotes.rb
Overview
All remotes
Direct Known Subclasses
Defined Under Namespace
Constant Summary collapse
- PORT =
The default TCP port all nodes are supposed to use.
4096
- TOLERANCE =
At what amount of errors we delete the remote automatically
8
- RUNTIME_LIMIT =
After this limit, the remote runtime must be recorded
16
- MAX_NODES =
Default number of nodes to fetch.
16
Instance Method Summary collapse
- #add(host, port = Remotes::PORT) ⇒ Object
- #all ⇒ Object
- #clean ⇒ Object
- #error(host, port = Remotes::PORT) ⇒ Object
- #errors(host, port = Remotes::PORT) ⇒ Object
- #exists?(host, port = Remotes::PORT) ⇒ Boolean
-
#initialize(file, network: 'test') ⇒ Remotes
constructor
A new instance of Remotes.
- #iterate(log, farm: Farm::Empty.new) ⇒ Object
- #remove(host, port = Remotes::PORT) ⇒ Object
- #rescore(host, port, score) ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(file, network: 'test') ⇒ Remotes
Returns a new instance of Remotes.
114 115 116 117 118 119 120 |
# File 'lib/zold/remotes.rb', line 114 def initialize(file, network: 'test') raise 'File can\'t be nil' if file.nil? @file = file raise 'Network can\'t be nil' if network.nil? @network = network @mutex = Mutex.new end |
Instance Method Details
#add(host, port = Remotes::PORT) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/zold/remotes.rb', line 152 def add(host, port = Remotes::PORT) raise 'Host can\'t be nil' if host.nil? raise 'Host can\'t be empty' if host.empty? raise 'Port can\'t be nil' if port.nil? raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise 'Port can\'t be zero' if port.zero? raise 'Port can\'t be negative' if port < 0 raise 'Port can\'t be over 65536' if port > 0xffff raise "#{host}:#{port} already exists" if exists?(host, port) list = load list << { host: host.downcase, port: port, score: 0 } list.uniq! { |r| "#{r[:host]}:#{r[:port]}" } save(list) end |
#all ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/zold/remotes.rb', line 122 def all list = load max_score = list.map { |r| r[:score] }.max || 0 max_score = 1 if max_score.zero? max_errors = list.map { |r| r[:errors] }.max || 0 max_errors = 1 if max_errors.zero? list.sort_by do |r| (1 - r[:errors] / max_errors) * 5 + (r[:score] / max_score) end.reverse end |
#clean ⇒ Object
133 134 135 |
# File 'lib/zold/remotes.rb', line 133 def clean save([]) end |
#error(host, port = Remotes::PORT) ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/zold/remotes.rb', line 210 def error(host, port = Remotes::PORT) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? raise 'Port has to be of type Integer' unless port.is_a?(Integer) list = load raise "#{host}:#{port} is absent among #{list.count} remotes" unless exists?(host, port) list.find { |r| r[:host] == host.downcase && r[:port] == port }[:errors] += 1 save(list) end |
#errors(host, port = Remotes::PORT) ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/zold/remotes.rb', line 201 def errors(host, port = Remotes::PORT) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? raise 'Port has to be of type Integer' unless port.is_a?(Integer) list = load raise "#{host}:#{port} is absent among #{list.count} remotes" unless exists?(host, port) list.find { |r| r[:host] == host.downcase && r[:port] == port }[:errors] end |
#exists?(host, port = Remotes::PORT) ⇒ Boolean
145 146 147 148 149 150 |
# File 'lib/zold/remotes.rb', line 145 def exists?(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? !load.find { |r| r[:host] == host.downcase && r[:port] == port }.nil? end |
#iterate(log, farm: Farm::Empty.new) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/zold/remotes.rb', line 177 def iterate(log, farm: Farm::Empty.new) raise 'Log can\'t be nil' if log.nil? raise 'Farm can\'t be nil' if farm.nil? best = farm.best[0] require_relative 'score' score = best.nil? ? Score::ZERO : best idx = 0 all.each do |r| start = Time.now begin yield Remotes::Remote.new(r[:host], r[:port], score, idx, log: log, network: @network) idx += 1 raise 'Took too long to execute' if (Time.now - start).round > Remotes::RUNTIME_LIMIT rescue StandardError => e error(r[:host], r[:port]) errors = errors(r[:host], r[:port]) log.info("#{Rainbow("#{r[:host]}:#{r[:port]}").red}: #{e.} \ in #{(Time.now - start).round}s; errors=#{errors}") log.debug(Backtrace.new(e).to_s) remove(r[:host], r[:port]) if errors > Remotes::TOLERANCE end end end |
#remove(host, port = Remotes::PORT) ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/zold/remotes.rb', line 167 def remove(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? raise "#{host}:#{port} is absent" unless exists?(host, port) list = load list.reject! { |r| r[:host] == host.downcase && r[:port] == port } save(list) end |
#rescore(host, port, score) ⇒ Object
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/zold/remotes.rb', line 220 def rescore(host, port, score) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? raise 'Score can\'t be nil' if score.nil? raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise "#{host}:#{port} is absent" unless exists?(host, port) list = load list.find { |r| r[:host] == host.downcase && r[:port] == port }[:score] = score save(list) end |
#reset ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/zold/remotes.rb', line 137 def reset FileUtils.mkdir_p(File.dirname(@file)) FileUtils.copy( File.join(File.dirname(__FILE__), '../../resources/remotes'), @file ) end |