Class: Zombees::Swarm

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Yell::Loggable
Defined in:
lib/zombees/swarm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, population = nil) ⇒ Swarm

Returns a new instance of Swarm.



13
14
15
16
17
18
# File 'lib/zombees/swarm.rb', line 13

def initialize(options, population=nil)
  @worker_count = options.worker_count
  @adapter = options.command
  @honey_comb = options.honey_comb
  @population = population
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/zombees/swarm.rb', line 9

def command
  @command
end

#honey_combObject (readonly)

Returns the value of attribute honey_comb.



9
10
11
# File 'lib/zombees/swarm.rb', line 9

def honey_comb
  @honey_comb
end

#worker_countObject (readonly)

Returns the value of attribute worker_count.



9
10
11
# File 'lib/zombees/swarm.rb', line 9

def worker_count
  @worker_count
end

Instance Method Details

#breedObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zombees/swarm.rb', line 20

def breed
  logger.info "Resurrecting the bees population of #{worker_count}"
  @population ||= worker_count.downto(1).pmap do |index|
    worker = honey_comb.worker
    begin 
      logger.info "Bee #{ index } is getting ready to fight"
      worker.bootstrap.tap { |w| @adapter.prepare(w) }
    rescue => e
      logger.error "ARGH! Bee #{ index } stayed dead: #{e.inspect}"
      worker.shutdown(e)
    end
    worker
  end

end

#populationObject



36
37
38
# File 'lib/zombees/swarm.rb', line 36

def population
  breed
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zombees/swarm.rb', line 40

def run
  logger.info "The swarm lurches toward the target..."
  results = population.pmap do |worker|
    begin
      logger.info "A zombie bee is attacking the target!"
      @adapter.run(worker)
    rescue => e
      logger.error "A zombie bee can't reach the brains due to #{e.message}"
      logger.debug e.backtrace
    ensure
      logger.info "A zombie bee is full of brains... Going back to the grave"
      #logger.debug "worker: #{ worker.object_id } server: #{ worker.server.inspect }"
      worker.shutdown
    end
  end
  logger.info "Digesting acquired brains"
  @adapter.aggregate(results)
end