lib/cfoundry/v2/app.rb in cfoundry-4.5.3 vs lib/cfoundry/v2/app.rb in cfoundry-4.6.0

- old
+ new

@@ -50,18 +50,16 @@ def delete!(opts = {}) super(opts.merge(:recursive => true)) end def instances - @client.base.instances(@guid).collect do |i, m| - Instance.new(self, i.to_s, @client, m) - end + AppInstance.for_app(@client, @guid, name) end def crashes @client.base.crashes(@guid).collect do |m| - Instance.new(self, m[:instance], @client, m) + AppInstance.new(self.name, self.guid, m[:instance], @client, m) end end def stats stats = {} @@ -201,10 +199,23 @@ end rescue CFoundry::StagingError, CFoundry::NotStaged "STAGING FAILED" end + def percent_running + if state == "STARTED" + healthy_count = running_instances + expected = total_instances + + expected > 0 ? (healthy_count / expected.to_f) * 100 : 0 + else + 0 + end + rescue CFoundry::StagingError, CFoundry::NotStaged + 0 + end + def running_instances return @cache[:running_instances] if @cache[:running_instances] running = 0 @@ -264,19 +275,19 @@ b.service_instance == instance } end def files(*path) - Instance.new(self, "0", @client).files(*path) + AppInstance.new(self.name, self.guid, "0", @client).files(*path) end def file(*path) - Instance.new(self, "0", @client).file(*path) + AppInstance.new(self.name, self.guid, "0", @client).file(*path) end def stream_file(*path, &blk) - Instance.new(self, "0", @client).stream_file(*path, &blk) + AppInstance.new(self.name, self.guid, "0", @client).stream_file(*path, &blk) end private def stringify(hash) @@ -285,73 +296,8 @@ hash.each do |k, v| new[k.to_s] = v.to_s end new - end - - class Instance - attr_reader :app, :id - - def initialize(app, id, client, manifest = {}) - @app = app - @id = id - @client = client - @manifest = manifest - end - - def inspect - "#<App::Instance '#{@app.name}' \##@id>" - end - - def state - @manifest[:state] - end - alias_method :status, :state - - def since - if since = @manifest[:since] - Time.at(@manifest[:since]) - end - end - - def debugger - return unless @manifest[:debug_ip] and @manifest[:debug_port] - - { :ip => @manifest[:debug_ip], - :port => @manifest[:debug_port] - } - end - - def console - return unless @manifest[:console_ip] and @manifest[:console_port] - - { :ip => @manifest[:console_ip], - :port => @manifest[:console_port] - } - end - - def healthy? - case state - when "STARTING", "RUNNING" - true - when "DOWN", "FLAPPING" - false - end - end - - def files(*path) - @client.base.files(@app.guid, @id, *path).split("\n").collect do |entry| - path + [entry.split(/\s+/, 2)[0]] - end - end - - def file(*path) - @client.base.files(@app.guid, @id, *path) - end - - def stream_file(*path, &blk) - @client.base.stream_file(@app.guid, @id, *path, &blk) - end end end end