Diff

util/sql.lua @ 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
parent 10534:8a42fd6702e6
child 12975:d10957394a3c
line wrap: on
line diff
--- 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 {