From 74f1177ec6f8c8bde49df1e882f44be648ea07ef Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Thu, 30 May 2024 16:43:15 +1000 Subject: [PATCH] DB lock v0.2 #685 --- front/php/server/db.php | 51 +++++++++++++++---------- front/php/server/dbHelper.php | 30 ++++++++++++--- front/php/templates/language/de_de.json | 0 front/php/templates/language/es_es.json | 0 front/php/templates/language/it_it.json | 0 front/php/templates/language/ru_ru.json | 0 6 files changed, 54 insertions(+), 27 deletions(-) mode change 100644 => 100755 front/php/templates/language/de_de.json mode change 100644 => 100755 front/php/templates/language/es_es.json mode change 100644 => 100755 front/php/templates/language/it_it.json mode change 100644 => 100755 front/php/templates/language/ru_ru.json diff --git a/front/php/server/db.php b/front/php/server/db.php index 48243f8e..8ed0f736 100755 --- a/front/php/server/db.php +++ b/front/php/server/db.php @@ -5,45 +5,54 @@ // // db.php - Front module. Server side. DB common file //------------------------------------------------------------------------------ -# Puche 2021 / 2022+ jokob jokob@duck.com GNU GPLv3 +# 2022 jokob jokob@duck.com GNU GPLv3 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ // DB File Path $DBFILE = dirname(__FILE__).'/../../../db/app.db'; +$DBFILE_LOCKED_FILE = dirname(__FILE__).'/../../../logs/db_is_locked.log'; $db_locked = false; //------------------------------------------------------------------------------ // Connect DB //------------------------------------------------------------------------------ -function SQLite3_connect ($trytoreconnect) { - global $DBFILE; - try - { - // connect to database +function SQLite3_connect ($trytoreconnect, $retryCount = 0) { + global $DBFILE, $DBFILE_LOCKED_FILE; + $maxRetries = 5; // Maximum number of retries + $baseDelay = 1; // Base delay in seconds - global $db_locked; + try { + // Connect to database + global $db_locked; $db_locked = false; - // return new SQLite3($DBFILE, SQLITE3_OPEN_READONLY); - return new SQLite3($DBFILE, SQLITE3_OPEN_READWRITE); - } - catch (Exception $exception) - { - // sqlite3 throws an exception when it is unable to connect - global $db_locked; + // Write unlock status to the locked file + file_put_contents($DBFILE_LOCKED_FILE, '0'); + return new SQLite3($DBFILE, SQLITE3_OPEN_READWRITE); + } catch (Exception $exception) { + // sqlite3 throws an exception when it is unable to connect + global $db_locked; $db_locked = true; - // try to reconnect one time after 3 seconds - - if($trytoreconnect) - { - echo ''; - sleep(3); - return SQLite3_connect(false); + // Write lock status to the locked file + file_put_contents($DBFILE_LOCKED_FILE, '1'); + + // Connection failed, check if we should retry + if ($trytoreconnect && $retryCount < $maxRetries) { + // Calculate exponential backoff delay + $delay = $baseDelay * pow(2, $retryCount); + sleep($delay); + + // Retry the connection with an increased retry count + return SQLite3_connect(true, $retryCount + 1); + } else { + // Maximum retries reached, hide loading spinner and show failure alert + echo ''; + return false; // Or handle the failure appropriately } } } diff --git a/front/php/server/dbHelper.php b/front/php/server/dbHelper.php index 7cf6239e..4362f8cb 100755 --- a/front/php/server/dbHelper.php +++ b/front/php/server/dbHelper.php @@ -72,7 +72,7 @@ case 'read' : read($rawSql); break; case 'update': update($columnName, $id, $defaultValue, $expireMinutes, $dbtable, $columns, $values); break; case 'delete': delete($columnName, $id, $dbtable); break; - case 'checkLock': checkLock(); break; + case 'checkLock': checkLock(); break; default: logServerConsole ('Action: '. $action); break; } } @@ -269,26 +269,44 @@ function delete($columnName, $id, $dbtable) // check if the database is locked //------------------------------------------------------------------------------ function checkLock() { + + return checkLock_file() or checkLock_db(); + +} + +function checkLock_db() { global $DBFILE, $db_locked; $file = fopen($DBFILE, 'r+'); if (!$file or $db_locked) { - echo 1; // Could not open the file - return; + // Could not open the file + return 1; } if (flock($file, LOCK_EX | LOCK_NB)) { // Lock acquired, meaning the database is not locked by another process flock($file, LOCK_UN); // Release the lock - echo 0; // Not locked + return 0; // Not locked } else { // Could not acquire lock, meaning the database is locked - echo 1; // Locked + fclose($file); + return 1; // Locked } - fclose($file); + } +function checkLock_file() { + $DBFILE_LOCKED_FILE = dirname(__FILE__).'/../../../logs/db_is_locked.log'; + + if (file_exists($DBFILE_LOCKED_FILE)) { + $status = file_get_contents($DBFILE_LOCKED_FILE); + return $status; // Output the content of the lock file (0 or 1) + } else { + return '0'; // If the file doesn't exist, consider it as unlocked + } + exit; +} ?> diff --git a/front/php/templates/language/de_de.json b/front/php/templates/language/de_de.json old mode 100644 new mode 100755 diff --git a/front/php/templates/language/es_es.json b/front/php/templates/language/es_es.json old mode 100644 new mode 100755 diff --git a/front/php/templates/language/it_it.json b/front/php/templates/language/it_it.json old mode 100644 new mode 100755 diff --git a/front/php/templates/language/ru_ru.json b/front/php/templates/language/ru_ru.json old mode 100644 new mode 100755