Skip the cache if the SQL query timed out

This commit is contained in:
FusionPBX 2024-05-06 13:08:02 -06:00 committed by GitHub
parent 94e3d1edf6
commit 04ee47ce8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 55 additions and 29 deletions

View File

@ -1,6 +1,6 @@
-- xml_handler.lua -- xml_handler.lua
-- Part of FusionPBX -- Part of FusionPBX
-- Copyright (C) 2013-2023 Mark J Crane <markjcrane@fusionpbx.com> -- Copyright (C) 2013-2024 Mark J Crane <markjcrane@fusionpbx.com>
-- All rights reserved. -- All rights reserved.
-- --
-- Redistribution and use in source and binary forms, with or without -- Redistribution and use in source and binary forms, with or without
@ -155,6 +155,12 @@
--exits the script if we didn't connect properly --exits the script if we didn't connect properly
assert(dbh:connected()); assert(dbh:connected());
-- set the start time of the query
local start_time = os.time();
-- set the timeout value as needed
local timeout_seconds = 10;
--get the hostname --get the hostname
hostname = trim(api:execute("hostname", "")); hostname = trim(api:execute("hostname", ""));
@ -167,6 +173,7 @@
--get the dialplan xml --get the dialplan xml
if (context_name == 'public' and dialplan_mode == 'single') then if (context_name == 'public' and dialplan_mode == 'single') then
--get the single inbound destination dialplan xml from the database
sql = "SELECT (SELECT domain_name FROM v_domains WHERE domain_uuid = p.domain_uuid) as domain_name, " sql = "SELECT (SELECT domain_name FROM v_domains WHERE domain_uuid = p.domain_uuid) as domain_name, "
sql = sql .. "(SELECT domain_enabled FROM v_domains WHERE domain_uuid = p.domain_uuid) as domain_enabled, p.dialplan_xml "; sql = sql .. "(SELECT domain_enabled FROM v_domains WHERE domain_uuid = p.domain_uuid) as domain_enabled, p.dialplan_xml ";
sql = sql .. "FROM v_dialplans AS p "; sql = sql .. "FROM v_dialplans AS p ";
@ -193,6 +200,7 @@
freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n"); freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
end end
dbh:query(sql, params, function(row) dbh:query(sql, params, function(row)
dialplan_found = true;
if (row.domain_uuid ~= nil) then if (row.domain_uuid ~= nil) then
domain_name = row.domain_name; domain_name = row.domain_name;
else else
@ -202,7 +210,18 @@
xml:append(row.dialplan_xml); xml:append(row.dialplan_xml);
end end
end); end);
if (xml == nil) then
--handle not found
if (dialplan_found == nil) then
--check if the sql query timed out
local current_time = os.time();
local elapsed_time = current_time - start_time;
if elapsed_time > timeout_seconds then
--sql query timed out - unset the xml object to prevent the xml not found
xml = nil;
end
if (xml ~= nil) then
--sanitize the destination if not numeric --sanitize the destination if not numeric
if (type(destination_number) == "string") then if (type(destination_number) == "string") then
destination_number = destination_number:gsub("^%+", ""); destination_number = destination_number:gsub("^%+", "");
@ -223,7 +242,9 @@
xml:append([[ </condition>]]); xml:append([[ </condition>]]);
xml:append([[ </extension>]]); xml:append([[ </extension>]]);
end end
end
else else
--get the domain diaplan xml from the database
sql = "select dialplan_xml from v_dialplans as p "; sql = "select dialplan_xml from v_dialplans as p ";
if (context_name == "public" or string.match(context_name, "@")) then if (context_name == "public" or string.match(context_name, "@")) then
sql = sql .. "where p.dialplan_context = :call_context "; sql = sql .. "where p.dialplan_context = :call_context ";
@ -238,20 +259,24 @@
freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n"); freeswitch.consoleLog("notice", "[dialplan] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
end end
dbh:query(sql, params, function(row) dbh:query(sql, params, function(row)
dialplan_found = true;
xml:append(row.dialplan_xml); xml:append(row.dialplan_xml);
end); end);
end end
--set the xml array and then concatenate the array to a string --set the xml array and then concatenate the array to a string
if (dialplan_found ~= nil and dialplan_found) then
xml:append([[ </context>]]); xml:append([[ </context>]]);
xml:append([[ </section>]]); xml:append([[ </section>]]);
xml:append([[</document>]]); xml:append([[</document>]]);
XML_STRING = xml:build(); XML_STRING = xml:build();
end
--close the database connection --close the database connection
dbh:release(); dbh:release();
--set the cache --set the cache
if (XML_STRING ~= nil) then
local ok, err = cache.set(dialplan_cache_key, XML_STRING, expire["dialplan"]); local ok, err = cache.set(dialplan_cache_key, XML_STRING, expire["dialplan"]);
if debug["cache"] then if debug["cache"] then
if ok then if ok then
@ -260,6 +285,7 @@
freeswitch.consoleLog("warning", "[xml_handler] " .. dialplan_cache_key .. " can not be stored in the cache: " .. tostring(err) .. "\n"); freeswitch.consoleLog("warning", "[xml_handler] " .. dialplan_cache_key .. " can not be stored in the cache: " .. tostring(err) .. "\n");
end end
end end
end
--send to the console --send to the console
if (debug["cache"]) then if (debug["cache"]) then