[Freeswitch-users] Re- End Lua script after HangupHook handled without all the extra code to handle the return to the function
Abaci B
abaci64 at gmail.com
Fri Jul 8 03:20:00 MSD 2016
I just got around to test this again and here is what I found.
from within the hangup hook you can exit using return "exit" and as noted
by Andrew it will exit with an error unless you set debug.traceback=nil
from the main code (not within a function) you can exit by using return
"exit" (or return anything else) which is just lua exiting but it has to be
at the end of a block (wither a loop or you can just return "exit" end).
from within a function there seems to be no way to exit the lua script, for
scripts that are session based you can probably use session:hangup() along
with calling return "exit" from the hangup hook, but on any other lua
scripts (api, event hooks, dialplan hook, startup) there seems to be no way.
So my question is if anyone know another way to exit a lua script from
within a function or if I should open a Jira.
On Thu, Mar 10, 2016 at 4:21 PM, Abaci B <abaci64 at gmail.com> wrote:
> I would expect to be able to exit from a lua script without a while loop,
> should I open a feature request on Jira for that?
>
> On Wed, Mar 9, 2016 at 1:20 AM, Chad Phillips <chad at apartmentlines.com>
> wrote:
>
>> Andrew,
>>
>> Curious if this structure will work at the bottom of either a a Lua
>> script or Lua function:
>>
>> do
>> return "exit"
>> end
>>
>> I seem to recall that being a trick that helped for weird 'return' cases.
>>
>> Chad
>>
>> On Tue, Mar 8, 2016 at 12:54 PM, Andrew Keil <andrew.keil at visytel.com>
>> wrote:
>>
>>> Michael,
>>>
>>>
>>>
>>> I have just run some more tests and you are correct that calling it
>>> inside a while loop like what you explained below does work outside of the
>>> hangup hook handler. Thanks for providing this extra information.
>>>
>>>
>>>
>>> It should be noted that it does not work on its own simply at the bottom
>>> of a Lua function (ie. as the last statement without a while loop) -
>>> except as the last statement inside the hangup hook function.
>>>
>>>
>>>
>>> ie. This works:
>>>
>>> ----------------------------------------------------------------
>>>
>>>
>>>
>>>
>>>
>>> function CleanUp()
>>>
>>> freeswitch.consoleLog("CLEANUP SECTION\n")
>>>
>>> freeswitch.consoleLog("CLEANUP SECTION COMPLETE\n")
>>>
>>> end
>>>
>>>
>>>
>>> function myHangupHook(s, status, arg)
>>>
>>> freeswitch.consoleLog(string.format("%s DETECTED\n",arg))
>>>
>>> session:hangup()
>>>
>>> -- Run CleanUp function now since the caller has
>>> disconnected
>>>
>>> CleanUp()
>>>
>>> -- Abort Lua script here to avoid returning to
>>> MainService()
>>>
>>> return "exit"
>>>
>>> end
>>>
>>> -- Setup Hangup event handler here
>>>
>>> v_hangup = "HANGUP"
>>>
>>> session:setHangupHook("myHangupHook", "v_hangup")
>>>
>>>
>>>
>>> Whereas this does not work:
>>>
>>> ----------------------------------------------------------------
>>>
>>>
>>>
>>> function CleanUp()
>>>
>>> freeswitch.consoleLog("CLEANUP SECTION\n")
>>>
>>> freeswitch.consoleLog("CLEANUP SECTION COMPLETE\n")
>>>
>>> return "exit"
>>>
>>> end
>>>
>>>
>>>
>>> function myHangupHook(s, status, arg)
>>>
>>> freeswitch.consoleLog(string.format("%s DETECTED\n",arg))
>>>
>>> session:hangup()
>>>
>>> -- Run CleanUp function now since the caller has
>>> disconnected
>>>
>>> CleanUp()
>>>
>>> end
>>>
>>> -- Setup Hangup event handler here
>>>
>>> v_hangup = "HANGUP"
>>>
>>> session:setHangupHook("myHangupHook", "v_hangup")
>>>
>>>
>>>
>>> Regards,
>>>
>>>
>>>
>>> Andrew
>>>
>>>
>>>
>>> *From:* freeswitch-users-bounces at lists.freeswitch.org [mailto:
>>> freeswitch-users-bounces at lists.freeswitch.org] *On Behalf Of *Michael
>>> Collins
>>> *Sent:* Saturday, 5 March 2016 8:11 AM
>>> *To:* FreeSWITCH Users Help <freeswitch-users at lists.freeswitch.org>
>>> *Subject:* Re: [Freeswitch-users] Re- End Lua script after HangupHook
>>> handled without all the extra code to handle the return to the function
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Fri, Mar 4, 2016 at 9:19 AM, Abaci B <abaci64 at gmail.com> wrote:
>>>
>>> The question is not how to figure out when to exit the lua script, the
>>> question is *how to exit the lua script*, and that can sometimes be
>>> tricky or complicated as return "exit" only works from hangup hook.
>>>
>>>
>>>
>>> Are you positive that it works only from a hangup hook? It seems to work
>>> at the end of any loop:
>>>
>>>
>>>
>>> -- testing exit (no session, call with luarun)
>>>
>>> freeswitch.consoleLog('INFO',"Starting infinite loop...\n")
>>>
>>> while(1) do
>>>
>>> freeswitch.consoleLog('WARNING',"Before exit...\n")
>>>
>>> return "exit"
>>>
>>> end
>>>
>>> freeswitch.consoleLog('INFO',"All done!\n")
>>>
>>>
>>>
>>>
>>>
>>> Or with a session:
>>>
>>> -- test exit with session, no hangup hook
>>>
>>> session:answer()
>>>
>>> freeswitch.consoleLog('INFO',"Entering main loop...\n")
>>>
>>> while ( session:ready() == true ) do
>>>
>>> freeswitch.consoleLog('WARNING',"Inside loop...\n")
>>>
>>> return "exit"
>>>
>>> end
>>>
>>> freeswitch.consoleLog('INFO',"All done!\n")
>>>
>>>
>>>
>>> In both cases I never see "All done!" at the CLI. Can you try it and see
>>> if there's a scenario where it does not exit as expected?
>>>
>>>
>>>
>>> -MC
>>>
>>>
>>>
>>> _________________________________________________________________________
>>> Professional FreeSWITCH Consulting Services:
>>> consulting at freeswitch.org
>>> http://www.freeswitchsolutions.com
>>>
>>> Official FreeSWITCH Sites
>>> http://www.freeswitch.org
>>> http://confluence.freeswitch.org
>>> http://www.cluecon.com
>>>
>>> FreeSWITCH-users mailing list
>>> FreeSWITCH-users at lists.freeswitch.org
>>> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
>>> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
>>> http://www.freeswitch.org
>>>
>>
>>
>> _________________________________________________________________________
>> Professional FreeSWITCH Consulting Services:
>> consulting at freeswitch.org
>> http://www.freeswitchsolutions.com
>>
>> Official FreeSWITCH Sites
>> http://www.freeswitch.org
>> http://confluence.freeswitch.org
>> http://www.cluecon.com
>>
>> FreeSWITCH-users mailing list
>> FreeSWITCH-users at lists.freeswitch.org
>> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
>> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
>> http://www.freeswitch.org
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20160707/1f7355e6/attachment-0001.html
Join us at ClueCon 2016 Aug 8-12, 2016
More information about the FreeSWITCH-users
mailing list