lib/zold/node/front.rb in zold-0.15.0 vs lib/zold/node/front.rb in zold-0.16.0
- old
+ new
@@ -53,11 +53,11 @@
Cachy.cache_store = Moneta.new(:Memory)
set :bind, '0.0.0.0'
set :suppress_messages, true
set :start, Time.now
set :lock, false
- set :show_exceptions, false
+ set :show_exceptions, true
set :server, :puma
set :log, nil? # to be injected at node.rb
set :trace, nil? # to be injected at node.rb
set :halt, '' # to be injected at node.rb
set :dump_errors, false # to be injected at node.rb
@@ -66,10 +66,11 @@
set :ignore_score_weakness, false # to be injected at node.rb
set :reboot, false # to be injected at node.rb
set :nohup_log, false # to be injected at node.rb
set :home, nil? # to be injected at node.rb
set :logging, true # to be injected at node.rb
+ set :logger, nil? # to be injected at node.rb
set :address, nil? # to be injected at node.rb
set :farm, nil? # to be injected at node.rb
set :metronome, nil? # to be injected at node.rb
set :entrance, nil? # to be injected at node.rb
set :network, 'test' # to be injected at node.rb
@@ -119,11 +120,11 @@
headers['Cache-Control'] = 'no-cache'
headers['X-Zold-Version'] = settings.version
headers[Http::PROTOCOL_HEADER] = settings.protocol.to_s
headers['Access-Control-Allow-Origin'] = '*'
headers[Http::SCORE_HEADER] = score.reduced(16).to_s
- headers['X-Zold-Thread'] = Thread.current.name
+ headers['X-Zold-Thread'] = Thread.current.name.to_s
headers['X-Zold-Milliseconds'] = ((Time.now - @start) * 1000).round.to_s
end
get '/robots.txt' do
content_type 'text/plain'
@@ -153,11 +154,11 @@
get '/nohup_log' do
raise 'Run it with --nohup in order to see this log' if settings.nohup_log.nil?
raise "Log not found at #{settings.nohup_log}" unless File.exist?(settings.nohup_log)
response.headers['Content-Type'] = 'text/plain'
response.headers['Content-Disposition'] = "attachment; filename='#{File.basename(settings.nohup_log)}'"
- File.read(settings.nohup_log)
+ IO.read(settings.nohup_log)
end
get '/favicon.ico' do
if score.value >= 16
redirect 'https://www.zold.io/images/logo-green.png'
@@ -175,16 +176,16 @@
alias: settings.node_alias,
network: settings.network,
protocol: settings.protocol,
score: score.to_h,
pid: Process.pid,
- cpus: Concurrent.processor_count,
+ cpus: Cachy.cache(:a_cpus) { Concurrent.processor_count },
memory: GetProcessMem.new.bytes.to_i,
platform: RUBY_PLATFORM,
- load: Usagewatch.uw_load.to_f,
+ load: Cachy.cache(:a_load, expires_in: 5 * 60) { Usagewatch.uw_load.to_f },
threads: "#{Thread.list.select { |t| t.status == 'run' }.count}/#{Thread.list.count}",
- wallets: Cachy.cache(:a_key, expires_in: 5 * 60) { settings.wallets.all.count },
+ wallets: Cachy.cache(:a_wallets, expires_in: 5 * 60) { settings.wallets.all.count },
remotes: settings.remotes.all.count,
nscore: settings.remotes.all.map { |r| r[:score] }.inject(&:+) || 0,
farm: settings.farm.to_json,
entrance: settings.entrance.to_json,
date: Time.now.utc.iso8601,
@@ -194,11 +195,11 @@
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'application/json'
JSON.pretty_generate(
version: settings.version,
alias: settings.node_alias,
@@ -217,11 +218,11 @@
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16}).json} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'application/json'
JSON.pretty_generate(
version: settings.version,
alias: settings.node_alias,
@@ -239,51 +240,51 @@
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/balance} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'text/plain'
wallet.balance.to_i.to_s
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/key} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'text/plain'
wallet.key.to_pub
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/mtime} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'text/plain'
wallet.mtime.utc.iso8601.to_s
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/digest} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'text/plain'
wallet.digest
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})\.txt} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'text/plain'
[
wallet.network,
wallet.protocol,
@@ -303,21 +304,21 @@
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})\.bin} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'text/plain'
- File.read(wallet.path)
+ IO.read(wallet.path)
end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/copies} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
content_type 'text/plain'
copies = Copies.new(File.join(settings.copies, id))
copies.load.map do |c|
"#{c[:name]}: #{c[:host]}:#{c[:port]} #{c[:score]} #{c[:time].utc.iso8601}"
@@ -333,16 +334,16 @@
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/copy/(?<name>[0-9]+)} do
error 404 if settings.disable_fetch
id = Id.new(params[:id])
name = params[:name]
- settings.wallets.find(id) do |wallet|
+ copy_of(id) do |wallet|
error 404 unless wallet.exists?
copy = Copies.new(File.join(settings.copies, id)).all.find { |c| c[:name] == name }
error 404 if copy.nil?
content_type 'text/plain'
- File.read(copy[:path])
+ IO.read(copy[:path])
end
end
put %r{/wallet/(?<id>[A-Fa-f0-9]{16})/?} do
error 404 if settings.disable_push
@@ -381,16 +382,19 @@
settings.metronome.to_text
end
get '/threads' do
content_type 'text/plain'
- Thread.list.map do |t|
- [
- "#{t.name}: status=#{t.status}; alive=#{t.alive?}",
- t.backtrace.nil? ? 'NO BACKTRACE' : " #{t.backtrace.join("\n ")}"
- ].join("\n")
- end.join("\n\n")
+ [
+ "Total threads: #{Thread.list.count}",
+ Thread.list.map do |t|
+ [
+ "#{t.name}: status=#{t.status}; alive=#{t.alive?}",
+ t.backtrace.nil? ? 'NO BACKTRACE' : " #{t.backtrace.join("\n ")}"
+ ].join("\n")
+ end
+ ].flatten.join("\n\n")
end
not_found do
status 404
content_type 'text/plain'
@@ -419,11 +423,22 @@
return unless header
yield header
end
def score
- best = settings.farm.best
+ best = Cachy.cache(:a_score, expires_in: 60) { settings.farm.best }
raise 'Score is empty, there is something wrong with the Farm!' if best.empty?
best[0]
+ end
+
+ def copy_of(id)
+ Tempfile.open([id.to_s, Wallet::EXT]) do |f|
+ settings.wallets.find(id) do |wallet|
+ IO.write(f, IO.read(wallet.path)) if File.exist?(wallet.path)
+ end
+ path = f.path
+ f.delete if File.size(f.path).zero?
+ yield Wallet.new(path)
+ end
end
end
end