Skip to content

Commit

Permalink
Merge pull request #64 from seomoz/spot_cleanup
Browse files Browse the repository at this point in the history
Spot cleanup
  • Loading branch information
delano committed Jun 22, 2011
2 parents e305c61 + c0d732b commit 52627b6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/rudy/aws/ec2/spot_request.rb
Expand Up @@ -89,6 +89,11 @@ def self.from_hash(hash)
end
end
end

def cancel(requests)
opts = requests ? {:spot_instance_request_id => requests.map(&:id)} : {}
Rudy::AWS::EC2.execute_request({}) { @@ec2.cancel_spot_instance_requests(opts) }
end

private

Expand Down
2 changes: 2 additions & 0 deletions lib/rudy/exceptions.rb
Expand Up @@ -68,5 +68,7 @@ class UnsupportedOS < Rudy::Error; end

class NotImplemented < Rudy::Error; end

class SpotRequestCancelled < Rudy::Error; end


end
7 changes: 6 additions & 1 deletion lib/rudy/metadata.rb
Expand Up @@ -88,6 +88,9 @@ def self.select(fields={})
# Generates a default criteria for all metadata based on
# region, zone, environment, and role. If a position has
# been specified in the globals it will also be included.
# The project name will also be removed if empty, to
# maintain backwards compatability with metadata entries
# that do not contain a projet field.
# * +rtype+ is the record type. One of: m, disk, or back.
# * +fields+ replaces and adds values to this criteria
# * +less+ removes keys from the default criteria.
Expand All @@ -102,6 +105,7 @@ def self.build_criteria(rtype, fields={}, less=[])
mixer = names.zip(values).flatten
criteria = Hash[*mixer].merge(fields)
criteria.reject! { |n,v| less.member?(n) }
criteria.delete(:project) if criteria[:project].to_s.empty?
Rudy::Huxtable.ld "CRITERIA: #{criteria.inspect}"
criteria
end
Expand Down Expand Up @@ -182,7 +186,8 @@ def initialize(rtype, opts={})
end

def name(*other)
parts = [@rtype, @zone, @project, @environment, @role, @position, *other]
project = (@project.is_a?(Array) && @project.first.empty?) ? nil : @project
parts = [@rtype, @zone, project, @environment, @role, @position, *other]
parts.compact.flatten.join Rudy::DELIM
end

Expand Down
3 changes: 3 additions & 0 deletions lib/rudy/routines/handlers/spot_request.rb
Expand Up @@ -21,6 +21,9 @@ def create
request = Rudy::AWS::EC2::SpotRequests.create(opts)
raise NoMachines unless wait_for_fulfillment_of(request)
Rudy::AWS::EC2::SpotRequests.list(request)
rescue NoMachines
Rudy::AWS::EC2::SpotRequests.cancel(request)
raise SpotRequestCancelled
end

def wait_for_fulfillment_of(spot_requests)
Expand Down

0 comments on commit 52627b6

Please sign in to comment.