Class: HipstaScale::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/hipsta_scale/process.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Process

Returns a new instance of Process.



20
21
22
23
24
25
# File 'lib/hipsta_scale/process.rb', line 20

def initialize(app)
  @app = app      
  logger.info "Checking usage for '#{app.name}' every #{app.interval} #{app.interval == 1 ? 'second' : 'seconds'}"
  EventMachine.add_periodic_timer(app.interval) { self.check }
  self.check
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/hipsta_scale/process.rb', line 6

def app
  @app
end

Class Method Details

.run!Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hipsta_scale/process.rb', line 8

def self.run!
  EventMachine.run do
    HipstaScale.logger.info "HipstaScale is watching #{HipstaScale.apps.size} #{HipstaScale.apps.size == 1 ? 'application' : 'applications'}"
    HipstaScale.apps.each_with_index do |app, index|
      # Simple way of spacing out the timers so they don't all run at once.
      time = 5 * index
      HipstaScale.logger.info "Will start '#{app.name}' in #{time} #{time == 1 ? 'second' : 'seconds'}..."
      EventMachine::add_timer(time) { Process.new(app) }
    end
  end
end

Instance Method Details

#checkObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hipsta_scale/process.rb', line 27

def check
  app.tick do
    logger.info "[#{app.name.upcase}] Instance usage: %.2f%%" % (app.current_load * 100.0)        
    logger.info "[#{app.name.upcase}] Amount of processes needed to reach the %.2f%% of target load: #{app.processes_needed}" % (app.load_limit * 100)
    
    if app.processes_needed != app.processes_running
      logger.info "[#{app.name.upcase}] Scaling processes from #{app.processes_running} to #{app.processes_needed}"
      app.scale!
    end
  end
end