[Freeswitch-users] Re- End Lua script after HangupHook handled without all the extra code to handle the return to the function

Michael Jerris mike at jerris.com
Thu Mar 10 03:56:44 MSK 2016


I have to double check, but this may leak memory... 

> On Mar 4, 2016, at 11:56 AM, Chad Phillips <chad at apartmentlines.com> wrote:
> 
> In cases where I've run Lua scripts on startup, I'll use a global variable, and periodically check it like so:
> 
>   kill_script = api:executeString("global_getvar custom_lua_kill_script")
> 
> If the variable is set to a specified value, then I take steps to end the script, which usually just means breaking out of an infinite loop.
> 
> This variable can be adjusted in a number of ways, including another Lua script, directly from CLI, or even by setting a timer using the schedule API in the same script:
> 
>   command = string.format([[sched_api %d none global_setvar custom_lua_kill_script=true]], shutdown_stamp)
>   api:executeString(command)
> 
> Not the most elegant solution, but it works.
> 
> On Fri, Mar 4, 2016 at 5:55 AM, Abaci B <abaci64 at gmail.com <mailto:abaci64 at gmail.com>> wrote:
> I still think there should be a way to exit a lua script without calling a hangup hook, this is needed for lua scripts with no session and even in some scenarios where there is a session.
> I think I'll open a feature request on Jira for that, unless someone knows a way to do this?
> 
> On Mon, Feb 29, 2016 at 5:09 PM, Andrew Keil <andrew.keil at visytel.com <mailto:andrew.keil at visytel.com>> wrote:
> Abaci B,
> 
>  
> 
> Thanks so much for sourcing the JIRA topic!
> 
>  
> 
> You were right, by placing the return “exit” inside the hanguphook handler then the sript does stop and abort with an error.  I have made this cleaner by adding debug.traceback=nil just prior.
> 
>  
> 
> So now my hangup hook handler looks like this:
> 
>  
> 
> function myHangupHook(s, status, arg)
> 
>     session:hangup()
> 
>     CleanUp() -- Run CleanUp function now since the caller has disconnected
> 
>     debug.traceback=nil
> 
>     return "exit" 
> 
> end
> 
>  
> 
> Obviously this generates the message: “2016-03-01 09:01:18.625809 [ERR] mod_lua.cpp:203 exit” <>
> in the console, but this I can live with especially to avoid the goto statements throughout the code after every audio related function.
> 
>  
> 
> Thanks again.
> 
>  
> 
> Andrew Keil
> 
> Visytel Pty Ltd
> 
>  
> 
> From: freeswitch-users-bounces at lists.freeswitch.org <mailto:freeswitch-users-bounces at lists.freeswitch.org> [mailto:freeswitch-users-bounces at lists.freeswitch.org <mailto:freeswitch-users-bounces at lists.freeswitch.org>] On Behalf Of Abaci B
> Sent: Tuesday, 1 March 2016 3:16 AM
> 
> 
> To: FreeSWITCH Users Help <freeswitch-users at lists.freeswitch.org <mailto: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
> 
>  
> 
> I don't see in your example above where you have a return "exit" in your hangup hook function
> 
> see https://freeswitch.org/jira/browse/FS-3841 <https://freeswitch.org/jira/browse/FS-3841>
> seems like the "exit" or "die" needs to be returned directly from the hanguphook function so try from that function (not cleanup function) to return "exit" or "dye", also it seems
> 
>  
> 
> On Mon, Feb 29, 2016 at 10:53 AM, Abaci B <abaci64 at gmail.com <mailto:abaci64 at gmail.com>> wrote:
> 
> That's exactly what I noticed, it breaks out of the current function, why not open a Jira? I don't think it's supposed to behave this way.
> 
>  
> 
> On Mon, Feb 29, 2016 at 1:37 AM, Andrew Keil <andrew.keil at visytel.com <mailto:andrew.keil at visytel.com>> wrote:
> 
> Thanks for your response.
> 
>  
> 
> I have gone through these with no luck.   Like I said the session:destroy(“…”) crashes FreeSWITCH, which is therefore off the list.  The rest simply interrupt the current function and do no end the script.
> 
>  
> 
> I guess my next move is to see why session:destroy() crashes FreeSWITCH, however I am a little snowed under at the moment so if anyone has some time to replicate this (only needs one line of code in a Lua script) and pass this on to the developers that would be great.
> 
>  
> 
> Andrew
> 
>   <>
> From: freeswitch-users-bounces at lists.freeswitch.org <mailto:freeswitch-users-bounces at lists.freeswitch.org> [mailto:freeswitch-users-bounces at lists.freeswitch.org <mailto:freeswitch-users-bounces at lists.freeswitch.org>] On Behalf Of Abaci B
> Sent: Saturday, 27 February 2016 1:29 AM
> To: FreeSWITCH Users Help <freeswitch-users at lists.freeswitch.org <mailto: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
> 
>  
> 
> See https://freeswitch.org/confluence/display/FREESWITCH/Lua+API+Reference#LuaAPIReference-session:setHangupHook <https://freeswitch.org/confluence/display/FREESWITCH/Lua+API+Reference#LuaAPIReference-session:setHangupHook> for a few ways to exit the lua script (error(), return "exit", return "die", s:destroy("error message")). I personally tried return "exit" but it seems to me that it only exits the calling function, haven't had a chance to look further, it's possible that the calling it from the within a function is different. if you play around and figure out please report back.
> 
>  
> 
> On Thu, Feb 25, 2016 at 9:33 PM, Andrew Keil <andrew.keil at visytel.com <mailto:andrew.keil at visytel.com>> wrote:
> 
> To FreeSWITCH Users,
> 
>  
> 
> See below for a sample template for a Lua Service Script running inside FreeSWITCH.
> 
>  
> 
> The issue I have is fairly straightforward.
> 
>  
> 
> I need a function to run when hangup is detected (ie. at the end of the call) however I understand this must not delay ending the script.  This function is CleanUp().  Then I would like the service to end.
> 
>  
> 
> The problem I am having is if the caller hangs up during the playback of “intro.wav” (as shown inside the MainService() function below), then the code jumps to the myHangupHook which calls CleanUp() perfectly, the issue is once CleanUp() is complete I would like the Lua script to end there and then (ie. at the bottom of CleanUp()).  What actually happens is it returns to MainService() and continues to try and play “info.wav”, unless I either check for session:ready() everywhere or add a goto as shown below under each streamFile() function call.
> 
>  
> 
> My aim is to reduce extra code and to make the Lua script simpler and easier to read.  Also I would like to try and avoid goto statements, which I know can be done with if (session:ready()) etc….
> 
>  
> 
> So is there a way to stop a Lua script running inside FreeSWITCH cleanly?  I have tried the os.exit() this is barred from use by FreeSWITCH.  I have also tried session:destroy() which crashes FreeSWITCH (version 1.6.5 on CentOS 6.7, CentOS 7 and windows) 100% of the time!
> 
>  
> 
> I could look further into the Lua additions done by the FreeSWITCH team in the source code, however if someone has already solved this then that would be the best solution.
> 
>  
> 
> FYI: Obviously the script below is simple, however I am sure that you understand if the script was complicated having to use “if (session:ready()) then ….” or “if (not session:ready()) then goto HANGUPEXIT end” makes the code ugly.
> 
>  
> 
> Thanks in advance,
> 
>  
> 
> Andrew Keil
> 
> Visytel Pty Ltd
> 
>  
> 
>  
> 
>  
> 
> ------------------------------------------------------------------------------  Sample Lua Service -----------------------------------------------------------------------
> 
>  
> 
> -- Lua template for FreeSWITCH service
> 
> -- By: Andrew Keil (Visytel Pty Ltd)
> 
> -- Email: support at visytel.com <mailto:support at visytel.com>
>  
> 
> -- Setup script wide variables here
> 
>  
> 
> function PreAnswer()
> 
>                 freeswitch.consoleLog("INFO", "PRE ANSWER SECTION\n");
> 
>                 -- Add your pre answer code from here
> 
>                
> 
>                 -- End of your pre answer code
> 
>                 freeswitch.consoleLog("INFO", "PRE ANSWER SECTION COMPLETE\n"); 
> 
> end       
> 
>  
> 
> function AnswerCaller()
> 
>                 session:answer()
> 
>                 session:sleep(1000)
> 
> end       
> 
>  
> 
> function MainService()
> 
>                 freeswitch.consoleLog("INFO", "MAIN SERVICE SECTION\n");      
> 
>                 if (session:ready()) then
> 
>                                 -- Note (1): If you wish to end the call then simply use: goto ENDSERVICE
> 
>                                 -- Note (2): To terminate the service sooner when HANGUP is detected use: if (not session:ready()) then goto HANGUPEXIT end
> 
>                                 -- Add your main service code from here               (caller would have been answered)
> 
>                                
> 
>                                 session:streamFile("intro.wav")
> 
>                                 if (not session:ready()) then goto HANGUPEXIT end
> 
>                                 session:streamFile("info.wav")
> 
>                                 if (not session:ready()) then goto HANGUPEXIT end
> 
>                                 session:streamFile("outro.wav")               
> 
>                                 if (not session:ready()) then goto HANGUPEXIT end
> 
>                                
> 
>                                 -- End of your main service code
> 
>                 end
> 
>                 ::ENDSERVICE::
> 
>                 if (session:ready()) then
> 
>                                 -- End of service so hangup
> 
>                                 session:hangup()  -- Should automatically jump to CleanUp() via hangup handler if caller still online at this stage
> 
>                 end
> 
>                 goto END
> 
>                 ::HANGUPEXIT::
> 
>                 freeswitch.consoleLog("INFO", "END OF SERVICE (HANGUP DETECTED)\n");         
> 
>                 ::END:: 
> 
>                 freeswitch.consoleLog("INFO", "MAIN SERVICE SECTION COMPLETE\n");               
> 
> end
> 
>  
> 
> function CleanUp()
> 
>                 freeswitch.consoleLog("INFO", "CLEANUP SECTION\n");
> 
>                 -- Add your cleanup code from here (caller would have been disconnected)
> 
>                
> 
>                 -- End of your cleanup code
> 
>                 freeswitch.consoleLog("INFO", "CLEANUP SECTION COMPLETE\n");         
> 
> end       
> 
>  
> 
> function myHangupHook(s, status, arg)
> 
>                 session:hangup()
> 
>                 CleanUp() -- Run CleanUp function now since the caller has disconnected
> 
> end
> 
>  
> 
> -- Setup Hangup event handler here
> 
> v_hangup = "HANGUP"
> 
> session:setHangupHook("myHangupHook", "v_hangup")
> 
>  
> 
> -- Call service functions in order
> 
> PreAnswer()
> 
> AnswerCaller()
> 
> MainService()
> 
> -- End of Lua service
> 
>  
> 
>  
> 
> 
> _________________________________________________________________________
> Professional FreeSWITCH Consulting Services:
> consulting at freeswitch.org <mailto:consulting at freeswitch.org>
> http://www.freeswitchsolutions.com <http://www.freeswitchsolutions.com/>
> 
> Official FreeSWITCH Sites
> http://www.freeswitch.org <http://www.freeswitch.org/>
> http://confluence.freeswitch.org <http://confluence.freeswitch.org/>
> http://www.cluecon.com <http://www.cluecon.com/>
> 
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org <mailto:FreeSWITCH-users at lists.freeswitch.org>
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users <http://lists.freeswitch.org/mailman/listinfo/freeswitch-users>
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users <http://lists.freeswitch.org/mailman/options/freeswitch-users>
> http://www.freeswitch.org <http://www.freeswitch.org/>
>  
> 
> 
> _________________________________________________________________________
> Professional FreeSWITCH Consulting Services:
> consulting at freeswitch.org <mailto:consulting at freeswitch.org>
> http://www.freeswitchsolutions.com <http://www.freeswitchsolutions.com/>
> 
> Official FreeSWITCH Sites
> http://www.freeswitch.org <http://www.freeswitch.org/>
> http://confluence.freeswitch.org <http://confluence.freeswitch.org/>
> http://www.cluecon.com <http://www.cluecon.com/>
> 
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org <mailto:FreeSWITCH-users at lists.freeswitch.org>
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users <http://lists.freeswitch.org/mailman/listinfo/freeswitch-users>
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users <http://lists.freeswitch.org/mailman/options/freeswitch-users>
> http://www.freeswitch.org <http://www.freeswitch.org/>
>  
> 
>  
> 
> 
> 
> _________________________________________________________________________
> Professional FreeSWITCH Consulting Services:
> consulting at freeswitch.org <mailto:consulting at freeswitch.org>
> http://www.freeswitchsolutions.com <http://www.freeswitchsolutions.com/>
> 
> Official FreeSWITCH Sites
> http://www.freeswitch.org <http://www.freeswitch.org/>
> http://confluence.freeswitch.org <http://confluence.freeswitch.org/>
> http://www.cluecon.com <http://www.cluecon.com/>
> 
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org <mailto:FreeSWITCH-users at lists.freeswitch.org>
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users <http://lists.freeswitch.org/mailman/listinfo/freeswitch-users>
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users <http://lists.freeswitch.org/mailman/options/freeswitch-users>
> http://www.freeswitch.org <http://www.freeswitch.org/>
> 
> 
> _________________________________________________________________________
> Professional FreeSWITCH Consulting Services:
> consulting at freeswitch.org <mailto:consulting at freeswitch.org>
> http://www.freeswitchsolutions.com <http://www.freeswitchsolutions.com/>
> 
> Official FreeSWITCH Sites
> http://www.freeswitch.org <http://www.freeswitch.org/>
> http://confluence.freeswitch.org <http://confluence.freeswitch.org/>
> http://www.cluecon.com <http://www.cluecon.com/>
> 
> FreeSWITCH-users mailing list
> FreeSWITCH-users at lists.freeswitch.org <mailto:FreeSWITCH-users at lists.freeswitch.org>
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-users <http://lists.freeswitch.org/mailman/listinfo/freeswitch-users>
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users <http://lists.freeswitch.org/mailman/options/freeswitch-users>
> http://www.freeswitch.org <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/20160309/42e1400b/attachment-0001.html 


Join us at ClueCon 2016 Aug 8-12, 2016
More information about the FreeSWITCH-users mailing list