On Sep 27, 2009, at 8:33 PM, Fabio Mascarenhas wrote:

Hi Chad,

It seems like your problem is not opening a connection for every
request per se, but not closing these connections after you are done
with them, which is triggering a resource leak. Could you provide more
information about your setup: is it a web application, and if it is,
what web server? Are you using any kind of framework or did you roll
your own?

It is of course impossible to tell for sure without more information,
but from what you have described so far of you problem ir should be
pretty easy to fix.

below i'm including the entire Lua script that i've used in my tests. this full script finishes successfully, so as far as i can tell, both the database connections and the envelope are closed for every run of this script.

the Lua scripts are called through FreeSWITCH via it's mod_lua. mod_lua is embedded in FreeSWITCH via SWIG. more details about mod_lua can be found here, if you're interested: http://wiki.freeswitch.org/wiki/Mod_lua


LUA SCRIPT:

--[[
  Sets up all customer channel variables based on an extension match in
  the database.
]]

require "support_functions"
require "luasql.mysql"
env = assert (luasql.mysql())


function mysql_connect(db)

        local mysql_con = env:connect(db, "asterisk", "XXXXXXXXXXXXX")
        return mysql_con

end

--[[
  Splits key=value into a keyed table.
]]
function split_variable_assignment(variable_string)
  local pos = string.find(variable_string, "=")
  local key = string.sub(variable_string, 1, pos - 1)
  local value = string.sub(variable_string, pos + 1)
  return {k=key, v=value}
end

--[[
  Load the basic channel variables associated with the extension:
    MasterID
    DID
]]
function lookup_extension(dialed_number)

  local extension_settings = {}

  local con = mysql_connect("asterisk_realtime")
local sql = string.format([[SELECT appdata FROM extensions WHERE exten = '%s' AND app = 'Set']], con:escape(dialed_number))
  debug_print("DIALED NUMBER QUERY: ".. sql)
  local res = assert (con:execute(sql))

  -- Load the initial variables into the extension_settings.
  local row = res:fetch ({}, "a")

  -- TODO: Take this hack out when the Asterisk conversion is complete.
  if not row then
    res:close()
sql = string.format([[SELECT appdata FROM extensions WHERE exten = '1%s' AND app = 'Set']], con:escape(dialed_number))
    debug_print("DIALED NUMBER QUERY: ".. sql)
    res = assert (con:execute(sql))
    row = res:fetch ({}, "a")
  end

  local variable
  while session_ready() and row
  do
    variable = split_variable_assignment(row.appdata)
    extension_settings[variable.k] = variable.v
    set_value(variable.k, variable.v)
    row = res:fetch ({}, "a")
  end

  res:close()
  con:close()

  return extension_settings

end

--[[
  Finishes setting up customer channel variables based on the DID's
  configuration.
]]
function set_up_customer(extension_settings)

  -- Pull customer settings from the database.
  local con = mysql_connect("apartmentlines")
sql = string.format([[SELECT * FROM asterisk_vendoritem WHERE DB_DID = %s]], con:escape(extension_settings.DID))
  debug_print("DID QUERY: ".. sql)
  local res = assert (con:execute(sql))
  local row = res:fetch ({}, "a")
  res:close()
  con:close()

  -- Merge the settings with the main settings.
  for k,v in pairs(row)
  do
    extension_settings[k] = v
    set_value(k, v)
  end

  -- Check for information boxes.
  local HAS_INFO_BOX
if extension_settings.INFO_BOX_4 == "no" and extension_settings.INFO_BOX_5 == "no" and extension_settings.INFO_BOX_6 == "no" then
    HAS_INFO_BOX = 0
  else
    HAS_INFO_BOX = 1
  end
  set_value("HAS_INFO_BOX", HAS_INFO_BOX)
  extension_settings["HAS_INFO_BOX"] = HAS_INFO_BOX

  -- Set customer prompts directory.
  CUSTOMER_RECORDINGS = get_value("CUSTOMER_RECORDINGS")
extension_settings["CUSTOMER_PROMPTS"] = CUSTOMER_RECORDINGS .. "/" .. extension_settings.MASTER_ID
  set_value("CUSTOMER_PROMPTS", extension_settings.CUSTOMER_PROMPTS)

  return extension_settings
end

-- Determine if the call is an incoming call, or a re-route to an internal
-- extension.
internal_voicemail = get_value("INTERNAL_VOICEMAIL")
dialed_number = get_value("dialed_number")
if internal_voicemail then
  extension = lookup_extension(internal_voicemail)
else
  extension = lookup_extension(dialed_number)
end

-- Only proceed if we've found a valid extension.
if extension.MASTER_ID then
  extension = set_up_customer(extension)
  debug_print("Found customer: " .. extension.MASTER_ID)
else
  debug_print("No number found!")

  GENERAL_RECORDINGS = get_value("GENERAL_RECORDINGS")
session:execute("playback", GENERAL_RECORDINGS .. "/account- disabled.wav")
  hangup()
end

if (DEBUG) then
  session:execute("info", "")
end

env:close()




_______________________________________________
Kepler-Project mailing list
[email protected]
http://lists.luaforge.net/cgi-bin/mailman/listinfo/kepler-project
http://www.keplerproject.org/

Reply via email to