Add database and Redis port configuration support, connection health checks, and refactor worker status endpoint.
This commit is contained in:
@@ -13,6 +13,7 @@ class db
|
||||
private string $user;
|
||||
private string $password;
|
||||
private string $database;
|
||||
private int $port = 3306;
|
||||
private string $ssl_mode = 'DISABLED'; // mysqldump SSL mode (e.g., DISABLED, PREFERRED, REQUIRED, VERIFY_CA, VERIFY_IDENTITY)
|
||||
|
||||
public function __construct(array $config)
|
||||
@@ -21,6 +22,9 @@ class db
|
||||
$this->user = $config['user'];
|
||||
$this->password = $config['password'];
|
||||
$this->database = $config['database'];
|
||||
if (isset($config['port']) && is_numeric($config['port'])) {
|
||||
$this->port = (int)$config['port'];
|
||||
}
|
||||
if (isset($config['ssl_mode']) && is_string($config['ssl_mode']) && $config['ssl_mode'] !== '') {
|
||||
$this->ssl_mode = $config['ssl_mode'];
|
||||
}
|
||||
@@ -28,22 +32,46 @@ class db
|
||||
|
||||
public static function getPDO(): \PDO
|
||||
{
|
||||
global $config;
|
||||
$dsn = "mysql:host={$config['db']['host']};dbname={$config['db']['database']};charset=utf8mb4";
|
||||
return new \PDO($dsn, $config['db']['user'], $config['db']['password'], [
|
||||
global $CONFIG_DB;
|
||||
$port = $CONFIG_DB['port'] ?? 3306;
|
||||
$dsn = "mysql:host={$CONFIG_DB['host']};port={$port};dbname={$CONFIG_DB['database']};charset=utf8mb4";
|
||||
return new \PDO($dsn, $CONFIG_DB['user'], $CONFIG_DB['password'], [
|
||||
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
||||
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
|
||||
\PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testConnection(): bool
|
||||
{
|
||||
try {
|
||||
$conn = new mysqli($this->host, $this->user, $this->password, $this->database, $this->port);
|
||||
if ($conn->connect_error) {
|
||||
return false;
|
||||
}
|
||||
$this->conn = $conn;
|
||||
return true;
|
||||
} catch (Exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function connect(): void
|
||||
{
|
||||
global $response;
|
||||
try {
|
||||
$this->conn = new mysqli($this->host, $this->user, $this->password, $this->database);
|
||||
// Enable error reporting for mysqli to catch connection issues via exceptions
|
||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||
$this->conn = new mysqli($this->host, $this->user, $this->password, $this->database, $this->port);
|
||||
if ($this->conn->connect_error) {
|
||||
throw new Exception($this->conn->connect_error);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$response->internal_server_error($e->getMessage());
|
||||
if ($response) {
|
||||
$response->internal_server_error("Database connection failed: " . $e->getMessage());
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,9 +198,10 @@ class db
|
||||
$user = escapeshellarg($this->user);
|
||||
$pass = escapeshellarg($this->password);
|
||||
$db = escapeshellarg($this->database);
|
||||
$port = (int)$this->port;
|
||||
$outfile = escapeshellarg($path);
|
||||
$sslPart = $sslFlag !== '' ? ($sslFlag . ' ') : '';
|
||||
$command = "mysqldump {$sslPart}-h $host -u $user --password=$pass $db > $outfile 2>&1";
|
||||
$command = "mysqldump {$sslPart}-h $host -P $port -u $user --password=$pass $db > $outfile 2>&1";
|
||||
exec($command, $output, $return);
|
||||
// Check if the command was successful
|
||||
return $return === 0;
|
||||
|
||||
Reference in New Issue
Block a user