Class: Stormtroopers::Army

Inherits:
Object
  • Object
show all
Defined in:
lib/stormtroopers/army.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Army

Returns a new instance of Army.



5
6
7
8
9
10
11
12
# File 'lib/stormtroopers/army.rb', line 5

def initialize(config)
  @name = config[:name] || factory_class(config).name
  factory_options = HashWithIndifferentAccess.new(config[:factory])
  factory_options[:name] ||= config[:name]
  @factory = factory_class(config).new(factory_options)
  @max_threads = config[:max_threads] || 1
  @threads = []
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



3
4
5
# File 'lib/stormtroopers/army.rb', line 3

def factory
  @factory
end

#max_threadsObject (readonly)

Returns the value of attribute max_threads.



3
4
5
# File 'lib/stormtroopers/army.rb', line 3

def max_threads
  @max_threads
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/stormtroopers/army.rb', line 3

def name
  @name
end

#threadsObject (readonly)

Returns the value of attribute threads.



3
4
5
# File 'lib/stormtroopers/army.rb', line 3

def threads
  @threads
end

Class Method Details

.factory_class(config) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
# File 'lib/stormtroopers/army.rb', line 86

def factory_class(config)
  raise ArgumentError, "Factory class or type must be defined" if config[:factory][:class].blank? && config[:factory][:type].blank?
  class_name ||= config[:factory].delete(:class)
  class_name ||= "stormtroopers/#{config[:factory].delete(:type)}_factory".camelize
  class_name.constantize
end

Instance Method Details

#factory_class(config) ⇒ Object



14
15
16
# File 'lib/stormtroopers/army.rb', line 14

def factory_class(config)
  @factory_class ||= self.class.factory_class(config)
end

#finishObject



70
71
72
73
# File 'lib/stormtroopers/army.rb', line 70

def finish
  logger.debug("#{name}: Finishing")
  threads.each(&:join)
end

#idle?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/stormtroopers/army.rb', line 34

def idle?
  cleanup!
  threads.count == 0
end

#manageObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/stormtroopers/army.rb', line 18

def manage
  assigned = false
  if need_more_troops?
    if trooper = factory.produce
      run_trooper(trooper)
      assigned = true
    end
  end
  assigned
end

#need_more_troops?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/stormtroopers/army.rb', line 29

def need_more_troops?
  cleanup!
  threads.count < max_threads
end

#run_trooper(trooper) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/stormtroopers/army.rb', line 48

def run_trooper(trooper)
  threads << Thread.new do
    begin
      Thread.current[:trooper] = trooper
      trooper.start
    rescue Exception => exception
      logger.error("Unexpected thread death for trooper:  #{trooper.status rescue "failed to retrieve trooper status"} : #{exception.message}:\n#{exception.backtrace.join("\n")}")
    ensure
      if defined?(::Mongoid)
        ::Mongoid::IdentityMap.clear
        ::Mongoid.sessions.keys.each do |session_name|
          ::Mongoid.session(session_name).disconnect
        end
      end
    end
  end
end

#running_task_namesObject



66
67
68
# File 'lib/stormtroopers/army.rb', line 66

def running_task_names
  threads.each
end

#running_troopersObject



39
40
41
42
# File 'lib/stormtroopers/army.rb', line 39

def running_troopers
  cleanup!
  threads.map{ |thread| thread[:trooper] }.compact
end

#running_troopers_statusObject



44
45
46
# File 'lib/stormtroopers/army.rb', line 44

def running_troopers_status
  running_troopers.map(&:status)
end