Class: Lapidar::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/lapidar/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buschtelefon_endpoint) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lapidar/runner.rb', line 7

def initialize(buschtelefon_endpoint)
  @logger = Logger.new(StringIO.new)
  @buschtelefon_endpoint = buschtelefon_endpoint
  @chain = Persistence.load_chain("#{@buschtelefon_endpoint.port}.json") || Chain.new
  @incoming_blocks = Queue.new
  @punch_queue = SizedQueue.new(1)
  @should_stop = nil
  @threads = []

  # Reload currently strongest chain into buschtelefon_endpoint
  if @chain.blocks.any?
    @buschtelefon_endpoint.load_messages(@chain.blocks.map(&:to_h).map(&:to_json))
  end
end

Instance Attribute Details

#buschtelefon_endpointObject (readonly)

Returns the value of attribute buschtelefon_endpoint.



5
6
7
# File 'lib/lapidar/runner.rb', line 5

def buschtelefon_endpoint
  @buschtelefon_endpoint
end

#chainObject (readonly)

Returns the value of attribute chain.



5
6
7
# File 'lib/lapidar/runner.rb', line 5

def chain
  @chain
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/lapidar/runner.rb', line 5

def logger
  @logger
end

#punch_queueObject (readonly)

Returns the value of attribute punch_queue.



5
6
7
# File 'lib/lapidar/runner.rb', line 5

def punch_queue
  @punch_queue
end

Instance Method Details

#startObject



22
23
24
25
26
27
# File 'lib/lapidar/runner.rb', line 22

def start
  @should_stop = false
  @threads = [consumer, local_producer, network_producer]
  @threads.each { |t| t.abort_on_exception = true }
  @threads.each(&:join)
end

#stopObject



29
30
31
32
33
34
# File 'lib/lapidar/runner.rb', line 29

def stop
  @should_stop = true
  Thread.pass
  Persistence.save_chain("#{@buschtelefon_endpoint.port}.json", @chain)
  @threads.each(&:exit)
end