Class: CFCM::Monkey::SoftMonkey

Inherits:
Object
  • Object
show all
Defined in:
lib/cfcm/monkey.rb

Instance Method Summary collapse

Constructor Details

#initialize(session, app_name, probability, min, max, frequency) ⇒ SoftMonkey

Returns a new instance of SoftMonkey.



9
10
11
12
13
14
15
16
17
# File 'lib/cfcm/monkey.rb', line 9

def initialize(session, app_name, probability, min, max, frequency)
  @session = session
  @app_name = app_name
  @app = @session.get_app(app_name)
  @probability = probability
  @min_instances = min
  @max_instances = max
  @frequency = frequency
end

Instance Method Details

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cfcm/monkey.rb', line 19

def start
  EventMachine.run do
    EventMachine.add_periodic_timer(@frequency) do
      # Determine if we should unleash the monkey
      if (Random.rand(100) + 1) <= @probability
        @app = @session.get_app(@app_name)
        instances = @app.total_instances
        max_growth = @session.max_instance_growth(@app_name)
        
        if instances > @max_instances
          puts "Shrink due to instance limitations"
          @app = @session.remove_instance(@app)
        elsif instances < @min_instances
          puts "Grow due to instance limitations"
          @app = @session.add_instance(@app)
        elsif max_growth == 0 || @max_instances == instances
          # Shrink
          puts "Shrink due to instance limitations"
          @app = @session.remove_instance(@app)
        elsif instances == @min_instances
          # Grow
          puts "Grow due to instance limitations"
          @app = @session.add_instance(@app)
        elsif Random.rand(2) == 0
          # Shrink
          puts "Shrink due to randomness"
          @app = @session.remove_instance(@app)
        else
          #Grow
          puts "Grow due to randomness"
          @app = @session.add_instance(@app)
        end
      end
    end          
  end
end