class Heroku::Command::Run

run one-off commands (console, rake)

Public Instance Methods

console() click to toggle source

run:console [COMMAND]

open a remote console session

if COMMAND is specified, run the command and exit

# File lib/heroku/command/run.rb, line 88
def console
  cmd = args.join(' ').strip
  if cmd.empty?
    console_session(app)
  else
    display heroku.console(app, cmd)
  end
rescue RestClient::RequestTimeout
  error "Timed out. Long running requests are not supported on the console.\nPlease consider creating a rake task instead."
rescue Heroku::Client::AppCrashed => e
  error e.message
end
detached() click to toggle source

run:detached COMMAND

run a detached process, where output is sent to your logs

# File lib/heroku/command/run.rb, line 50
def detached
  command = args.join(" ")
  fail "Usage: heroku run COMMAND" if command.empty?
  opts = { :command => command }
  display "Running #{command}... ", false
  begin
    ps = heroku.ps_run(app, opts)
    puts "up, #{ps["process"]}"
    puts "Use 'heroku logs -p #{ps["process"]}' to view the log output."
  rescue
    puts "failed"
    raise
  end
end
index() click to toggle source

run COMMAND

run an attached process

# File lib/heroku/command/run.rb, line 12
def index
  command = args.join(" ")
  fail "Usage: heroku run COMMAND" if command.empty?
  opts = { :attach => true, :command => command, :ps_env => get_terminal_environment }
  display "Running #{command} attached to terminal... ", false

  begin
    ps = heroku.ps_run(app, opts)
  rescue
    puts "failed"
    raise
  end

  begin
    set_buffer(false)
    $stdin.sync = $stdout.sync = true
    rendezvous = Heroku::Client::Rendezvous.new(
      :rendezvous_url => ps["rendezvous_url"],
      :connect_timeout => (ENV['HEROKU_CONNECT_TIMEOUT'] || 120).to_i,
      :activity_timeout => nil,
      :input => $stdin,
      :output => $stdout)
    rendezvous.on_connect { display "up, #{ps["process"]}" }
    rendezvous.start
  rescue Timeout::Error
    error "\nTimeout awaiting process"
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET, OpenSSL::SSL::SSLError
    error "\nError connecting to process"
  rescue Interrupt
  ensure
    set_buffer(true)
  end
end
rake() click to toggle source

run:rake COMMAND

remotely execute a rake command

# File lib/heroku/command/run.rb, line 69
def rake
  cmd = args.join(' ')
  if cmd.length == 0
    raise Heroku::Command::CommandFailed, "Usage: heroku run:rake COMMAND"
  else
    heroku.start(app, "rake #{cmd}", :attached).each { |chunk| display(chunk, false) }
  end
rescue Heroku::Client::AppCrashed => e
  error "Couldn't run rake\n#{e.message}"
end

Protected Instance Methods

console_history_add(app, cmd) click to toggle source
# File lib/heroku/command/run.rb, line 144
def console_history_add(app, cmd)
  Readline::HISTORY.push(cmd)
  File.open(console_history_file(app), "a") { |f| f.puts cmd + "\n" }
end
console_history_dir() click to toggle source
# File lib/heroku/command/run.rb, line 105
def console_history_dir
  FileUtils.mkdir_p(path = "#{home_directory}/.heroku/console_history")
  path
end
console_history_file(app) click to toggle source
# File lib/heroku/command/run.rb, line 125
def console_history_file(app)
  "#{console_history_dir}/#{app}"
end
console_history_read(app) click to toggle source
# File lib/heroku/command/run.rb, line 129
def console_history_read(app)
  history = File.read(console_history_file(app)).split("\n")
  if history.size > 50
    history = history[(history.size - 51),(history.size - 1)]
    File.open(console_history_file(app), "w") { |f| f.puts history.join("\n") }
  end
  history.each { |cmd| Readline::HISTORY.push(cmd) }
rescue Errno::ENOENT
rescue Exception => ex
  display "Error reading your console history: #{ex.message}"
  if confirm("Would you like to clear it? (y/N):")
    FileUtils.rm(console_history_file(app)) rescue nil
  end
end
console_session(app) click to toggle source
# File lib/heroku/command/run.rb, line 110
def console_session(app)
  heroku.console(app) do |console|
    console_history_read(app)

    display "Ruby console for #{app}.#{heroku.host}"
    while cmd = Readline.readline('>> ')
      unless cmd.nil? || cmd.strip.empty?
        console_history_add(app, cmd)
        break if cmd.downcase.strip == 'exit'
        display console.run(cmd)
      end
    end
  end
end