🔒DB lock v0.4 #685 + 🔔userNotifications
This commit is contained in:
@@ -11,130 +11,184 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// DB File Path
|
||||
$DBFILE = dirname(__FILE__).'/../../../db/app.db';
|
||||
$DBFILE_LOCKED_FILE = dirname(__FILE__).'/../../../logs/db_is_locked.log';
|
||||
$DBFILE_LOCKED_FILE = dirname(__FILE__).'/../../../front/log/db_is_locked.log';
|
||||
|
||||
$db_locked = false;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Connect DB
|
||||
//------------------------------------------------------------------------------
|
||||
function SQLite3_connect ($trytoreconnect, $retryCount = 0) {
|
||||
global $DBFILE, $DBFILE_LOCKED_FILE;
|
||||
$maxRetries = 5; // Maximum number of retries
|
||||
$baseDelay = 1; // Base delay in seconds
|
||||
function SQLite3_connect($trytoreconnect = true, $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;
|
||||
|
||||
try {
|
||||
// Connect to database
|
||||
global $db_locked;
|
||||
$db_locked = false;
|
||||
// Write unlock status to the locked file
|
||||
file_put_contents($DBFILE_LOCKED_FILE, '0');
|
||||
|
||||
// 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;
|
||||
|
||||
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;
|
||||
// Write lock status to the locked file
|
||||
file_put_contents($DBFILE_LOCKED_FILE, '1');
|
||||
error_log("Failed to connect to database: " . $exception->getMessage());
|
||||
|
||||
// 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);
|
||||
|
||||
// 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
|
||||
// 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
|
||||
$message = 'Failed to connect to database after ' . $retryCount . ' retries.';
|
||||
write_notification($message);
|
||||
return false; // Or handle the failure appropriately
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// ->query override to handle retries
|
||||
//------------------------------------------------------------------------------
|
||||
class CustomDatabaseWrapper {
|
||||
private $sqlite;
|
||||
private $maxRetries;
|
||||
private $retryDelay;
|
||||
|
||||
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);
|
||||
public function __construct($filename, $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $maxRetries = 10, $retryDelay = 1000, $encryptionKey = null) {
|
||||
$this->sqlite = new SQLite3($filename, $flags, $encryptionKey);
|
||||
$this->maxRetries = $maxRetries;
|
||||
$this->retryDelay = $retryDelay;
|
||||
}
|
||||
|
||||
$this->maxRetries = $maxRetries;
|
||||
$this->retryDelay = $retryDelay;
|
||||
}
|
||||
|
||||
public function query(string $query): SQLite3Result|bool {
|
||||
public function query(string $query): SQLite3Result|bool {
|
||||
global $DBFILE_LOCKED_FILE;
|
||||
|
||||
// Check if the query is an UPDATE, DELETE, or INSERT
|
||||
$queryType = strtoupper(substr(trim($query), 0, strpos(trim($query), ' ')));
|
||||
$isModificationQuery = in_array($queryType, ['UPDATE', 'DELETE', 'INSERT']);
|
||||
|
||||
$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;
|
||||
while ($attempts < $this->maxRetries) {
|
||||
|
||||
$result = false;
|
||||
|
||||
try {
|
||||
$result = $this->sqlite->query($query);
|
||||
} catch (Exception $exception) {
|
||||
// continue unless maxRetries reached
|
||||
if($attempts > $this->maxRetries)
|
||||
{
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
if ($result !== false and $result !== null) {
|
||||
|
||||
$this->query_log_remove($query);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Write lock status to the locked file
|
||||
file_put_contents($DBFILE_LOCKED_FILE, '1');
|
||||
$this->query_log_add($query);
|
||||
|
||||
$attempts++;
|
||||
usleep($this->retryDelay * 1000); // Retry delay in milliseconds
|
||||
}
|
||||
usleep($this->retryDelay * 1000 * $attempts); // 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());
|
||||
}
|
||||
// If all retries failed, throw an exception or handle the error as needed
|
||||
// Add '0' to indicate that the database is not locked/execution failed
|
||||
file_put_contents($DBFILE_LOCKED_FILE, '0');
|
||||
|
||||
// Delegate other SQLite3 methods to the $sqlite instance
|
||||
public function __call($name, $arguments) {
|
||||
return call_user_func_array([$this->sqlite, $name], $arguments);
|
||||
}
|
||||
$message = 'Error executing query (attempts: ' . $attempts . '), query: ' . $query;
|
||||
write_notification($message);
|
||||
error_log("Query failed after {$this->maxRetries} attempts: " . $this->sqlite->lastErrorMsg());
|
||||
}
|
||||
|
||||
public function query_log_add($query)
|
||||
{
|
||||
global $DBFILE_LOCKED_FILE;
|
||||
|
||||
// Remove new lines from the query
|
||||
$query = str_replace(array("\r", "\n"), ' ', $query);
|
||||
|
||||
// Generate a hash of the query
|
||||
$queryHash = md5($query);
|
||||
|
||||
// Log the query being attempted along with timestamp and query hash
|
||||
$executionLog = "1|" . date('Y-m-d H:i:s') . "|$queryHash|$query";
|
||||
error_log("Attempting to write '$executionLog' to execution log file after failed query: $query");
|
||||
file_put_contents($DBFILE_LOCKED_FILE, $executionLog . PHP_EOL, FILE_APPEND);
|
||||
error_log("Execution log file content after failed query attempt: " . file_get_contents($DBFILE_LOCKED_FILE));
|
||||
}
|
||||
|
||||
public function query_log_remove($query)
|
||||
{
|
||||
global $DBFILE_LOCKED_FILE;
|
||||
|
||||
// Remove new lines from the query
|
||||
$query = str_replace(array("\r", "\n"), ' ', $query);
|
||||
|
||||
// Generate a hash of the query
|
||||
$queryHash = md5($query);
|
||||
|
||||
// Remove the entry corresponding to the finished query from the execution log based on query hash
|
||||
$executionLogs = file($DBFILE_LOCKED_FILE, FILE_IGNORE_NEW_LINES);
|
||||
$executionLogs = array_filter($executionLogs, function($log) use ($queryHash) {
|
||||
return strpos($log, $queryHash) === false;
|
||||
});
|
||||
file_put_contents($DBFILE_LOCKED_FILE, implode(PHP_EOL, $executionLogs));
|
||||
}
|
||||
|
||||
// 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 = null) {
|
||||
global $DBFILE;
|
||||
global $db;
|
||||
global $DBFILE;
|
||||
global $db;
|
||||
|
||||
// Use custom path if supplied
|
||||
if ($DBPath !== null) {
|
||||
$DBFILE = $DBPath;
|
||||
}
|
||||
// Use custom path if supplied
|
||||
if ($DBPath !== null) {
|
||||
$DBFILE = $DBPath;
|
||||
}
|
||||
|
||||
if (strlen($DBFILE) == 0) {
|
||||
echo '<script>alert("Database not available")</script>';
|
||||
die('<div style="padding-left:150px">Database not available</div>');
|
||||
}
|
||||
if (strlen($DBFILE) == 0) {
|
||||
$message = 'Database not available';
|
||||
echo '<script>alert('.$message.')</script>';
|
||||
write_notification($message);
|
||||
|
||||
die('<div style="padding-left:150px">'.$message.'</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>');
|
||||
}
|
||||
try {
|
||||
$db = new CustomDatabaseWrapper($DBFILE);
|
||||
} catch (Exception $e) {
|
||||
$message = "Error connecting to the database";
|
||||
echo '<script>alert('.$message.'": ' . $e->getMessage() . '")</script>';
|
||||
write_notification($message);
|
||||
die('<div style="padding-left:150px">'.$message.'</div>');
|
||||
}
|
||||
|
||||
$db->exec('PRAGMA journal_mode = wal;');
|
||||
$db->exec('PRAGMA journal_mode = wal;');
|
||||
}
|
||||
|
||||
|
||||
|
||||
// # Open DB once and keep open
|
||||
// # Opening / closing DB frequently actually casues more issues
|
||||
OpenDB (); // main
|
||||
// Open DB once and keep open
|
||||
OpenDB(); // main
|
||||
?>
|
||||
|
||||
@@ -48,6 +48,10 @@
|
||||
$id = $_REQUEST['id'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['delay'])) {
|
||||
$delay = $_REQUEST['delay'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['values'])) {
|
||||
$values = $_REQUEST['values'];
|
||||
}
|
||||
@@ -72,7 +76,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 'lockDatabase': lockDatabase($delay); break;
|
||||
default: logServerConsole ('Action: '. $action); break;
|
||||
}
|
||||
}
|
||||
@@ -264,49 +268,11 @@ 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) {
|
||||
// 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
|
||||
return 0; // Not locked
|
||||
} else {
|
||||
// Could not acquire lock, meaning the database is 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;
|
||||
// Simulate database locking by starting a transaction
|
||||
function lockDatabase($delay) {
|
||||
$db = new SQLite3($GLOBALS['DBFILE']);
|
||||
$db->exec('BEGIN EXCLUSIVE;');
|
||||
sleep($delay); // Sleep for N seconds to simulate long-running transaction
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
ini_set('error_log', '../../log/app.php_errors.log'); // initializing the app.php_errors.log file for the maintenance section
|
||||
require dirname(__FILE__).'/../templates/timezone.php';
|
||||
require dirname(__FILE__).'/db.php';
|
||||
require dirname(__FILE__).'/util.php';
|
||||
require dirname(__FILE__).'/../templates/language/lang.php';
|
||||
require dirname(__FILE__).'/utilNotification.php';
|
||||
?>
|
||||
|
||||
169
front/php/server/utilNotification.php
Executable file
169
front/php/server/utilNotification.php
Executable file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
// Check if the action parameter is set in the GET request
|
||||
if (isset($_GET['action'])) {
|
||||
// Collect GUID if provided
|
||||
$guid = isset($_GET['guid']) ? $_GET['guid'] : null;
|
||||
|
||||
// Perform the appropriate action based on the action parameter
|
||||
switch ($_GET['action']) {
|
||||
case 'write_notification':
|
||||
// Call the write_notification function with content and level parameters
|
||||
if (isset($_GET['content'])) {
|
||||
$content = $_GET['content'];
|
||||
$level = isset($_GET['level']) ? $_GET['level'] : "interrupt";
|
||||
write_notification($content, $level);
|
||||
}
|
||||
break;
|
||||
case 'remove_notification':
|
||||
// Call the remove_notification function with guid parameter
|
||||
if ($guid) {
|
||||
remove_notification($guid);
|
||||
}
|
||||
break;
|
||||
case 'mark_notification_as_read':
|
||||
// Call the mark_notification_as_read function with guid parameter
|
||||
if ($guid) {
|
||||
mark_notification_as_read($guid);
|
||||
}
|
||||
break;
|
||||
case 'notifications_clear':
|
||||
// Call the notifications_clear function
|
||||
notifications_clear();
|
||||
break;
|
||||
case 'get_unread_notifications':
|
||||
// Call the get_unread_notifications function
|
||||
get_unread_notifications();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function generate_guid() {
|
||||
if (function_exists('com_create_guid') === true) {
|
||||
return trim(com_create_guid(), '{}');
|
||||
}
|
||||
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
|
||||
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535),
|
||||
mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535),
|
||||
mt_rand(0, 65535), mt_rand(0, 65535));
|
||||
}
|
||||
|
||||
function write_notification($content, $level = "interrupt") {
|
||||
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
|
||||
|
||||
// Generate GUID
|
||||
$guid = generate_guid();
|
||||
|
||||
// Generate timestamp
|
||||
$timestamp = date("Y-m-d H:i:s");
|
||||
|
||||
// Escape content to prevent breaking JSON
|
||||
$escaped_content = json_encode($content);
|
||||
|
||||
// Prepare notification array
|
||||
$notification = array(
|
||||
'timestamp' => $timestamp,
|
||||
'guid' => $guid,
|
||||
'read' => 0,
|
||||
'level'=> $level,
|
||||
'content' => $escaped_content,
|
||||
);
|
||||
|
||||
// Read existing notifications
|
||||
$notifications = json_decode(file_get_contents($NOTIFICATION_API_FILE), true);
|
||||
|
||||
// Add new notification
|
||||
$notifications[] = $notification;
|
||||
|
||||
// Write notifications to file
|
||||
file_put_contents($NOTIFICATION_API_FILE, json_encode($notifications));
|
||||
}
|
||||
|
||||
function remove_notification($guid) {
|
||||
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
|
||||
|
||||
// Read existing notifications
|
||||
$notifications = json_decode(file_get_contents($NOTIFICATION_API_FILE), true);
|
||||
|
||||
// Filter out the notification with the specified GUID
|
||||
$filtered_notifications = array_filter($notifications, function($notification) use ($guid) {
|
||||
return $notification['guid'] !== $guid;
|
||||
});
|
||||
|
||||
// Write filtered notifications back to file
|
||||
file_put_contents($NOTIFICATION_API_FILE, json_encode(array_values($filtered_notifications)));
|
||||
}
|
||||
|
||||
function notifications_clear() {
|
||||
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
|
||||
|
||||
// Clear notifications by writing an empty array to the file
|
||||
file_put_contents($NOTIFICATION_API_FILE, json_encode(array()));
|
||||
}
|
||||
|
||||
function mark_notification_as_read($guid) {
|
||||
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
|
||||
$max_attempts = 3;
|
||||
$attempts = 0;
|
||||
|
||||
do {
|
||||
// Check if the file exists and is readable
|
||||
if (file_exists($NOTIFICATION_API_FILE) && is_readable($NOTIFICATION_API_FILE)) {
|
||||
// Attempt to read existing notifications
|
||||
$notifications = json_decode(file_get_contents($NOTIFICATION_API_FILE), true);
|
||||
|
||||
// Check if reading was successful
|
||||
if ($notifications !== null) {
|
||||
// Iterate over notifications to find the one with the specified GUID
|
||||
foreach ($notifications as &$notification) {
|
||||
if ($notification['guid'] === $guid) {
|
||||
// Mark the notification as read
|
||||
$notification['read'] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Write updated notifications back to file
|
||||
file_put_contents($NOTIFICATION_API_FILE, json_encode($notifications));
|
||||
return; // Exit the function after successful operation
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the attempt count
|
||||
$attempts++;
|
||||
|
||||
// Sleep for a short duration before retrying
|
||||
usleep(500000); // Sleep for 0.5 seconds (500,000 microseconds) before retrying
|
||||
|
||||
} while ($attempts < $max_attempts);
|
||||
|
||||
// If maximum attempts reached or file reading failed, handle the error
|
||||
echo "Failed to read notification file after $max_attempts attempts.";
|
||||
}
|
||||
|
||||
function get_unread_notifications() {
|
||||
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
|
||||
|
||||
// Read existing notifications
|
||||
if (file_exists($NOTIFICATION_API_FILE) && is_readable($NOTIFICATION_API_FILE)) {
|
||||
$notifications = json_decode(file_get_contents($NOTIFICATION_API_FILE), true);
|
||||
|
||||
if ($notifications !== null) {
|
||||
// Filter unread notifications
|
||||
$unread_notifications = array_filter($notifications, function($notification) {
|
||||
return $notification['read'] === 0;
|
||||
});
|
||||
|
||||
// Return unread notifications as JSON
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(array_values($unread_notifications));
|
||||
} else {
|
||||
echo json_encode([]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode([]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user