Class: Zold::Pipeline

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

Overview

The pipeline

Instance Method Summary collapse

Constructor Details

#initialize(remotes, copies, address, ledger: '/dev/null', network: 'test') ⇒ Pipeline

Returns a new instance of Pipeline.



42
43
44
45
46
47
48
49
50
51
# File 'lib/zold/node/pipeline.rb', line 42

def initialize(remotes, copies, address, ledger: '/dev/null', network: 'test')
  @remotes = remotes
  @copies = copies
  @address = address
  @network = network
  @history = []
  @speed = []
  @mutex = Mutex.new
  @ledger = ledger
end

Instance Method Details

#push(id, body, wallets, log) ⇒ Object

Returns a list of modifed wallets (as Zold::Id)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zold/node/pipeline.rb', line 61

def push(id, body, wallets, log)
  start = Time.now
  copies = Copies.new(File.join(@copies, id.to_s))
  host = '0.0.0.0'
  copies.add(body, host, Remotes::PORT, 0)
  unless @remotes.all.empty?
    Fetch.new(
      wallets: wallets, remotes: @remotes, copies: copies.root, log: log
    ).run(['fetch', id.to_s, "--ignore-node=#{@address}", "--network=#{@network}", '--quiet-if-absent'])
  end
  modified = merge(id, copies, wallets, log)
  Clean.new(wallets: wallets, copies: copies.root, log: log).run(
    ['clean', id.to_s, '--max-age=1']
  )
  copies.remove(host, Remotes::PORT)
  if modified.empty?
    log.info("Accepted #{id} in #{Age.new(start, limit: 1)} and not modified anything")
  else
    log.info("Accepted #{id} in #{Age.new(start, limit: 1)} and modified #{modified.join(', ')}")
  end
  modified << id if copies.all.count > 1
  modified
end

#to_jsonObject

Show its internals.



54
55
56
57
58
# File 'lib/zold/node/pipeline.rb', line 54

def to_json
  {
    'ledger': File.exist?(@ledger) ? IO.read(@ledger).split("\n").count : 0
  }
end