[asterisk-users] AGI and forking
Thorsten Göllner
tg at ovm-group.com
Wed Apr 13 08:28:24 CDT 2011
Am 13.04.2011 15:08, schrieb A J Stiles:
> Hi. I just want to make sure I understand this before doing something that
> might break things spectacularly for our users and customers :)
>
> We are using Asterisk 1.6.2.9 and my programming language of choice is Perl.
>
> I want, when a call comes in on someone's DDI number (which the person who
> dialled it can only possibly have obtained by dialling 1471 after we called
> them), to be able to look up the caller's details from one of our databases
> (where the number ought to be stored, because we already dialled it).
>
> Now, this search is going to take some time; so I'd like for the AGI script to
> fork a clone of itself, so the parent process can exit and the dialplan
> continue on to ring the person's phone, while the database lookup is done in
> the background (the script doesn't need to have any further contact with
> Asterisk -- it will initiate any necessary future communication via other
> channels).
>
>
> Is this the sort of thing I need?
>
> ########## begin code snippet ##########
>
> #!/usr/bin/perl -w
> use strict;
> use Asterisk::AGI;
>
> my $AGI = new Asterisk::AGI;
> my %params = $AGI->ReadParse();
>
> $SIG{CHLD} = "IGNORE";
>
> if (my $child_pid = fork) {
> # This is executed in the parent process
> exit;
> }
> elsif (defined $child_pid) {
> # This is executed in the child process
>
> close STDIN;
> close STDOUT;
> close STDERR;
>
> # Load some more modules and do some stuff
> # that will take a long time
>
> exit;
> }
> else {
> die "Could not fork: $!";
> };
>
> ########## end code snippet ##########
>
> Am I right in thinking I shouldn't have to worry about zombie processes,
> because the parent exits before the child and the init in modern Linux
> distros is smart enough to deal with orphaned processes itself?
>
It should work - I think. BUT I am not really sure what will happen, if
the child process exits. The child works with a copy of all asterisk
ressources given to it, when forking. So when the child dies, perhaps
asterisk will do a hangup or continues in the dialplan for this process.
I think, that could cause some unwanted results.
You should try to write a daemon process which handles the database
lookups or whataver while being totally independent of the atserisk
process. This is a little bit more overhead for the communication
between the astersk process (the call itself) and your lookup-daemon.
But it would be more stable - for my point of view.
-Thorsten-
More information about the asterisk-users
mailing list