Class: DynoBattery

Inherits:
Object
  • Object
show all
Defined in:
lib/dyno-battery.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ DynoBattery

Returns a new instance of DynoBattery.



2
3
4
# File 'lib/dyno-battery.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
35
# File 'lib/dyno-battery.rb', line 10

def _call(env)
  @start = Time.now
  @status, @headers, @response = @app.call(env)
  @stop = Time.now
  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
      [@status, @headers, @response]
    elsif env["PATH_INFO"] =~ /^\/remove_dyno/ and @params["id"] == DYNOBATTERY_ID
      remove_dyno
      [@status, @headers, @response]
    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
     @dynos = env['HTTP_X_HEROKU_DYNOS_IN_USE'] || 1
      http.post("/heroku","name=#{DYNOBATTERY_ID}&date=#{@start.to_s}&time=#{"%10.6f" % (@stop - @start)}&dyno=#{@dynos}")
      [@status, @headers, @response]
    end
  else
    [@status, @headers, @response]
  end
end

#add_dynoObject



42
43
44
45
# File 'lib/dyno-battery.rb', line 42

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/dyno-battery.rb', line 6

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

#remove_dynoObject



47
48
49
50
# File 'lib/dyno-battery.rb', line 47

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

#setup_heroku_clientObject



52
53
54
55
# File 'lib/dyno-battery.rb', line 52

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