Class: Breeder::Core
- Inherits:
-
Object
- Object
- Breeder::Core
- Defined in:
- lib/breeder/core.rb
Instance Attribute Summary collapse
-
#initial_workers ⇒ Object
number of workers to start with.
-
#interval ⇒ Object
the poll interval in seconds (default is 60).
-
#watcher ⇒ Object
an instance with spawn? and reap? methods to determine when to spawn and reap.
Instance Method Summary collapse
- #init_strategy ⇒ Object
-
#initialize ⇒ Core
constructor
A new instance of Core.
- #run ⇒ Object
- #task(&block) ⇒ Object
- #worker_factory(&block) ⇒ Object
Constructor Details
#initialize ⇒ Core
Returns a new instance of Core.
12 13 14 15 |
# File 'lib/breeder/core.rb', line 12 def initialize self.interval = 60 self.initial_workers = 4 end |
Instance Attribute Details
#initial_workers ⇒ Object
number of workers to start with
10 11 12 |
# File 'lib/breeder/core.rb', line 10 def initial_workers @initial_workers end |
#interval ⇒ Object
the poll interval in seconds (default is 60)
4 5 6 |
# File 'lib/breeder/core.rb', line 4 def interval @interval end |
#watcher ⇒ Object
an instance with spawn? and reap? methods to determine when to spawn and reap
7 8 9 |
# File 'lib/breeder/core.rb', line 7 def watcher @watcher end |
Instance Method Details
#init_strategy ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/breeder/core.rb', line 39 def init_strategy @strategy = Breeder::BreedingStrategy::Forks.new(@worker_factory, initial_workers) # catch Ctrl+C and cleanup trap('INT') do puts 'INTERRUPT caught, killing workers and exiting...' @strategy.stop! @polling_thread.kill rescue nil end end |
#run ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/breeder/core.rb', line 50 def run # setup the breeding strategy init_strategy # start the workers @strategy.start! warn "No Watcher specified, will not spawn or reap workers" if @watcher.nil? @polling_thread = Thread.new do loop do if !!@watcher decision = @watcher.check(@strategy.num_workers) if :spawn == decision @strategy.spawn! puts "Spawned worker, now running #{@strategy.num_workers} workers" elsif :reap == decision @strategy.reap! puts "Reaped worker, now running #{@strategy.num_workers} workers" end end sleep interval end end @polling_thread.join end |
#task(&block) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/breeder/core.rb', line 30 def task(&block) raise ArgumentError("No block supplied") if block.nil? worker = Breeder::Worker.new worker..class_eval do define_method(:do_work, block) end worker_factory { worker } end |
#worker_factory(&block) ⇒ Object
25 26 27 28 |
# File 'lib/breeder/core.rb', line 25 def worker_factory(&block) raise "No block supplied to worker_factory" unless !!block @worker_factory = block end |