Skip to content

Instantly share code, notes, and snippets.

@andrius
Created February 25, 2013 10:02
Show Gist options
  • Save andrius/5028865 to your computer and use it in GitHub Desktop.
Save andrius/5028865 to your computer and use it in GitHub Desktop.
How to daemonise Ruby AGI app.
#!/usr/bin/env ruby
# NOTE - Following code was tested with AsteriskRuby gem and Adhearsion v. 1.2.3
require 'rubygems'
require 'pp'
require 'monitor'
require 'timeout'
require 'time'
require 'AGIServer'
require 'daemons'
class YourAGIAppClass
# ...
class << self
def start
begin
unless defined?(@@server)
@@logger = Logger.new(STDERR)
@@logger.level = $in_debug_mode ? Logger::DEBUG : Logger::FATAL
@@server = AGIServer.new :bind_port => 30303, :bind_host => '127.0.0.1',
:min_workers => 2, :max_workers => 2, :jobs_per_worker => 2,
:logger => @@logger, :stats => false
@@server.start
end
rescue => err
pp err.backtrace
end
end
def stop
begin
sleep 10
@@server.shutdown if defined?(@@server)
puts "CALL SERVER STOPPED!!!"
rescue => err
pp err.backtrace
end
end
end
end
def daemonize
daemon_specs = { :dir_mode => :normal,
:dir => "/var/log/asterisk/", # directory where pid file will be stored
:backtrace => true,
#:monitor => true,
:log_output => true
}
Daemons.run_proc("call-logger", daemon_specs) do
YourAGIAppClass::start
trap("INT") { CallServer::stop }
loop do
sleep CHECK_INTERVAL
YourAGIAppClass::do_periodic_taks
end
end
end
daemonize
@gemasergey
Copy link

It seems like missed @@server.finish after @@server.start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment