fix hanging when starting multiple intances on the same db

This commit is contained in:
Beu
2025-08-28 17:04:42 +02:00
parent 6af13da300
commit 64832d41e3

View File

@@ -2,6 +2,8 @@
namespace ManiaControl\Database;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Logger;
use ManiaControl\ManiaControl;
@@ -13,7 +15,7 @@ use ManiaControl\ManiaControl;
* @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class Database implements TimerListener {
class Database implements CallbackListener, TimerListener {
/*
* Public properties
*/
@@ -56,12 +58,14 @@ class Database implements TimerListener {
return;
}
$this->getMysqli()->set_charset("utf8");
$this->reduceLockWaitTimeout();
$this->initDatabase();
// $this->optimizeTables();
// Register Method which checks the Database Connection every 5 seconds
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'checkConnection', 5000);
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
// Children
$this->migrationHelper = new MigrationHelper($maniaControl);
@@ -201,6 +205,24 @@ class Database implements TimerListener {
return true;
}
/**
* Reduce session lock wait timeout value to between 5 and 10 seconds
* designed to be used during the initialization to prevent maniacontrol to be stuck when launched in parallel with other instances
*/
public function reduceLockWaitTimeout() {
// limit session lock during initialization
$this->getMysqli()->query("SET SESSION lock_wait_timeout = ". rand(5, 10) .";");
}
/**
* Reset session lock wait timeout value to the default one
* designed to be used during the initialization to prevent maniacontrol to be stuck when launched in parallel with other instances
*/
public function resetLockWaitTimeout() {
// limit session lock during initialization
$this->getMysqli()->query("SET SESSION lock_wait_timeout = @@GLOBAL.lock_wait_timeout;");
}
/**
* Return the database config
*
@@ -228,6 +250,10 @@ class Database implements TimerListener {
}
}
public function handleAfterInit() {
$this->resetLockWaitTimeout();
}
/**
* Destruct Database Connection
*/