class ElasticDot::Command::Ps

Public Class Methods

list(opts) click to toggle source
# File lib/elasticdot/command/ps.rb, line 65
def self.list(opts)
  require 'time'

  find_app! opts

  loop do

    info = api.get("/domains/#{@app}")

    tier = info['dot_tier']
    dots = info['dots']

    puts "=== web (#{tier['name']}): #{info['procfile']}"
    dots.each_with_index do |dot, i|
      now     = Time.now
      elapsed = now - Time.parse(dot['started_at'])
      since   = time_ago(now - elapsed)

      puts "web.#{i+1}: up #{since} cpu: #{dot['cpu_load']}%"
    end

    break unless opts[:follow]
    sleep 2
    system 'clear'
  end
end
resize(settings, opts) click to toggle source
# File lib/elasticdot/command/ps.rb, line 2
def self.resize(settings, opts)
  find_app! opts

  params = apps_info @app

  web = nil
  settings.each do |s|
    p, v = s.split('=',2)
    web = v and break if p == 'web'
  end

  unless web
    puts 'At the moment you can only resize web processes'
    exit 1
  end

  params[:tier] = web

  api.put "/websites/#{@app}/scaling", params

  spinner "Resizing dots and restarting specified processes..." do
    loop do
      sleep 3
      info = api.get "/domains/#{@app}"
      break if info['status'] == 'active'
    end
  end
end
restart(opts) click to toggle source
# File lib/elasticdot/command/ps.rb, line 92
def self.restart(opts)
  find_app! opts

  api.post "/apps/#{@app}/restart"

  spinner 'Restarting dots...' do
    loop do
      sleep 3
      info = api.get "/domains/#{@app}"
      break if info['status'] == 'active'
    end
  end
end
scale(settings, opts) click to toggle source
# File lib/elasticdot/command/ps.rb, line 31
def self.scale(settings, opts)
  find_app! opts

  params = apps_info @app

  web, mode = nil
  settings.each do |s|
    p, v = s.split('=',2)
    web  = v if p == 'web'
    mode = v if p == 'mode'
  end

  unless web
    puts 'Usage: elasticdot ps:scale web=N [mode=auto|manual]'
    exit 1
  end

  params[:scaling] = mode if mode
  params[:dots]    = web

  api.put "/websites/#{@app}/scaling", params
  info = apps_info @app

  spinner "Scaling web processes..." do
    loop do
      sleep 3
      info = api.get "/domains/#{@app}"
      break if info['status'] == 'active'
    end
  end

  puts "Now running #{info[:dots]} dots"
end
stop(opts) click to toggle source
# File lib/elasticdot/command/ps.rb, line 106
def self.stop(opts)
  find_app! opts

  spinner "Stopping dots..." do
    api.post "/apps/#{@app}/stop"
  end
end

Private Class Methods

apps_info(app) click to toggle source
# File lib/elasticdot/command/ps.rb, line 115
def self.apps_info(app)
  info = api.get "/domains/#{@app}"

  app_tier = info['production'] ? 'production' : 'development'
  {app_tier: app_tier, scaling: info['scaling'], tier: info['dot_tier']['name'], dots: info['min_dots'] }
end
time_ago(since) click to toggle source
# File lib/elasticdot/command/ps.rb, line 122
def self.time_ago(since)
  if since.is_a?(String)
    since = Time.parse(since)
  end

  elapsed = Time.now - since

  message = since.strftime("%Y/%m/%d %H:%M:%S")
  if elapsed <= 60
    message << " (~ #{elapsed.floor}s ago)"
  elsif elapsed <= (60 * 60)
    message << " (~ #{(elapsed / 60).floor}m ago)"
  elsif elapsed <= (60 * 60 * 25)
    message << " (~ #{(elapsed / 60 / 60).floor}h ago)"
  end
  message
end