From 937f8f3f52603a92948fb9e5d294ea7fdeaf544b Mon Sep 17 00:00:00 2001 From: kremsy Date: Wed, 12 Mar 2014 13:27:14 +0100 Subject: [PATCH] handle database loss --- application/core/Database.php | 23 +++++++++++++++++++++-- application/core/ManiaControl.php | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/application/core/Database.php b/application/core/Database.php index 6c4480ee..a409d62a 100644 --- a/application/core/Database.php +++ b/application/core/Database.php @@ -1,13 +1,14 @@ mysqli = @new \mysqli($host, $user, $pass, null, $port); if ($this->mysqli->connect_error) { @@ -49,13 +53,28 @@ class Database { $this->initDatabase(); $this->optimizeTables(); + + //Register Method which checks the Database Connection every 5 seconds + $this->maniaControl->timerManager->registerTimerListening($this, 'checkConnection', 5000); + } + + /** + * Check if Connection still exists every 5 seconds + * @param $time + */ + public function checkConnection($time){ + if (!$this->mysqli->ping()) { + $this->maniaControl->quit("The MySQL server has gone away"); + } } /** * Destruct database connection */ public function __destruct() { - $this->mysqli->close(); + if($this->mysqli){ + $this->mysqli->close(); + } } /** diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 83ffb628..e5cbcfaf 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -98,9 +98,9 @@ class ManiaControl implements CommandListener, TimerListener { $this->config = FileUtil::loadConfig('server.xml'); // Load ManiaControl Modules - $this->database = new Database($this); $this->callbackManager = new CallbackManager($this); $this->timerManager = new TimerManager($this); + $this->database = new Database($this); $this->fileReader = new AsynchronousFileReader($this); $this->billManager = new BillManager($this); $this->settingManager = new SettingManager($this);