Module: Larynx::Server

Defined in:
lib/larynx/server.rb

Class Method Summary collapse

Class Method Details

.bootObject



8
9
10
11
12
13
14
15
# File 'lib/larynx/server.rb', line 8

def boot
  parse_options(ARGV)
  setup_app
  daemonize if @options[:daemonize]
  setup_logger
  trap_signals
  start_server
end

.daemonizeObject



55
56
57
58
59
# File 'lib/larynx/server.rb', line 55

def daemonize
  Daemonize.daemonize
  Dir.chdir LARYNX_ROOT
  File.open(@options[:pid_file], 'w+') {|f| f.write("#{Process.pid}\n") }
end

.graceful_exitObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/larynx/server.rb', line 44

def graceful_exit
  msg = "Shutting down Larynx"
  $stderr.puts msg unless @options[:daemon]
  LARYNX_LOGGER.info msg

  EM.stop_server @em_signature
  @em_signature = nil
  remove_pid_file if @options[:daemonize]
  exit 130
end

.parse_options(args = ARGV) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/larynx/server.rb', line 17

def parse_options(args=ARGV)
  @options = {
    :ip       => "0.0.0.0",
    :port     => 8084,
    :pid_file => "./larynx.pid",
    :log_file => "./larynx.log"
  }
  opts = OptionParser.new
  opts.banner = "Usage: larynx [options] app_file"
  opts.separator ''
  opts.separator "Larynx is a framework to develop FreeSWITCH IVR applications in Ruby."
  opts.on('-i', '--ip IP',         'Listen for connections on this IP') {|ip| @options[:ip] = ip }
  opts.on('-p', '--port PORT',     'Listen on this port', Integer)      {|port| @options[:port] = port }
  opts.on('-d', '--daemonize',     'Run as daemon')                     { @options[:daemonize] = true }
  opts.on('-l', '--log-file FILE', 'Defaults to /app/root/larynx.log')  {|log| @options[:log_file] = log }
  opts.on(      '--pid-file FILE', 'Defaults to /app/root/larynx.pid')  {|pid| @options[:pid_file] = pid }
  opts.on('-h', '--help',          'This is it')                        { $stderr.puts opts; exit 0 }
  opts.on('-v', '--version')                                            { $stderr.puts "Larynx version #{Larynx::VERSION}"; exit 0 }
  opts.parse!(args)
end

.remove_pid_fileObject



61
62
63
# File 'lib/larynx/server.rb', line 61

def remove_pid_file
  File.delete @options[:pid_file]
end

.running?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/larynx/server.rb', line 89

def running?
  !@em_signature.nil?
end

.setup_appObject



70
71
72
73
74
75
76
77
# File 'lib/larynx/server.rb', line 70

def setup_app
  if ARGV[0].nil?
    $stderr.puts "You must specify an application file"
    exit -1
  end
  Object.const_set "LARYNX_ROOT", File.expand_path(File.dirname(ARGV[0]))
  require File.expand_path(ARGV[0])
end

.setup_loggerObject



38
39
40
41
42
# File 'lib/larynx/server.rb', line 38

def setup_logger
  logger = Larynx::Logger.new(@options[:log_file])
  logger.level = Logger::INFO
  Object.const_set "LARYNX_LOGGER", logger
end

.start_serverObject



79
80
81
82
83
84
85
86
87
# File 'lib/larynx/server.rb', line 79

def start_server
  msg = "Larynx starting up on #{@options[:ip]}:#{@options[:port]}"
  $stderr.puts msg unless @options[:daemon]
  LARYNX_LOGGER.info msg

  EM::run {
    @em_signature = EM::start_server @options[:ip], @options[:port], Larynx::CallHandler
  }
end

.trap_signalsObject



65
66
67
68
# File 'lib/larynx/server.rb', line 65

def trap_signals
  trap('TERM') { graceful_exit }
  trap('INT')  { graceful_exit }
end