class Adhearsion::CLI::AhnCommand

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 14
def self.exit_on_failure?
  true
end

Public Instance Methods

create(path) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 20
def create(path)
  require 'adhearsion/generators/app/app_generator'
  Generators::AppGenerator.start
end
daemon(path = nil) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 48
def daemon(path = nil)
  start_app path, :daemon, options[:pidfile]
end
generate(generator_name = nil, *args) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 26
def generate(generator_name = nil, *args)
  if generator_name
    Generators.invoke generator_name
  else
    help 'generate'
  end
end
restart(path = nil) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 89
def restart(path = nil)
  path = execute_from_app_dir! path
  begin
    invoke :stop
  rescue PIDReadError => e
    puts e.message
  end
  invoke :daemon
end
start(path = nil) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 42
def start(path = nil)
  start_app path, options[:noconsole] ? :simple : :console
end
stop(path = nil) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 54
def stop(path = nil)
  path = execute_from_app_dir! path

  pid_file = if options[:pidfile]
    File.exists?(File.expand_path(options[:pidfile])) ?
      options[:pidfile] :
      File.join(path, options[:pidfile])
  else
    path = Dir.pwd
    File.join path, Adhearsion::Initializer::DEFAULT_PID_FILE_NAME
  end
  pid_file = File.expand_path pid_file

  begin
    pid = File.read(pid_file).to_i
  rescue
    raise PIDReadError, pid_file
  end

  raise PIDReadError, pid_file if pid.nil?

  say "Stopping Adhearsion server at #{path} with pid #{pid}"
  waiting_timeout = Time.now + 15
  begin
    ::Process.kill "TERM", pid
    sleep 0.25 while process_exists?(pid) && Time.now < waiting_timeout
    ::Process.kill "KILL", pid
  rescue Errno::ESRCH
  end

  File.delete pid_file if File.exists? pid_file
end
version() click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 35
def version
  say "Adhearsion v#{Adhearsion::VERSION}"
  exit 0
end

Protected Instance Methods

execute_from_app_dir!(path) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 107
def execute_from_app_dir!(path)
  if in_app? and running_script_ahn?
    return Dir.pwd
  end

  path ||= Dir.pwd if in_app?

  raise PathRequired, ARGV[0] if path.nil? or path.empty?

  Dir.chdir path do
    raise PathInvalid, path unless ScriptAhnLoader.in_ahn_application?
    args = ARGV.dup
    args[1] = '.'
    ScriptAhnLoader.exec_script_ahn! args
  end
  path
end
in_app?() click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 129
def in_app?
  ScriptAhnLoader.in_ahn_application? or ScriptAhnLoader.in_ahn_application_subdirectory?
end
method_missing(action, *args) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 139
def method_missing(action, *args)
  help
  raise UnknownCommand, [action, *args] * " "
end
process_exists?(pid = nil) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 133
def process_exists?(pid = nil)
  # FIXME: Raise some error here
  return false if pid.nil?
  `ps -p #{pid} | sed -e '1d'`.strip.empty?
end
running_script_ahn?() click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 125
def running_script_ahn?
  $0.to_s == "script/ahn"
end
start_app(path, mode, pid_file = nil) click to toggle source
# File lib/adhearsion/cli_commands/ahn_command.rb, line 101
def start_app(path, mode, pid_file = nil)
  path = execute_from_app_dir! path
  say "Starting Adhearsion server at #{Dir.pwd}"
  Adhearsion::Initializer.start :mode => mode, :pid_file => pid_file
end