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
-- 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 settings = require (wolfa_getLuaPath()..".util.settings")
@ -116,6 +118,13 @@ auth.PERM_IMMUNE = "immune"
-- this, but it will suffice.
function auth.onGameInit()
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.readPermissions()

View File

@ -17,6 +17,8 @@
local acl = require (wolfa_getLuaPath()..".auth.acl")
local db = require (wolfa_getLuaPath()..".db.db")
local commands = require (wolfa_getLuaPath()..".commands.commands")
local settings = require (wolfa_getLuaPath()..".util.settings")
@ -195,4 +197,4 @@ function commandAcl(command, action, ...)
return true
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
con = require (wolfa_getLuaPath()..".db.mysql")
else
error("invalid database system (none|sqlite3|mysql)")
outputDebug("Invalid database system (none|sqlite3|mysql), defaulting to 'none'.")
return
end
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
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"))
if not con then
error("could not connect to database")
outputDebug("Could not connect to database.", 3)
return false
elseif not mysql.isSchemaExistent() then
mysql.close()
error("schema does not exist")
outputDebug("Database schema does not exist.", 3)
return false
end
return true
end
function mysql.close(doSave)

View File

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