Class: DynoBattery

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ DynoBattery

Returns a new instance of DynoBattery.



2
3
4
# File 'lib/dynobattery.rb', line 2

def initialize(app)
  @app = app
end

Instance Method Details

#_call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dynobattery.rb', line 10

def _call(env)
  if defined?(DYNOBATTERY_ID)
    req = Rack::Request.new(env)
    @params = req.params
    if env["PATH_INFO"] =~ /^\/add_dyno/ and @params["id"] == DYNOBATTERY_ID
      add_dyno
      [200, {"Content-Type" => "text/plain"}, ["Dyno +1"]]
    elsif env["PATH_INFO"] =~ /^\/remove_dyno/ and @params["id"] == DYNOBATTERY_ID
      remove_dyno
      [200, {"Content-Type" => "text/plain"}, ["Dyno -1"]]
    else
      uri = URI.parse("http://dyno-battery.heroku.com/")
      http = Net::HTTP.new(uri.host, uri.port)
      http.open_timeout = 3
      http.read_timeout = 3
      start = Time.now  
      status, headers, response = @app.call(env)  
      stop = Time.now
      http.post("/heroku","name=#{DYNOBATTERY_ID}&date=#{start.to_s}&time=#{"%10.6f" % (stop - start)}&dyno=#{env['HTTP_X_HEROKU_DYNOS_IN_USE']}")
      [status, headers, response]
    end
  else
    [status, headers, response]
  end
end

#add_dynoObject



41
42
43
44
# File 'lib/dynobattery.rb', line 41

def add_dyno
  setup_heroku_client
  @heroku.set_dynos(@heroku_app[:name],@heroku_app[:dynos] + 1)
end

#call(env) ⇒ Object



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

def call(env)
  dup._call(env)
end

#remove_dynoObject



46
47
48
49
# File 'lib/dynobattery.rb', line 46

def remove_dyno
  setup_heroku_client
  @heroku.set_dynos(@heroku_app[:name],@heroku_app[:dynos] - 1)
end

#setup_heroku_clientObject



51
52
53
54
# File 'lib/dynobattery.rb', line 51

def setup_heroku_client
  @heroku = Heroku::Client.new(DYNO_USER, DYNO_PASSWORD)
  @heroku_app = @heroku.info(DYNO_DOMAIN)
end