Class: Stormtroopers::Army
- Inherits:
-
Object
- Object
- Stormtroopers::Army
- Defined in:
- lib/stormtroopers/army.rb
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#max_threads ⇒ Object
readonly
Returns the value of attribute max_threads.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
Class Method Summary collapse
Instance Method Summary collapse
- #factory_class(config) ⇒ Object
- #finish ⇒ Object
- #idle? ⇒ Boolean
-
#initialize(config) ⇒ Army
constructor
A new instance of Army.
- #manage ⇒ Object
- #need_more_troops? ⇒ Boolean
- #run_trooper(trooper) ⇒ Object
- #running_task_names ⇒ Object
- #running_troopers ⇒ Object
- #running_troopers_status ⇒ Object
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 = HashWithIndifferentAccess.new(config[:factory]) [:name] ||= config[:name] @factory = factory_class(config).new() @max_threads = config[:max_threads] || 1 @threads = [] end |
Instance Attribute Details
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
3 4 5 |
# File 'lib/stormtroopers/army.rb', line 3 def factory @factory end |
#max_threads ⇒ Object (readonly)
Returns the value of attribute max_threads.
3 4 5 |
# File 'lib/stormtroopers/army.rb', line 3 def max_threads @max_threads end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/stormtroopers/army.rb', line 3 def name @name end |
#threads ⇒ Object (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
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 |
#finish ⇒ Object
70 71 72 73 |
# File 'lib/stormtroopers/army.rb', line 70 def finish logger.debug("#{name}: Finishing") threads.each(&:join) end |
#idle? ⇒ Boolean
34 35 36 37 |
# File 'lib/stormtroopers/army.rb', line 34 def idle? cleanup! threads.count == 0 end |
#manage ⇒ Object
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
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.}:\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_names ⇒ Object
66 67 68 |
# File 'lib/stormtroopers/army.rb', line 66 def running_task_names threads.each end |
#running_troopers ⇒ Object
39 40 41 42 |
# File 'lib/stormtroopers/army.rb', line 39 def running_troopers cleanup! threads.map{ |thread| thread[:trooper] }.compact end |
#running_troopers_status ⇒ Object
44 45 46 |
# File 'lib/stormtroopers/army.rb', line 44 def running_troopers_status running_troopers.map(&:status) end |