Fixed Lua game init completely being interrupted when database is unavailable (refs #59)

* database module will throw debug messages when it cannot connect or read
* ACL module will enter 'dumb' mode and disallow any permissions (should be changed in future)
This commit is contained in:
Timo Smit 2019-01-01 21:12:22 +01:00
parent aa67685e45
commit 98d4a30b21
5 changed files with 36 additions and 7 deletions

View File

@ -15,6 +15,8 @@
-- You should have received a copy of the GNU General Public License -- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
local db = require (wolfa_getLuaPath()..".db.db")
local events = require (wolfa_getLuaPath()..".util.events") local events = require (wolfa_getLuaPath()..".util.events")
local settings = require (wolfa_getLuaPath()..".util.settings") local settings = require (wolfa_getLuaPath()..".util.settings")
@ -116,6 +118,13 @@ auth.PERM_IMMUNE = "immune"
-- this, but it will suffice. -- this, but it will suffice.
function auth.onGameInit() function auth.onGameInit()
if settings.get("g_standalone") == 1 then if settings.get("g_standalone") == 1 then
if not db.isConnected() then
-- FIXME simple workaround to deny any commands
function auth.isPlayerAllowed() return false end
return
end
srv = require (wolfa_getLuaPath()..".auth.acl") srv = require (wolfa_getLuaPath()..".auth.acl")
srv.readPermissions() srv.readPermissions()

View File

@ -17,6 +17,8 @@
local acl = require (wolfa_getLuaPath()..".auth.acl") local acl = require (wolfa_getLuaPath()..".auth.acl")
local db = require (wolfa_getLuaPath()..".db.db")
local commands = require (wolfa_getLuaPath()..".commands.commands") local commands = require (wolfa_getLuaPath()..".commands.commands")
local settings = require (wolfa_getLuaPath()..".util.settings") local settings = require (wolfa_getLuaPath()..".util.settings")
@ -195,4 +197,4 @@ function commandAcl(command, action, ...)
return true return true
end end
commands.addserver("acl", commandAcl, (settings.get("g_standalone") == 0)) commands.addserver("acl", commandAcl, (settings.get("g_standalone") == 0 or not db.isConnected()))

View File

@ -36,12 +36,16 @@ function db.oninit()
elseif settings.get("db_type") == "mysql" then elseif settings.get("db_type") == "mysql" then
con = require (wolfa_getLuaPath()..".db.mysql") con = require (wolfa_getLuaPath()..".db.mysql")
else else
error("invalid database system (none|sqlite3|mysql)") outputDebug("Invalid database system (none|sqlite3|mysql), defaulting to 'none'.")
return
end end
setmetatable(db, {__index = con}) setmetatable(db, {__index = con})
db.start() if not db.start() then
outputDebug("Database could not be loaded, only limited functionality is available.", 3)
end
end end
end end
events.handle("onGameInit", db.oninit) events.handle("onGameInit", db.oninit)

View File

@ -514,11 +514,18 @@ function mysql.start()
con = env:connect(settings.get("db_database"), settings.get("db_username"), settings.get("db_password"), settings.get("db_hostname"), settings.get("db_port")) con = env:connect(settings.get("db_database"), settings.get("db_username"), settings.get("db_password"), settings.get("db_hostname"), settings.get("db_port"))
if not con then if not con then
error("could not connect to database") outputDebug("Could not connect to database.", 3)
return false
elseif not mysql.isSchemaExistent() then elseif not mysql.isSchemaExistent() then
mysql.close() mysql.close()
error("schema does not exist")
outputDebug("Database schema does not exist.", 3)
return false
end end
return true
end end
function mysql.close(doSave) function mysql.close(doSave)

View File

@ -522,15 +522,22 @@ function sqlite3.start()
con = env:connect(uri) con = env:connect(uri)
if not con then if not con then
error("could not connect to database") outputDebug("Could not connect to database.", 3)
return false
elseif not sqlite3.isSchemaExistent() then elseif not sqlite3.isSchemaExistent() then
sqlite3.close() sqlite3.close()
error("schema does not exist")
outputDebug("Database schema does not exist.", 3)
return false
end end
-- enable foreign key enforcement -- enable foreign key enforcement
assert(con:execute("PRAGMA foreign_keys=1")) assert(con:execute("PRAGMA foreign_keys=1"))
assert(con:execute("PRAGMA synchronous=0")) assert(con:execute("PRAGMA synchronous=0"))
return true
end end
function sqlite3.close(doSave) function sqlite3.close(doSave)