[asterisk-users] Handling a long-running agi on hangup-handler?

Jonathan H lardconcepts at gmail.com
Sat Jan 20 02:36:15 CST 2018


Thanks Eric,

Well, with your guidance and after a fair bit of Googling, experimentation
and headscratching, here is the Python version for anyone who wants it....
Also works with other long AGI background tasks to quickly return to the
dialplan when something else is being processed.

import os, sys
from asterisk.agi import AGI
from speechrecgoogle import *

agi = AGI()

recFilename = str(agi.env['agi_arg_1'])

if os.fork():
    sys.exit(0)
else:
    os.setsid()
    os.close(sys.stdout.fileno())
    response_data = speechrec(recFilename)
    feedback = response_data.results[0].alternatives[0].transcript
    # You now have your plain text feedback which takes a few seconds, but
    # Asterisk has safely already returned to the dialplan or hangup
handler


Hope this helps someone!

On 18 January 2018 at 17:42, Eric Wieling <ewieling at nyigc.com> wrote:

>
> Asterisk (after 1.4?) sends the AGI a HUP when the call hangs up.
>
> Try setting your script to ignore the HUP signal and make it fork and go
> into the background so Asterisk thinks the process has completed.
>
> In PHP ignore HUP:
>
>     pcntl_signal(SIGHUP, SIG_IGN);
>
> In PHP fork and become a short lived daemon:
>
>     $pid = pcntl_fork();
>         if ($pid == -1) {
>             die("could not fork");
>         } elseif ($pid) {
>             exit; // we are the parent
>         }
>         // we are the child
>         // detatch from the controlling terminal so we don't become a
> zombie when we die.
>         if (posix_setsid() == -1) {
>             die("could not detach from terminal");
>
>         }
>
>
> On 01/18/2018 12:27 PM, Jonathan H wrote:
>
> I know that hangup handlers (https://wiki.asterisk.org/
> wiki/display/AST/Hangup+Handlers) have to finish quickly.
>
> So it's no surprise that my speech to text agi which takes 8 seconds gets
> killed.
>
> However, can anyone think of a way round this? So, once the caller has
> hung up, I need to take one of the channel variables, and pass it to a
> python agi script which then does speech to text.
>
> In-call, it works fine. After hangup, it doesn't. Which is correct, but
> any thoughts on ways round this?
>
> Thanks.
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-users/attachments/20180120/0bedbf0c/attachment.html>


More information about the asterisk-users mailing list