Class: Zold::SpreadEntrance

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/node/spread_entrance.rb

Overview

The entrance

Instance Method Summary collapse

Constructor Details

#initialize(entrance, wallets, remotes, address, log: Log::Quiet.new, ignore_score_weakeness: false) ⇒ SpreadEntrance

Returns a new instance of SpreadEntrance.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zold/node/spread_entrance.rb', line 40

def initialize(entrance, wallets, remotes, address, log: Log::Quiet.new, ignore_score_weakeness: false)
  raise 'Entrance can\'t be nil' if entrance.nil?
  @entrance = entrance
  raise 'Wallets can\'t be nil' if wallets.nil?
  raise 'Wallets must be of type Wallets' unless wallets.is_a?(Wallets)
  @wallets = wallets
  raise 'Remotes can\'t be nil' if remotes.nil?
  raise 'Remotes must be of type Remotes' unless remotes.is_a?(Remotes)
  @remotes = remotes
  raise 'Address can\'t be nil' if address.nil?
  @address = address
  raise 'Log can\'t be nil' if log.nil?
  @log = log
  @ignore_score_weakeness = ignore_score_weakeness
end

Instance Method Details

#push(id, body) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/zold/node/spread_entrance.rb', line 96

def push(id, body)
  @entrance.push(id, body).each do |m|
    next if @seen.include?(m)
    @seen << m
    @modified.push(m)
    @log.debug("Push scheduled for #{m}, queue size is #{@modified.size}")
  end
end

#startObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/zold/node/spread_entrance.rb', line 63

def start
  @entrance.start do
    @seen = Set.new
    @modified = Queue.new
    @push = Thread.start do
      Thread.current.abort_on_exception = true
      Thread.current.name = 'push'
      VerboseThread.new(@log).run do
        loop do
          id = @modified.pop
          if @remotes.all.empty?
            @log.info("There are no remotes, won\'t spread #{id}")
          else
            Push.new(wallets: @wallets, remotes: @remotes, log: @log).run(
              ['push', "--ignore-node=#{@address}", id.to_s] +
              (@ignore_score_weakeness ? ['--ignore-score-weakness'] : [])
            )
          end
          @seen.delete(id)
        end
      end
    end
    begin
      yield(self)
    ensure
      @log.info('Waiting for spread entrance to finish...')
      @modified.clear
      @push.exit
      @log.info('Spread entrance finished, thread killed')
    end
  end
end

#to_jsonObject



56
57
58
59
60
61
# File 'lib/zold/node/spread_entrance.rb', line 56

def to_json
  @entrance.to_json.merge(
    'modified': @modified.size,
    'push': @push.status
  )
end