[Freeswitch-users] Re- FreeSWITCH CDRs ${billsec} not correct when using Lua scripts and running a hangup handler with any delays inside it
Andrew Keil
andrew.keil at visytel.com
Fri Feb 27 13:44:44 MSK 2015
Anthony,
Many thanks for your detailed response. It now makes sense to me and I will adjust my code appropriately.
Andrew
_____________________________
From: Anthony Minessale <anthony.minessale at gmail.com<mailto:anthony.minessale at gmail.com>>
Sent: Friday, February 27, 2015 5:08 pm
Subject: Re: [Freeswitch-users] Re- FreeSWITCH CDRs ${billsec} not correct when using Lua scripts and running a hangup handler with any delays inside it
To: FreeSWITCH Users Help <freeswitch-users at lists.freeswitch.org<mailto:freeswitch-users at lists.freeswitch.org>>
I would say you did not actually understand what he was saying based on your response.
I can try to explain:
The channel is busy executing your script. All scripts must exit immediately when the channel is transferred or hungup. That's why you must always wrap things in session:ready()
If you hangup the call from your script the next thing you MUST do is exit from the script so the session can transition to the hangup state. Until then it will never be able to complete the EXECUTE state.
What you can do is set the api_hangup_hook to another lua script that can run once the call is hungup or use CDRS to process billing data from another system designated for billing.
You may not like the idea now but one day you will come to understand that doing call logic and billing all in the same script in the foreground of the call is not a good way to do things.
On Thu, Feb 26, 2015 at 8:27 PM, Andrew Keil <andrew.keil at visytel.com<mailto:andrew.keil at visytel.com>> wrote:
Steven,
Many thanks for your fast response. I understand what you are saying, however even when I add “session:hangup()” as the first line inside my hangup handler (function myHangupHook(s, status, arg)), that is prior to the pause of 5 seconds waiting for an event that never comes (at the moment) the resulting CDRs are based on when the Lua script completes (it seems) and not when the call disconnects. I fully understand that this is a two way conversation (ie. Remote Disconnect received then Release sent and Release Complete received).
Does this mean there is no way (within Lua under FreeSWITCH) to clear up a call correctly, then complete some extra code within the Lua script, then end the Lua script and generate correct CDRs from within FreeSWITCH?
Regarding subscribing to the HANGUP event or using the Lua script to fire events to a third party application I will be looking into this, however I thought I would first ask if I was missing something within FreeSWITCH.
One more question is there a FreeSWITCH action to correctly release the call apart from session:hangup() {which obviously does not work when the remote end disconnects first}?
I am more than happy to experiment with any ideas.
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 Steven Ayre
Sent: Friday, 27 February 2015 10:47 AM
To: FreeSWITCH Users Help
Subject: Re: [Freeswitch-users] Re- FreeSWITCH CDRs ${billsec} not correct when using Lua scripts and running a hangup handler with any delays inside it
Sorry, jumped the gun a little based on the subject.
A more relevant answer is that during the hangup state the call is still in the process of hanging up, it isn't hung up and ended until the hangup_complete event. So your 5s sleep is extending the life of the call.
Is it perhaps possible to subscribe to the hangup event and process it asyncronously?
On 26 February 2015 at 23:32, Steven Ayre <steveayre at gmail.com<mailto:steveayre at gmail.com>> wrote:
CDR processing should be done in the reporting not hangup state.
On 26 February 2015 at 22:50, Andrew Keil <andrew.keil at visytel.com<mailto:andrew.keil at visytel.com>> wrote:
To FreeSWITCH users,
I am currently experimenting with Lua scripts on FreeSWITCH (latest build) and found an interesting issue with FreeSWITCH default CDRs (inside log/cdr-csv/1000.csv).
See below for my simple Lua script called from a diaplan as follows:
<include>
<!-- To test Lua script -->
<extension name="ivr_demo">
<condition field="destination_number" expression="^(9101)$">
<action application="lua" data="myservice.lua"/>
</condition>
</extension>
</include>
What I noticed is that placing a call into FreeSWITCH via a SIP phone (calling 9101) and landing on myservice.lua everything processes as expected. If I hangup after 3 seconds then I should expect the CDR to show ${billsec} = 3. However this is not the case since both ${duration} and ${billsec} are actually set once the Lua script completes (not based on HANGUP). So inside 1000.csv I see duration and billsec both having 8 seconds.
You will see below that inside my hangup handler (function myHangupHook(s, status, arg)) I fire an event and wait up to 5000ms for an event to be returned (currently I am not returning an event so the 5000ms will take place). This explains the extra 5 seconds after the 3 seconds of the call.
My question is since FreeSWITCH knows that the HANGUP has taken place why is the ${billsec} not correct (ie. should be 3 seconds)?
I tried adding “session:hangup()” as the first line inside my hangup handler (function myHangupHook(s, status, arg)) and this made no difference.
Is there a solution to this issue? Perhaps there is a different CDR variable that will contain the correct duration based on when HANGUP was actually received.
Thanks in advance for any assistance with this.
Regards,
Andrew Keil
Simple Lua Script:
-- myservice.lua
-- To test a simple service
function MainService()
freeswitch.consoleLog("INFO", "MAIN SERVICE SECTION ")
-- answer the call
freeswitch.consoleLog("INFO", "ANSWER CALLER ")
session:answer();
-- sleep a second
freeswitch.consoleLog("INFO", "Sleep for 1000ms ")
session:sleep(1000);
freeswitch.consoleLog("INFO", "Playing 12345 ")
session:say("12345", "en", "number", "pronounced");
freeswitch.consoleLog("INFO", "Finished playing 12345 ")
if (session:ready()) then
-- hangup
freeswitch.consoleLog("INFO", "HANGUP ")
session:hangup()
end
end
--------------------------------------------------------------------------------------------
-- Functions section …
function myHangupHook(s, status, arg)
freeswitch.consoleLog("INFO", "myHangupHook: " .. status .. " ")
-- send hangup event test (after caller hangs up)
local event = freeswitch.Event("CUSTOM", "visytel::HANGUP?")
event:addHeader("hi", "there")
event:addHeader("channel-uuid", channeluuid)
-- Add Event consumer if a response is required!
local con = freeswitch.EventConsumer("CUSTOM", "visytel::HANGUP!")
-- fire event
freeswitch.consoleLog("INFO", "Fire HANGUP event ")
event:fire()
-- Wait for 5000ms to test response
local retevent = con:pop(1, 5000)
if retevent then
freeswitch.consoleLog("INFO", "Reply received! ")
freeswitch.consoleLog("INFO", string.format("Reply received: %s ",retevent:getHeader("Result")))
end
end
-- Setup handlers here
myparam="empty";
session:setHangupHook("myHangupHook", "myparam")
-- Source channel-uuid here {used inside myHangupHook handler and for all fired events inside the header
channeluuid = session:getVariable("uuid");
MainService()
-- End of lua script
_________________________________________________________________________
Professional FreeSWITCH Consulting Services:
consulting at freeswitch.org<mailto: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<mailto: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<mailto: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<mailto: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
--
Anthony Minessale II ? @anthmfs ? @FreeSWITCH ?
? http://freeswitch.org/ ? http://cluecon.com/ ? http://twitter.com/FreeSWITCH
? irc.freenode.net<http://irc.freenode.net> #freeswitch ? http://freeswitch.org/g+
ClueCon Weekly Development Call
? sip:888 at conference.freeswitch.org<mailto:sip%3A888 at conference.freeswitch.org> ? +19193869900<tel:+19193869900>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20150227/a26b311d/attachment-0001.html
Join us at ClueCon 2016 Aug 8-12, 2016
More information about the FreeSWITCH-users
mailing list