breeder 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,10 @@ module Breeder
8
8
  @initial_workers = initial_workers
9
9
  end
10
10
 
11
+ def num_workers
12
+ @children.size
13
+ end
14
+
11
15
  def start!
12
16
  @initial_workers.times { spawn! }
13
17
  end
@@ -38,6 +42,7 @@ module Breeder
38
42
  end
39
43
  trap('INT', 'DEFAULT')
40
44
  worker.run
45
+ Process.exit
41
46
  end
42
47
  end
43
48
  t.join
@@ -9,6 +9,10 @@ module Breeder
9
9
  @initial_workers = initial_workers
10
10
  end
11
11
 
12
+ def num_workers
13
+ @threads.size
14
+ end
15
+
12
16
  def start!
13
17
  @initial_workers.times { spawn! }
14
18
  end
data/lib/breeder/core.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Breeder
2
2
  class Core
3
- # the poll interval in seconds
3
+ # the poll interval in seconds (default is 60)
4
4
  attr_accessor :interval
5
5
 
6
6
  # an instance with spawn? and reap? methods to determine when to spawn and reap
@@ -10,13 +10,13 @@ module Breeder
10
10
  attr_accessor :initial_workers
11
11
 
12
12
  def initialize
13
- self.interval = 5
13
+ self.interval = 6
14
14
  self.initial_workers = 4
15
15
  end
16
16
 
17
17
  def watcher=(watcher)
18
- unless watcher.respond_to?(:spawn?) && watcher.respond_to?(:reap?)
19
- raise "Watcher must implement spawn? and reap?"
18
+ unless watcher.respond_to?(:check)
19
+ raise "Watcher must implement 'check' method"
20
20
  end
21
21
 
22
22
  @watcher = watcher
@@ -54,9 +54,20 @@ module Breeder
54
54
  # start the workers
55
55
  @strategy.start!
56
56
 
57
+ warn "No Watcher specified, will not spawn or reap workers" if @watcher.nil?
58
+
57
59
  @polling_thread = Thread.new do
58
- # TODO polling
59
60
  loop do
61
+ if !!@watcher
62
+ decision = @watcher.check(@strategy.num_workers)
63
+ if :spawn == decision
64
+ @strategy.spawn!
65
+ puts "Spawned worker, now running #{@strategy.num_workers} workers"
66
+ elsif :reap == decision
67
+ @strategy.reap!
68
+ puts "Reaped worker, now running #{@strategy.num_workers} workers"
69
+ end
70
+ end
60
71
  sleep interval
61
72
  end
62
73
  end
@@ -0,0 +1,11 @@
1
+ module Breeder
2
+ class Base
3
+
4
+ # Check whether to spawn, reap, or do nothing
5
+ # Returns :spawn or :reap or nil
6
+ def check(num_workers)
7
+ raise NotImplementedError
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module Breeder
2
+ module Watcher
3
+ class Beanstalk < Base
4
+
5
+ def initialize(client, tube, max_workers)
6
+ raise "max_workers must be at least 1" if max_workers < 1
7
+ @client, @tube, @max_workers = client, tube, max_workers
8
+ @last_check = nil
9
+ end
10
+
11
+ def check(num_workers)
12
+ num = jobs_ready
13
+ if !!@last_check && num > @last_check && num_workers <= @max_workers
14
+ decision = :spawn
15
+ elsif !!@last_check && num < 0.5 * @last_check
16
+ decision = :reap
17
+ else
18
+ decision = nil
19
+ end
20
+ @last_check = num
21
+ decision
22
+ end
23
+
24
+ def jobs_ready
25
+ @client.stats_tube(@tube)['current-jobs-ready']
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -1,16 +1,7 @@
1
- module Breeder
2
-
3
- # A watcher has a spawn? and reap? method that indicate when to
4
- # spawn and reap more threads
5
- class Watcher
6
-
7
- def spawn?(num_workers)
8
- raise NotImplementedError
9
- end
10
-
11
- def reap?(num_workers)
12
- raise NotImplementedError
13
- end
1
+ require 'breeder/watcher/base'
2
+ require 'breeder/watcher/beanstalk'
14
3
 
4
+ module Breeder
5
+ module Watcher
15
6
  end
16
7
  end
data/lib/breeder.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'rubygems'
2
- require 'bundler/setup'
3
2
  require 'metaclass'
3
+ require 'beanstalk-client'
4
4
  require 'breeder/breeding_strategy'
5
5
  require 'breeder/core'
6
6
  require 'breeder/watcher'
data/spec/core_spec.rb CHANGED
@@ -15,26 +15,16 @@ module Breeder
15
15
 
16
16
  context 'when the argument is a valid watcher' do
17
17
  it 'sets the watcher' do
18
- watcher = Breeder::Watcher.new
18
+ watcher = Breeder::Watcher::Beanstalk.new(mock('stalk'), 'tube', 8)
19
19
  @core.watcher = watcher
20
20
  @core.watcher.should == watcher
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
- describe '#worker=' do
26
- context 'when the argument is not a valid worker' do
27
- it 'raises an error' do
28
- lambda { @core.worker= 42 }.should raise_error
29
- end
30
- end
31
-
32
- context 'when the argument is a valid worker' do
33
- it 'sets the worker' do
34
- test_worker = Breeder::Worker.new
35
- @core.worker_factory { test_worker }
36
- @core.create_worker.should == test_worker
37
- end
25
+ describe '#worker_factory' do
26
+ it 'sets the worker factory' do
27
+ pending
38
28
  end
39
29
  end
40
30
 
@@ -47,6 +37,7 @@ module Breeder
47
37
 
48
38
  context 'when a block of work is supplied' do
49
39
  it 'creates a worker with the block as its workload' do
40
+ pending 'no create_watcher method'
50
41
  task_done = false
51
42
  @core.task { task_done = true }
52
43
  @core.create_worker.do_work
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require 'rubygems'
2
2
  $:.unshift(File.dirname(__FILE__) + '/../lib/')
3
3
  require 'breeder'
4
+ require 'rspec'
5
+ require 'rcov'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breeder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-10-20 00:00:00.000000000Z
13
+ date: 2011-10-21 00:00:00.000000000Z
14
14
  dependencies: []
15
15
  description:
16
16
  email:
@@ -29,6 +29,8 @@ files:
29
29
  - lib/breeder/core.rb
30
30
  - lib/breeder/watcher.rb
31
31
  - lib/breeder/breeding_strategy.rb
32
+ - lib/breeder/watcher/base.rb
33
+ - lib/breeder/watcher/beanstalk.rb
32
34
  - lib/breeder/breeding_strategy/forks.rb
33
35
  - lib/breeder/breeding_strategy/threads.rb
34
36
  - spec/worker_spec.rb