🔒DB lock v0.3 #685 + cleanup

This commit is contained in:
jokob-sk
2024-05-31 19:30:11 +10:00
parent 29c3a46170
commit 2e9aa37cd2
3 changed files with 67 additions and 24 deletions

View File

@@ -500,14 +500,10 @@
<!-- Buttons -->
<div class="col-xs-12">
<div class="pull-right">
<button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;"
id="btnDeleteEvents" onclick="askDeleteDeviceEvents()"> <?= lang('DevDetail_button_DeleteEvents');?> </button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;"
id="btnDelete" onclick="askDeleteDevice()"> <?= lang('DevDetail_button_Delete');?> </button>
<button type="button" class="btn btn-default pa-btn" style="margin-left:6px;"
id="btnRestore" onclick="getDeviceData(true)"> <?= lang('DevDetail_button_Reset');?> </button>
<button type="button" disabled class="btn btn-primary pa-btn" style="margin-left:6px; "
id="btnSave" onclick="setDeviceData()" > <?= lang('DevDetail_button_Save');?> </button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;" id="btnDeleteEvents" onclick="askDeleteDeviceEvents()"> <?= lang('DevDetail_button_DeleteEvents');?> </button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;" id="btnDelete" onclick="askDeleteDevice()"> <?= lang('DevDetail_button_Delete');?> </button>
<!-- <button type="button" class="btn btn-default pa-btn" style="margin-left:6px;" id="btnRestore" onclick="getDeviceData(true)"> <?= lang('DevDetail_button_Reset');?> </button> -->
<button type="button" disabled class="btn btn-primary pa-btn" style="margin-left:6px; " id="btnSave" onclick="setDeviceData()" > <?= lang('DevDetail_button_Save');?> </button>
</div>
</div>

View File

@@ -467,6 +467,7 @@ $db->close();
<div class="row logs-row" >
<div>
<div class="log-file" title="/var/log/nginx/error.log">nginx/error.log</div>
<span class="span-padding"><a href="/var/log/nginx/error.log"><i class="fa fa-download"></i> </a></span>
</div>
</div>
</div>

View File

@@ -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 '<script>alert("Error executing query (attempts: ' . $attempts . '"), query: '.$query.'</script>';
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 '<script>alert("Database not available")</script>';
die ('<div style="padding-left:150px">Database not available</div>');
// Use custom path if supplied
if ($DBPath !== null) {
$DBFILE = $DBPath;
}
$db = SQLite3_connect(true);
if(!$db)
{
echo '<script>alert("Error connecting to the database")</script>';
die ('<div style="padding-left:150px">Error connecting to the database</div>');
if (strlen($DBFILE) == 0) {
echo '<script>alert("Database not available")</script>';
die('<div style="padding-left:150px">Database not available</div>');
}
try {
$db = new CustomDatabaseWrapper($DBFILE);
} catch (Exception $e) {
echo '<script>alert("Error connecting to the database: ' . $e->getMessage() . '")</script>';
die('<div style="padding-left:150px">Error connecting to the database</div>');
}
$db->exec('PRAGMA journal_mode = wal;');
}
// # Open DB once and keep open
// # Opening / closing DB frequently actually casues more issues
OpenDB (); // main