JR Richardson
2010-Jan-07 23:27 UTC
[asterisk-users] AGI perl script set timeout within script?
Hi All, I'm running an AGI, calling a perl script the does number lookups to a remote server. I would like to put a timeout in the script. The problem I'm running into is if the DNS server is not responding, the script hangs and waits for 30 seconds before returning to the Asterisk dialplan. I would like a timeout of 1 second, then return. Here is my clean script: *********************************************************** #!/usr/bin/perl $|=1; ##################### Modules to Use ############### use Asterisk::AGI; $AGI = new Asterisk::AGI; my %input = $AGI->ReadParse(); # Set variables according to supplied arguments $number = $ARGV[0]; $AGI->exec("agi","agi://agi.server.com/script.agi?user=username&number=$number"); *********************************************************** Any assistance will be appreciated. Thanks. JR -- JR Richardson Engineering for the Masses
Steve Edwards
2010-Jan-08 00:09 UTC
[asterisk-users] AGI perl script set timeout within script?
On Thu, 7 Jan 2010, JR Richardson wrote:> I'm running an AGI, calling a perl script the does number lookups to a > remote server. I would like to put a timeout in the script. The > problem I'm running into is if the DNS server is not responding, the > script hangs and waits for 30 seconds before returning to the Asterisk > dialplan. I would like a timeout of 1 second, then return.I'm a C weenie, so I can't provide Perl code. But it should look something like this: // handle the alarm static void lookup_failed ( void ) { exit(EXIT_FAILURE); } int main ( ) ... // set a signal alarm handler signal(SIGALRM, (void (*)(int))(int)lookup_failed); // set the alarm to go off in 1 second alarm(1); // lookup my number do_lookup(dnis); // cancel the alarm alarm(0); ... Of course, solving the real issue (DNS lookups), or masking it with a local caching server or even a /etc/hosts file are viable alternatives. -- Thanks in advance, ------------------------------------------------------------------------- Steve Edwards sedwards at sedwards.com Voice: +1-760-468-3867 PST Newline Fax: +1-760-731-3000
David Backeberg
2010-Jan-08 00:59 UTC
[asterisk-users] AGI perl script set timeout within script?
On Thu, Jan 7, 2010 at 6:27 PM, JR Richardson <jmr.richardson at gmail.com> wrote:> problem I'm running into is if the DNS server is not responding, the > script hangs and waits for 30 seconds before returning to the Asterisk > dialplan. ?I would like a timeout of 1 second, then return.A few things... * stop using DNS? Problem solved. * put nagios monitoring on your DNS server? * put in a second DNS server, and tune your DNS timeout to a very low value in /etc/resolv.conf (read the man page) before jumping to next server? Or you could use the Perl language feature, which is called 'alarm'. Google around for some code samples. None of these are actually specific to asterisk, as it turns out. I don't know of any explicit asterisk method to force a timeout.
JR Richardson
2010-Jan-08 03:17 UTC
[asterisk-users] AGI perl script set timeout within script?
> >> On Thu, Jan 7, 2010 at 6:27 PM, JR Richardson > <jmr.richardson at gmail.com> > > wrote: > >>> problem I'm running into is if the DNS server is not responding, the > >>> script hangs and waits for 30 seconds before returning to the Asterisk > >>> dialplan. ?I would like a timeout of 1 second, then return. > >> > > On Thursday 07 January 2010 18:59:24 David Backeberg wrote: > >> > >> * stop using DNS? Problem solved. * put nagios monitoring on your DNS > >> server? * put in a second DNS server, and tune your DNS timeout to a > >> very low value in /etc/resolv.conf (read the man page) before jumping > >> to next server? > >> > >> Or you could use the Perl language feature, which is called 'alarm'. > >> Google around for some code samples. > > On Thu, 7 Jan 2010, Tilghman Lesher wrote: > > > Ah, but Perl isn't actually doing the DNS lookup. If you examine his > > script, he's merely passing back a name to the Asterisk process, which > > is then calling inet_aton(), which is the reason why he cannot control > > it from within the script. What he'd actually need to do is to start > > using Net::DNS to do the resolution on that name, first, perhaps even > > going as far as to connect to the server himself, and relay the channel > > between the AGI interface and the remote TCP interface. > > > > Then, he could use alarm() or the Time::Hires module to ensure his own > > timeouts override the builtins. But as it stands now, it's all > > Asterisk. > > If the DNS lookup is being done by Asterisk to resolve the FastAGI server > name. If the DNS lookup is for the (assumed) database server in his script > then the suggestions to use alarm() would do the trick. > > I guess we need clarification from the OP.I tend to agree with Tilghman on this. I tried the perl script eval, alarm, $SIG{ALRM} functions till I was blue in the face from cussing at the screen. It does not appear that the perl script is doing the DNS query, otherwise the eval alarm would timeout and pass control back to asterisk. Another indication is that '#define MAX_AGI_CONNECT 2000' in res_agi is not being invoked because the timeout is around 30 seconds. Is that 30 second timeout built into Asterisk? Can I put an absolute timeout on an agi script from the dialplan prior to calling the agi application? Maybe I'll fork a macro with a timeout, yea, that's it, let start forking, something new to cuss at. Thanks for your input guys. JR
JR Richardson
2010-Jan-09 00:33 UTC
[asterisk-users] AGI perl script set timeout within script?
> What about: > > 1) Fixing the slow responding DNS server? > > 2) Tweaking /etc/resolv.conf options? > > 3) Setting up a caching name server on your Asterisk host? > > 4) Adding the AGI server host name and IP address to /etc/hosts? > > 5) Using the IP address of the AGI server in your dialplan? >Ok, I went with #4 for a bit, then resolved to #5 (pardon the pun), works fine. Thanks. JR -- JR Richardson Engineering for the Masses