Changeset

12872:a20923f7d5fd

mod_storage_sql: Record connection to database as module status Allows retrieving this in e.g. a health reporting module Thanks pfak
author Kim Alvefur <zash@zash.se>
date Mon, 30 Jan 2023 00:38:26 +0100
parents 12871:885323e2a1ce
children 12873:7f4f834fae79
files plugins/mod_storage_sql.lua util/sql.lua util/sqlite3.lua
diffstat 3 files changed, 18 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua	Mon Jan 30 00:14:50 2023 +0100
+++ b/plugins/mod_storage_sql.lua	Mon Jan 30 00:38:26 2023 +0100
@@ -840,6 +840,7 @@
 	engine = engines[db_uri];
 	if not engine then
 		module:log("debug", "Creating new engine %s", db_uri);
+		module:log_status("debug", "Creating new engine for "..params.driver);
 		engine = sql:create_engine(params, function (engine) -- luacheck: ignore 431/engine
 			if module:get_option("sql_manage_tables", true) then
 				-- Automatically create table, ignore failure (table probably already exists)
@@ -858,8 +859,13 @@
 					end
 				end
 			end
+			module:set_status("info", "Connected to " .. engine.params.driver);
+		end, function (engine)
+			module:set_status("error", "Disconnected from " .. engine.params.driver);
 		end);
 		engines[sql.db2uri(params)] = engine;
+	else
+		module:set_status("info", "Using existing engine");
 	end
 
 	module:provides("storage", driver);
--- a/util/sql.lua	Mon Jan 30 00:14:50 2023 +0100
+++ b/util/sql.lua	Mon Jan 30 00:38:26 2023 +0100
@@ -99,6 +99,9 @@
 function engine:onconnect() -- luacheck: ignore 212/self
 	-- Override from create_engine()
 end
+function engine:ondisconnect() -- luacheck: ignore 212/self
+	-- Override from create_engine()
+end
 
 function engine:prepquery(sql)
 	if self.params.driver == "MySQL" then
@@ -224,6 +227,7 @@
 		if not conn or not conn:ping() then
 			log("debug", "Database connection was closed. Will reconnect and retry.");
 			self.conn = nil;
+			self:ondisconnect();
 			log("debug", "Retrying SQL transaction [%s]", (...));
 			ok, ret, b, c = self:_transaction(...);
 			log("debug", "SQL transaction retry %s", ok and "succeeded" or "failed");
@@ -365,8 +369,8 @@
 	};
 end
 
-local function create_engine(_, params, onconnect)
-	return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt);
+local function create_engine(_, params, onconnect, ondisconnect)
+	return setmetatable({ url = db2uri(params); params = params; onconnect = onconnect; ondisconnect = ondisconnect }, engine_mt);
 end
 
 return {
--- a/util/sqlite3.lua	Mon Jan 30 00:14:50 2023 +0100
+++ b/util/sqlite3.lua	Mon Jan 30 00:38:26 2023 +0100
@@ -159,6 +159,9 @@
 function engine:onconnect()
 	-- Override from create_engine()
 end
+function engine:ondisconnect() -- luacheck: ignore 212/self
+	-- Override from create_engine()
+end
 function engine:execute(sql, ...)
 	local success, err = self:connect();
 	if not success then return success, err; end
@@ -322,6 +325,7 @@
 		local conn = self.conn;
 		if not conn or not conn:isopen() then
 			self.conn = nil;
+			self:ondisconnect();
 			ok, ret = self:_transaction(...);
 		end
 	end
@@ -389,9 +393,9 @@
 	};
 end
 
-local function create_engine(_, params, onconnect)
+local function create_engine(_, params, onconnect, ondisconnect)
 	assert(params.driver == "SQLite3", "Only SQLite3 is supported without LuaDBI");
-	return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt);
+	return setmetatable({ url = db2uri(params); params = params; onconnect = onconnect; ondisconnect = ondisconnect }, engine_mt);
 end
 
 return {