exit lua script in hangup hook
Hello, i'm trying to exit my lua script (called from dialplan) to exit in the the hanguphook: function on_hangup(s,status) freeswitch.consoleLog("NOTICE","---- on_hangup: "..status.."\n"); error(); end freeswitch.consoleLog("NOTICE","---- ANSWER:\n"); session:answer(); freeswitch.consoleLog("NOTICE","---- SETHOOK: \n"); session:setHangupHook("on_hangup"); while (session:ready() == true) do freeswitch.consoleLog("NOTICE","---- START OF LOOP \n"); freeswitch.consoleLog("NOTICE","---- STREAMFILE \n"); session:streamFile("/usr/local/freeswitch/sounds/en/us/callie/ivr/8000/ivr-on_hold_indefinitely.wav"); freeswitch.consoleLog("NOTICE","---- END OF SCRIPT 1 -> HANGUP\n"); end freeswitch.consoleLog("NOTICE","---- END OF SCRIPT 2 -> HANGUP\n"); session:hangup() When I hangup during the streamFile Call, I geht the following output: 2012-01-25 12:10:19.936097 [NOTICE] switch_channel.c:930 New Channel sofia/internal/2001@192.168.20.73 [2b1cd9f6-4745-11e1-8c71-0d2938abf0b5] 2012-01-25 12:10:19.936097 [INFO] mod_dialplan_xml.c:481 Processing B#-FS-2001 <2001>->3001 in context default 2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- ANSWER: 2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:599 Channel [sofia/internal/2001@192.168.20.73] has been answered 2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- SETHOOK: 2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- START OF LOOP 2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- STREAMFILE 2012-01-25 12:10:21.116050 [NOTICE] sofia.c:624 Hangup sofia/internal/2001@192.168.20.73 [CS_EXECUTE] [NORMAL_CLEARING] 2012-01-25 12:10:21.116050 [NOTICE] switch_cpp.cpp:1227 ---- on_hangup: hangup 2012-01-25 12:10:21.116050 [NOTICE] switch_cpp.cpp:1227 ---- END OF SCRIPT 1 -> HANGUP 2012-01-25 12:10:21.116050 [NOTICE] switch_cpp.cpp:1227 ---- END OF SCRIPT 2 -> HANGUP 2012-01-25 12:10:21.116050 [NOTICE] switch_core_session.c:1398 Session 2 (sofia/internal/2001@192.168.20.73) Ended 2012-01-25 12:10:21.116050 [NOTICE] switch_core_session.c:1400 Close Channel sofia/internal/2001@192.168.20.73 [CS_DESTROY] Why does the hangup handler return and the rest of the script is executed ? I event tried "do return end;" , "exit;" ," exit();" instead of "error(); - none of them works. If i put the same logic into javascript, the hook works and the script is terminated: function on_hangup(s,status) { console_log("NOTICE","---- hangup "+status+"\n"); return "exit"; } console_log("NOTICE","---- ANSWER\n"); session.answer(); console_log("NOTICE","---- ANSWER\n"); session.setHangupHook(on_hangup); while(session.ready()) { console_log("NOTICE","---- START OF LOOP\n"); session.streamFile("/usr/local/freeswitch/sounds/en/us/callie/ivr/8000/ivr-on_hold_indefinitely.wav"); console_log("NOTICE","---- END OF LOOP\n"); } console_log("NOTICE","---- END OF SCRIPT\n"); session.hangup(); 2012-01-25 12:13:08.396041 [NOTICE] switch_channel.c:930 New Channel sofia/internal/2001@192.168.20.73 [8f85a15c-4745-11e1-8c76-0d2938abf0b5] 2012-01-25 12:13:08.396041 [INFO] mod_dialplan_xml.c:481 Processing B#-FS-2001 <2001>->3002 in context default 2012-01-25 12:13:08.396041 [NOTICE] hangup.js:1 ---- ANSWER 2012-01-25 12:13:08.396041 [NOTICE] mod_spidermonkey.c:2068 Channel [sofia/internal/2001@192.168.20.73] has been answered 2012-01-25 12:13:08.396041 [NOTICE] hangup.js:1 ---- ANSWER 2012-01-25 12:13:08.396041 [NOTICE] hangup.js:1 ---- START OF LOOP 2012-01-25 12:13:09.875996 [NOTICE] sofia.c:624 Hangup sofia/internal/2001@192.168.20.73 [CS_EXECUTE] [NORMAL_CLEARING] 2012-01-25 12:13:09.875996 [NOTICE] hangup.js:2 ---- hangup hangup 2012-01-25 12:13:09.875996 [NOTICE] switch_core_session.c:1398 Session 3 (sofia/internal/2001@192.168.20.73) Ended 2012-01-25 12:13:09.875996 [NOTICE] switch_core_session.c:1400 Close Channel sofia/internal/2001@192.168.20.73 [CS_DESTROY] Any suggestions ? Or do i miss something here ? The dialplan part: <extension name="hanguptest"> <condition field="destination_number" expression="^(3001)$"> <action application="set" data="session_in_hangup_hook=true"/> <action application="lua" data="hangup.lua"/> </condition> </extension> (replace hangup.lua with hangup.js for the javascript version) Freeswitch Version: FreeSWITCH Version 1.0.head (git-9be51d5 2012-01-21 13-45-21 -0500) thx®ards, gw
Gerald, This is a feature/quirk of Lua. There is no explicit exit command. The script keeps running until there are no more commands. You'll need to restructure your loops so that when someone hangups it breaks out of the outermost loop and then has nothing left to execute. -MC On Wed, Jan 25, 2012 at 3:17 AM, Gerald Weber <gerald.weber@besharp.at>wrote:
Hello,****
** **
i’m trying to exit my lua script (called from dialplan) to exit in the the hanguphook:****
** **
function on_hangup(s,status)****
freeswitch.consoleLog("NOTICE","---- on_hangup: "..status.."\n");* ***
error(); ****
end****
** **
freeswitch.consoleLog("NOTICE","---- ANSWER:\n");****
session:answer();****
freeswitch.consoleLog("NOTICE","---- SETHOOK: \n");****
session:setHangupHook("on_hangup");****
** **
while (session:ready() == true) do****
freeswitch.consoleLog("NOTICE","---- START OF LOOP \n");****
freeswitch.consoleLog("NOTICE","---- STREAMFILE \n");****
session:streamFile("/usr/local/freeswitch/sounds/en/us/callie/ivr/8000/ivr-on_hold_indefinitely.wav"); ****
freeswitch.consoleLog("NOTICE","---- END OF SCRIPT 1 -> HANGUP\n"); ****
end****
** **
freeswitch.consoleLog("NOTICE","---- END OF SCRIPT 2 -> HANGUP\n");****
session:hangup()****
** **
When I hangup during the streamFile Call, I geht the following output:*** *
** **
2012-01-25 12:10:19.936097 [NOTICE] switch_channel.c:930 New Channel sofia/internal/2001@192.168.20.73 [2b1cd9f6-4745-11e1-8c71-0d2938abf0b5]** **
2012-01-25 12:10:19.936097 [INFO] mod_dialplan_xml.c:481 Processing B#-FS-2001 <2001>->3001 in context default****
2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- ANSWER:****
2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:599 Channel [sofia/internal/2001@192.168.20.73] has been answered****
2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- SETHOOK:****
2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- START OF LOOP ****
2012-01-25 12:10:19.936097 [NOTICE] switch_cpp.cpp:1227 ---- STREAMFILE*** *
2012-01-25 12:10:21.116050 [NOTICE] sofia.c:624 Hangup sofia/internal/ 2001@192.168.20.73 [CS_EXECUTE] [NORMAL_CLEARING]****
2012-01-25 12:10:21.116050 [NOTICE] switch_cpp.cpp:1227 ---- on_hangup: hangup****
2012-01-25 12:10:21.116050 [NOTICE] switch_cpp.cpp:1227 ---- END OF SCRIPT 1 -> HANGUP****
2012-01-25 12:10:21.116050 [NOTICE] switch_cpp.cpp:1227 ---- END OF SCRIPT 2 -> HANGUP****
2012-01-25 12:10:21.116050 [NOTICE] switch_core_session.c:1398 Session 2 (sofia/internal/2001@192.168.20.73) Ended****
2012-01-25 12:10:21.116050 [NOTICE] switch_core_session.c:1400 Close Channel sofia/internal/2001@192.168.20.73 [CS_DESTROY]****
** **
Why does the hangup handler return and the rest of the script is executed ? ****
I event tried “do return end;” , “exit;” ,” exit();” instead of “error(); - none of them works.****
** **
If i put the same logic into javascript, the hook works and the script is terminated:****
** **
function on_hangup(s,status)****
{****
console_log("NOTICE","---- hangup "+status+"\n");****
return "exit";****
}****
** **
console_log("NOTICE","---- ANSWER\n");****
session.answer();****
console_log("NOTICE","---- ANSWER\n");****
session.setHangupHook(on_hangup);****
** **
while(session.ready())****
{****
console_log("NOTICE","---- START OF LOOP\n");****
session.streamFile("/usr/local/freeswitch/sounds/en/us/callie/ivr/8000/ivr-on_hold_indefinitely.wav"); ****
console_log("NOTICE","---- END OF LOOP\n");****
}****
console_log("NOTICE","---- END OF SCRIPT\n");****
session.hangup();****
** **
** **
2012-01-25 12:13:08.396041 [NOTICE] switch_channel.c:930 New Channel sofia/internal/2001@192.168.20.73 [8f85a15c-4745-11e1-8c76-0d2938abf0b5]** **
2012-01-25 12:13:08.396041 [INFO] mod_dialplan_xml.c:481 Processing B#-FS-2001 <2001>->3002 in context default****
2012-01-25 12:13:08.396041 [NOTICE] hangup.js:1 ---- ANSWER****
2012-01-25 12:13:08.396041 [NOTICE] mod_spidermonkey.c:2068 Channel [sofia/internal/2001@192.168.20.73] has been answered****
2012-01-25 12:13:08.396041 [NOTICE] hangup.js:1 ---- ANSWER****
2012-01-25 12:13:08.396041 [NOTICE] hangup.js:1 ---- START OF LOOP****
2012-01-25 12:13:09.875996 [NOTICE] sofia.c:624 Hangup sofia/internal/ 2001@192.168.20.73 [CS_EXECUTE] [NORMAL_CLEARING]****
2012-01-25 12:13:09.875996 [NOTICE] hangup.js:2 ---- hangup hangup****
2012-01-25 12:13:09.875996 [NOTICE] switch_core_session.c:1398 Session 3 (sofia/internal/2001@192.168.20.73) Ended****
2012-01-25 12:13:09.875996 [NOTICE] switch_core_session.c:1400 Close Channel sofia/internal/2001@192.168.20.73 [CS_DESTROY]****
** **
** **
Any suggestions ? Or do i miss something here ?****
** **
The dialplan part:****
** **
<extension name="hanguptest">****
<condition field="destination_number" expression="^(3001)$">****
<action application="set" data="session_in_hangup_hook=true"/>****
<action application="lua" data="hangup.lua"/>****
</condition>****
</extension>****
** **
(replace hangup.lua with hangup.js for the javascript version)****
Freeswitch Version:****
FreeSWITCH Version 1.0.head (git-9be51d5 2012-01-21 13-45-21 -0500)****
** **
thx®ards,****
gw****
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
Just to let you know mercutioviz that anthm did make some source code changes last week that should have enabled this to work. Commit:09ad887948f7513725ca8b53bdfe721d9008e73b * FS-3841 --resolve ok return the string "die" or "exit" from hanguphook to cause an error or call s:destroy("any err message"); either should now halt the script -- View this message in context: http://freeswitch-users.2379917.n2.nabble.com/exit-lua-script-in-hangup-hook... Sent from the freeswitch-users mailing list archive at Nabble.com.
That's what I get for being 2 months behind on the changelog! :D If we have any Lua peeps out there who could test this and update the wiki I would be eternally grateful... -MC On Tue, Jan 31, 2012 at 12:06 PM, Jeff Lenk <jeff@jefflenk.com> wrote:
Just to let you know mercutioviz that anthm did make some source code changes last week that should have enabled this to work.
Commit:09ad887948f7513725ca8b53bdfe721d9008e73b
* FS-3841 --resolve ok return the string "die" or "exit" from hanguphook to cause an error or call s:destroy("any err message"); either should now halt the script
-- View this message in context: http://freeswitch-users.2379917.n2.nabble.com/exit-lua-script-in-hangup-hook... Sent from the freeswitch-users mailing list archive at Nabble.com.
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
Hello, Just tried all 4 cases (error(), return "exit", return "die", session:destroy("42")) All of them are working as expected. Thanks for implementing ! I just checked the wiki and the example for the setHangupHook Call (http://wiki.freeswitch.org/wiki/Mod_lua#session:setHangupHook) In the code, error() is used to exit the script - which is now working. So I guess the possibility to exit the script was already there, but not working until the patch from Anthony ? Anyways, thanks ! Now I have to figure out how to handle outgoing calls from lua. And how to control them. (seems like ESL is the only choice for fully asynchronous<http://dict.leo.org/ende?lp=ende&p=DOKJAA&search=asynchronous&trestr=0x804> call handling ?) PS: I've updated the wiki page. Von: freeswitch-users-bounces@lists.freeswitch.org [mailto:freeswitch-users-bounces@lists.freeswitch.org] Im Auftrag von Michael Collins Gesendet: Dienstag, 31. Jänner 2012 22:08 An: FreeSWITCH Users Help Betreff: Re: [Freeswitch-users] exit lua script in hangup hook That's what I get for being 2 months behind on the changelog! :D If we have any Lua peeps out there who could test this and update the wiki I would be eternally grateful... -MC On Tue, Jan 31, 2012 at 12:06 PM, Jeff Lenk <jeff@jefflenk.com<mailto:jeff@jefflenk.com>> wrote: Just to let you know mercutioviz that anthm did make some source code changes last week that should have enabled this to work. Commit:09ad887948f7513725ca8b53bdfe721d9008e73b * FS-3841 --resolve ok return the string "die" or "exit" from hanguphook to cause an error or call s:destroy("any err message"); either should now halt the script -- View this message in context: http://freeswitch-users.2379917.n2.nabble.com/exit-lua-script-in-hangup-hook... Sent from the freeswitch-users mailing list archive at Nabble.com. _________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org<mailto:consulting@freeswitch.org> http://www.freeswitchsolutions.com FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org<mailto:FreeSWITCH-users@lists.freeswitch.org> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
On Wed, Feb 1, 2012 at 12:48 AM, Gerald Weber <gerald.weber@besharp.at>wrote:
Hello,****
** **
Just tried all 4 cases (error(), return “exit”, return “die”, session:destroy(“42”))****
All of them are working as expected. Thanks for implementing !****
** **
I just checked the wiki and the example for the setHangupHook Call ( http://wiki.freeswitch.org/wiki/Mod_lua#session:setHangupHook)****
In the code, error() is used to exit the script – which is now working.*** *
So I guess the possibility to exit the script was already there, but not working until the patch from Anthony ?****
** **
Anyways, thanks !****
** **
Now I have to figure out how to handle outgoing calls from lua. And how to control them.****
(seems like ESL is the only choice for fully asynchronous<http://dict.leo.org/ende?lp=ende&p=DOKJAA&search=asynchronous&trestr=0x804> call handling ?)
Yes, fully async call control is best handled with ESL or the new mod_httapi. If you do a Lua dialplan script then you are kind of in your own little world (or thread, as it were). External call control has many advantages and very few disadvantages.
****
** **
PS:****
I’ve updated the wiki page.
Thanks! -MC
****
participants (3)
-
Gerald Weber
-
Jeff Lenk
-
Michael Collins