lib/zold/hungry_wallets.rb in zold-0.18.8 vs lib/zold/hungry_wallets.rb in zold-0.18.9

- old
+ new

@@ -19,16 +19,45 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. require 'delegate' +require_relative 'log' +require_relative 'thread_pool' +require_relative 'commands/pull' +# Wallets that PULL what's missing, in the background. +# +# Author:: Yegor Bugayenko (yegor256@gmail.com) +# Copyright:: Copyright (c) 2018 Yegor Bugayenko +# License:: MIT module Zold # Wallets decorator that adds missing wallets to the queue to be pulled later. class HungryWallets < SimpleDelegator - # @todo #280:30min Add to the queue. Once in there, try - # to pull it as soon as possible as is described in #280. + def initialize(wallets, remotes, copies, pool, + log: Log::NULL, network: 'test') + @wallets = wallets + @remotes = remotes + @copies = copies + @log = log + @network = network + @pool = pool + @queue = Queue.new + @pool.add do + Endless.new('hungry', log: log).run do + id = @queue.pop + Pull.new(wallets: @wallets, remotes: @remotes, copies: @copies, log: @log).run( + ['pull', id.to_s, "--network=#{@network}"] + ) + end + end + super(wallets) + end + def acq(id, exclusive: false) - yield super(id, exclusive: exclusive) + @wallets.acq(id, exclusive: exclusive) do |wallet| + @queue << id unless wallet.exists? + yield wallet + end end end end