⚙ settings saving improvements + refactor - DB lock v0.1 #685

This commit is contained in:
jokob-sk
2024-05-29 19:24:43 +10:00
parent 3853b8a4ec
commit bb2beda12a
10 changed files with 334 additions and 302 deletions

View File

@@ -12,6 +12,8 @@
// DB File Path
$DBFILE = dirname(__FILE__).'/../../../db/app.db';
$db_locked = false;
//------------------------------------------------------------------------------
// Connect DB
//------------------------------------------------------------------------------
@@ -20,12 +22,21 @@ function SQLite3_connect ($trytoreconnect) {
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;
$db_locked = true;
// try to reconnect one time after 3 seconds
if($trytoreconnect)

View File

@@ -269,16 +269,26 @@ function delete($columnName, $id, $dbtable)
// check if the database is locked
//------------------------------------------------------------------------------
function checkLock() {
global $db;
try {
$db->exec('BEGIN EXCLUSIVE TRANSACTION');
$db->exec('COMMIT');
echo 0; // Not locked
return 0;
} catch (Exception $e) {
echo 1; // Locked
return 1;
global $DBFILE, $db_locked;
$file = fopen($DBFILE, 'r+');
if (!$file or $db_locked) {
echo 1; // Could not open the file
return;
}
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
} else {
// Could not acquire lock, meaning the database is locked
echo 1; // Locked
}
fclose($file);
}
?>

View File

@@ -387,8 +387,10 @@ function saveSettings()
// Replace the original file with the temporary file
rename($tempConfPath, $fullConfPath);
displayMessage("<br/>Settings saved to the <code>app.conf</code> file.<br/><br/>A time-stamped backup of the previous file created. <br/><br/> Reloading...<br/>",
FALSE, TRUE, TRUE, TRUE);
// displayMessage(lang('settings_saved'),
// FALSE, TRUE, TRUE, TRUE);
echo "OK";
}