Created
February 25, 2013 10:02
-
-
Save andrius/5028865 to your computer and use it in GitHub Desktop.
How to daemonise Ruby AGI app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems like missed @@server.finish after @@server.start