mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
DB lock v0.2 #685
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
try {
|
||||
// Connect to database
|
||||
global $db_locked;
|
||||
|
||||
$db_locked = false;
|
||||
|
||||
// return new SQLite3($DBFILE, SQLITE3_OPEN_READONLY);
|
||||
// Write unlock status to the locked file
|
||||
file_put_contents($DBFILE_LOCKED_FILE, '0');
|
||||
|
||||
return new SQLite3($DBFILE, SQLITE3_OPEN_READWRITE);
|
||||
}
|
||||
catch (Exception $exception)
|
||||
{
|
||||
} 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
|
||||
// Write lock status to the locked file
|
||||
file_put_contents($DBFILE_LOCKED_FILE, '1');
|
||||
|
||||
if($trytoreconnect)
|
||||
{
|
||||
echo '<script>alert("Error connecting to database, will try in 3s")</script>';
|
||||
sleep(3);
|
||||
return SQLite3_connect(false);
|
||||
// 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 '<script>alert("Failed to connect to database after ' . $retryCount . ' retries.")</script>';
|
||||
return false; // Or handle the failure appropriately
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
0
front/php/templates/language/de_de.json
Normal file → Executable file
0
front/php/templates/language/de_de.json
Normal file → Executable file
0
front/php/templates/language/es_es.json
Normal file → Executable file
0
front/php/templates/language/es_es.json
Normal file → Executable file
0
front/php/templates/language/it_it.json
Normal file → Executable file
0
front/php/templates/language/it_it.json
Normal file → Executable file
0
front/php/templates/language/ru_ru.json
Normal file → Executable file
0
front/php/templates/language/ru_ru.json
Normal file → Executable file
Reference in New Issue
Block a user