lib/zold/node/front.rb in zold-0.16.4 vs lib/zold/node/front.rb in zold-0.16.5
- old
+ new
@@ -24,12 +24,10 @@
require 'eventmachine'
require 'thin'
require 'json'
require 'sinatra/base'
-require 'cachy'
-require 'moneta'
require 'get_process_mem'
require 'diffy'
require 'usagewatch_ext'
require 'concurrent'
require 'backtrace'
@@ -39,21 +37,21 @@
require_relative '../age'
require_relative '../copies'
require_relative '../log'
require_relative '../id'
require_relative '../http'
+require_relative '../cache'
# The web front of the node.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
# License:: MIT
module Zold
# Web front
class Front < Sinatra::Base
configure do
Thread.current.name = 'sinatra'
- 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
@@ -77,14 +75,16 @@
set :network, 'test' # to be injected at node.rb
set :wallets, nil? # to be injected at node.rb
set :remotes, nil? # to be injected at node.rb
set :copies, nil? # to be injected at node.rb
set :node_alias, nil? # to be injected at node.rb
+ set :cache, Cache.new
end
use Rack::Deflater
before do
+ Thread.current.thread_variable_set(:uri, request.url)
@start = Time.now
if !settings.halt.empty? && params[:halt] && params[:halt] == settings.halt
settings.log.error('Halt signal received, shutting the front end down...')
Front.stop!
end
@@ -187,14 +187,14 @@
alias: settings.node_alias,
network: settings.network,
protocol: settings.protocol,
score: score.to_h,
pid: Process.pid,
- cpus: Cachy.cache(:a_cpus) { Concurrent.processor_count },
+ cpus: settings.cache.get(:cpus) { Concurrent.processor_count },
memory: GetProcessMem.new.bytes.to_i,
platform: RUBY_PLATFORM,
- load: Cachy.cache(:a_load, expires_in: 5 * 60) { Usagewatch.uw_load.to_f },
+ load: settings.cache.get(:load, lifetime: 5 * 60) { Usagewatch.uw_load.to_f },
threads: "#{Thread.list.select { |t| t.status == 'run' }.count}/#{Thread.list.count}",
wallets: total_wallets,
remotes: settings.remotes.all.count,
nscore: settings.remotes.all.map { |r| r[:score] }.inject(&:+) || 0,
farm: settings.farm.to_json,
@@ -388,10 +388,11 @@
[
"Total threads: #{Thread.list.count}",
Thread.list.map do |t|
[
"#{t.name}: status=#{t.status}; alive=#{t.alive?}",
+ 'Vars: ' + t.thread_variables.map { |v| "#{v}=\"#{t.thread_variable_get(v)}\"" }.join('; '),
t.backtrace.nil? ? 'NO BACKTRACE' : " #{t.backtrace.join("\n ")}"
].join("\n")
end
].flatten.join("\n\n")
end
@@ -425,17 +426,23 @@
header = request.env[name]
return unless header
yield header
end
+ # @todo #513:30min This method is temporarily disabled since it
+ # takes a lot of time (when the amount of wallets is big, like 40K). However,
+ # we must find a way to count them somehow faster.
def total_wallets
- Cachy.cache(:a_wallets, expires_in: 5 * 60) { settings.wallets.all.count }
+ return 256 if settings.network == Wallet::MAIN_NETWORK
+ settings.wallets.all.count
end
def score
- 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]
+ settings.cache.get(:score, lifetime: settings.network == Wallet::MAIN_NETWORK ? 60 : 1) do
+ b = settings.farm.best
+ raise 'Score is empty, there is something wrong with the Farm!' if b.empty?
+ b[0]
+ end
end
def copy_of(id)
Tempfile.open([id.to_s, Wallet::EXT]) do |f|
settings.wallets.find(id) do |wallet|