From 2e9aa37cd2779b671668b1f02bc21fe1045f62f0 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Fri, 31 May 2024 19:30:11 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92DB=20lock=20v0.3=20#685=20+=20clean?= =?UTF-8?q?up?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/deviceDetails.php | 12 +++---- front/maintenance.php | 1 + front/php/server/db.php | 78 ++++++++++++++++++++++++++++++++--------- 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/front/deviceDetails.php b/front/deviceDetails.php index a3a3947e..6ba2de3f 100755 --- a/front/deviceDetails.php +++ b/front/deviceDetails.php @@ -500,14 +500,10 @@
- - - - + + + +
diff --git a/front/maintenance.php b/front/maintenance.php index 0a87da8f..e041c800 100755 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -467,6 +467,7 @@ $db->close();
nginx/error.log
+
diff --git a/front/php/server/db.php b/front/php/server/db.php index 8ed0f736..e13035f3 100755 --- a/front/php/server/db.php +++ b/front/php/server/db.php @@ -58,37 +58,83 @@ function SQLite3_connect ($trytoreconnect, $retryCount = 0) { } +//------------------------------------------------------------------------------ +// ->query override to handle retries +//------------------------------------------------------------------------------ +class CustomDatabaseWrapper { + private $sqlite; + private $maxRetries; + private $retryDelay; + + + public function __construct($filename, $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $maxRetries = 3, $retryDelay = 1000, $encryptionKey = null) { + $this->sqlite = new SQLite3($filename, $flags, $encryptionKey); + + $this->maxRetries = $maxRetries; + $this->retryDelay = $retryDelay; + } + + public function query(string $query): SQLite3Result|bool { + global $DBFILE_LOCKED_FILE; + + $attempts = 0; + while ($attempts < $this->maxRetries) { + $result = $this->sqlite->query($query); + if ($result !== false) { + // Write unlock status to the locked file + file_put_contents($DBFILE_LOCKED_FILE, '0'); + return $result; + } + + // Write lock status to the locked file + file_put_contents($DBFILE_LOCKED_FILE, '1'); + + $attempts++; + usleep($this->retryDelay * 1000); // Retry delay in milliseconds + } + + // If all retries failed, throw an exception or handle the error as needed + echo ''; + throw new Exception("Query failed after {$this->maxRetries} attempts: " . $this->sqlite->lastErrorMsg()); + } + + // Delegate other SQLite3 methods to the $sqlite instance + public function __call($name, $arguments) { + return call_user_func_array([$this->sqlite, $name], $arguments); + } +} + + //------------------------------------------------------------------------------ // Open DB //------------------------------------------------------------------------------ -function OpenDB (...$DBPath) { +function OpenDB($DBPath = null) { global $DBFILE; global $db; - // use custom path if supplied - foreach ($DBPath as $path) { - $DBFILE = $path; - } - - if(strlen($DBFILE) == 0) - { - echo ''; - die ('
Database not available
'); + // Use custom path if supplied + if ($DBPath !== null) { + $DBFILE = $DBPath; } - $db = SQLite3_connect(true); - - if(!$db) - { - echo ''; - die ('
Error connecting to the database
'); + if (strlen($DBFILE) == 0) { + echo ''; + die('
Database not available
'); + } + + try { + $db = new CustomDatabaseWrapper($DBFILE); + } catch (Exception $e) { + echo ''; + die('
Error connecting to the database
'); } $db->exec('PRAGMA journal_mode = wal;'); } + // # Open DB once and keep open // # Opening / closing DB frequently actually casues more issues OpenDB (); // main