Class: Hatchet::Reaper

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

Overview

Hatchet apps are useful after the tests run for debugging purposes the reaper is designed to allow the most recent apps to stay alive while keeping the total number of apps under the global Heroku limit. Any time you’re worried about hitting the limit call @reaper.cycle

Constant Summary collapse

HEROKU_APP_LIMIT =

the number of apps heroku allows you to keep

Integer(ENV["HEROKU_APP_LIMIT"]  || 100)
HATCHET_APP_LIMT =

the number of apps hatchet keeps around

Integer(ENV["HATCHET_APP_LIMIT"] || 20)
DEFAULT_REGEX =
/^hatchet-t-/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(heroku, regex = DEFAULT_REGEX) ⇒ Reaper

Returns a new instance of Reaper.



14
15
16
17
# File 'lib/hatchet/reaper.rb', line 14

def initialize(heroku, regex = DEFAULT_REGEX)
  @heroku = heroku
  @regex  = regex
end

Instance Attribute Details

#appsObject

Returns the value of attribute apps.



11
12
13
# File 'lib/hatchet/reaper.rb', line 11

def apps
  @apps
end

Instance Method Details

#cycle(apps = get_apps) ⇒ Object



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

def cycle(apps = get_apps)
  if over_limit?
    if @hatchet_apps.count > 1
      destroy_oldest
      cycle
    else
      puts "Warning: Reached Heroku app limit of #{HEROKU_APP_LIMIT}."
    end
  else
    # do nothing
  end
end

#destroy_allObject



46
47
48
49
50
51
# File 'lib/hatchet/reaper.rb', line 46

def destroy_all
  get_apps
  @hatchet_apps.each do |app|
    destroy_by_name(app["name"])
  end
end

#destroy_by_name(name, details = "") ⇒ Object



53
54
55
56
# File 'lib/hatchet/reaper.rb', line 53

def destroy_by_name(name, details="")
  puts "Destroying #{name.inspect}. #{details}"
  @heroku.delete_app(name)
end

#destroy_oldestObject



39
40
41
42
43
44
# File 'lib/hatchet/reaper.rb', line 39

def destroy_oldest
  oldest_name = @hatchet_apps.pop["name"]
  destroy_by_name(oldest_name, "Hatchet app limit: #{HATCHET_APP_LIMT}")
rescue Heroku::API::Errors::NotFound
  # app already deleted, cycle will catch if there's still too many
end

#get_appsObject

Ascending order, oldest is last



20
21
22
23
24
# File 'lib/hatchet/reaper.rb', line 20

def get_apps
  @apps         = @heroku.get_apps.body.sort_by {|app| DateTime.parse(app["created_at"]) }.reverse
  @hatchet_apps = @apps.select {|app| app["name"].match(@regex) }
  @apps
end