<div dir="ltr"><div>I just got around to test this again and here is what I found.</div><div>from within the hangup hook you can exit using <font face="monospace,monospace">return &quot;exit&quot;</font> and as noted by Andrew it will exit with an error unless you set <font face="monospace,monospace">debug.traceback=nil</font></div><div>from the main code (not within a function) you can exit by using <font face="monospace,monospace">return &quot;exit&quot;</font> (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 <font face="monospace,monospace">return &quot;exit&quot; end</font>).</div><div>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 <font face="monospace,monospace">session:hangup()</font> along with calling <font face="monospace,monospace">return &quot;exit&quot;</font> from the hangup hook, but on any other lua scripts (api, event hooks, dialplan hook, startup) there seems to be no way.</div><div>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.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 10, 2016 at 4:21 PM, Abaci B <span dir="ltr">&lt;<a href="mailto:abaci64@gmail.com" target="_blank">abaci64@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>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?</div><div><div class="h5"><div><br></div><div class="gmail_extra"><div class="gmail_quote">On Wed, Mar 9, 2016 at 1:20 AM, Chad Phillips <span dir="ltr">&lt;<a href="mailto:chad@apartmentlines.com" target="_blank">chad@apartmentlines.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Andrew,<div><br></div><div>Curious if this structure will work at the bottom of either a a Lua script or Lua function:</div><div><br></div><div>do</div><div>  return &quot;exit&quot;</div><div>end</div><div><br></div><div>I seem to recall that being a trick that helped for weird &#39;return&#39; cases.</div><span><font color="#888888"><div><br></div><div>Chad</div></font></span></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Tue, Mar 8, 2016 at 12:54 PM, Andrew Keil <span dir="ltr">&lt;<a href="mailto:andrew.keil@visytel.com" target="_blank">andrew.keil@visytel.com</a>&gt;</span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>





<div lang="EN-AU" vlink="purple" link="blue">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">Michael,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">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.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">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.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">ie. This works:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">----------------------------------------------------------------<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">function CleanUp()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                freeswitch.consoleLog(&quot;CLEANUP SECTION\n&quot;)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                freeswitch.consoleLog(&quot;CLEANUP SECTION COMPLETE\n&quot;)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">end<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">function myHangupHook(s, status, arg)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                freeswitch.consoleLog(string.format(&quot;%s DETECTED\n&quot;,arg))<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                session:hangup()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                -- Run CleanUp function now since the caller has disconnected  
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                CleanUp()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                -- Abort Lua script here to avoid returning to MainService()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                return &quot;exit&quot;      
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">end<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">-- Setup Hangup event handler here<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">v_hangup = &quot;HANGUP&quot;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">session:setHangupHook(&quot;myHangupHook&quot;, &quot;v_hangup&quot;)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">Whereas this does not work:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">----------------------------------------------------------------<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">function CleanUp()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                freeswitch.consoleLog(&quot;CLEANUP SECTION\n&quot;)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                freeswitch.consoleLog(&quot;CLEANUP SECTION COMPLETE\n&quot;)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                return &quot;exit&quot;      
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">end       
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">function myHangupHook(s, status, arg)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                freeswitch.consoleLog(string.format(&quot;%s DETECTED\n&quot;,arg))<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                session:hangup()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                -- Run CleanUp function now since the caller has disconnected  
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">                CleanUp()<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">end<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">-- Setup Hangup event handler here<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">v_hangup = &quot;HANGUP&quot;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">session:setHangupHook(&quot;myHangupHook&quot;, &quot;v_hangup&quot;)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><a name="m_1710721875543731147_-1920980919_-766515437_1060642169__MailEndCompose"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">Regards,<u></u><u></u></span></a></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">Andrew<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif"> <a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a> [mailto:<a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a>]
<b>On Behalf Of </b>Michael Collins<br>
<b>Sent:</b> Saturday, 5 March 2016 8:11 AM<br>
<b>To:</b> FreeSWITCH Users Help &lt;<a href="mailto:freeswitch-users@lists.freeswitch.org" target="_blank">freeswitch-users@lists.freeswitch.org</a>&gt;<br>
<b>Subject:</b> Re: [Freeswitch-users] Re- End Lua script after HangupHook handled without all the extra code to handle the return to the function<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Fri, Mar 4, 2016 at 9:19 AM, Abaci B &lt;<a href="mailto:abaci64@gmail.com" target="_blank">abaci64@gmail.com</a>&gt; wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<p class="MsoNormal">The question is not how to figure out when to exit the lua script, the question is <b>how to exit the lua script</b>, and that can sometimes be tricky or complicated as return &quot;exit&quot; only works from hangup hook.<u></u><u></u></p>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Are you positive that it works only from a hangup hook? It seems to work at the end of any loop:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">-- testing exit (no session, call with luarun)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">freeswitch.consoleLog(&#39;INFO&#39;,&quot;Starting infinite loop...\n&quot;)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">while(1) do</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">  freeswitch.consoleLog(&#39;WARNING&#39;,&quot;Before exit...\n&quot;)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">  return &quot;exit&quot;</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">end</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">freeswitch.consoleLog(&#39;INFO&#39;,&quot;All done!\n&quot;)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Or with a session:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">-- test exit with session, no hangup hook</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">session:answer()</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">freeswitch.consoleLog(&#39;INFO&#39;,&quot;Entering main loop...\n&quot;)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">while ( session:ready() == true ) do</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">  freeswitch.consoleLog(&#39;WARNING&#39;,&quot;Inside loop...\n&quot;)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">  return &quot;exit&quot;</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">end</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;">freeswitch.consoleLog(&#39;INFO&#39;,&quot;All done!\n&quot;)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">In both cases I never see &quot;All done!&quot; at the CLI. Can you try it and see if there&#39;s a scenario where it does not exit as expected?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">-MC<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>

<br></div></div><span>_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org" target="_blank">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com" target="_blank" rel="noreferrer">http://www.freeswitchsolutions.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br>
<a href="http://confluence.freeswitch.org" target="_blank" rel="noreferrer">http://confluence.freeswitch.org</a><br>
<a href="http://www.cluecon.com" target="_blank" rel="noreferrer">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br></span></blockquote></div><br></div>
<br>_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org" target="_blank">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com" target="_blank" rel="noreferrer">http://www.freeswitchsolutions.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br>
<a href="http://confluence.freeswitch.org" target="_blank" rel="noreferrer">http://confluence.freeswitch.org</a><br>
<a href="http://www.cluecon.com" target="_blank" rel="noreferrer">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank" rel="noreferrer">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org" target="_blank" rel="noreferrer">http://www.freeswitch.org</a><br></blockquote></div><br></div></div></div></div>
</blockquote></div><br></div>