From 5463e173a5db7c7b7eb88f545e00e3a1d986da57 Mon Sep 17 00:00:00 2001 From: Collin Date: Tue, 5 Mar 2024 15:02:45 +0000 Subject: [PATCH] Small update for more stability --- build/run-phar.sh | 2 +- build_config.json | 2 +- src/LonaDB/LonaDB.php | 92 ++++++++++++---------------- src/LonaDB/Plugins/PluginManager.php | 2 + src/LonaDB/Server.php | 7 ++- 5 files changed, 48 insertions(+), 57 deletions(-) diff --git a/build/run-phar.sh b/build/run-phar.sh index 25e9c72..59b7983 100755 --- a/build/run-phar.sh +++ b/build/run-phar.sh @@ -1 +1 @@ -cd build/release ; printf "test\n" | php -dextension=openswoole.so -dphar.readonly=0 LonaDB-4.3.0-stable.phar \ No newline at end of file +cd build/release ; printf "test\n" | php -dextension=openswoole.so -dphar.readonly=0 LonaDB-4.3.1-stable.phar \ No newline at end of file diff --git a/build_config.json b/build_config.json index 2329c20..77f516b 100644 --- a/build_config.json +++ b/build_config.json @@ -1,6 +1,6 @@ { "filename": "LonaDB", - "version": "4.3.0-stable", + "version": "4.3.1-stable", "path": "build", "debug": false } \ No newline at end of file diff --git a/src/LonaDB/LonaDB.php b/src/LonaDB/LonaDB.php index 477f663..942e879 100644 --- a/src/LonaDB/LonaDB.php +++ b/src/LonaDB/LonaDB.php @@ -14,7 +14,7 @@ use LonaDB\Plugins\PluginManager; class LonaDB { public array $config; - private bool $Running = true; + public bool $Running = false; public string $EncryptionKey; public Logger $Logger; @@ -27,6 +27,7 @@ class LonaDB { public function __construct(string $key) { $this->EncryptionKey = $key; $this->Logger = new Logger($this); + $run = false; try{ echo chr(27).chr(91).'H'.chr(27).chr(91).'J'; @@ -42,60 +43,36 @@ class LonaDB { $decrypted = openssl_decrypt($parts[0], AES_256_CBC, $this->EncryptionKey, 0, base64_decode($parts[1])); if(!json_decode($decrypted, true)) { echo "Encryption Key does not match the existing Configuration file. Exiting.\n"; - exit; + }else $run = true; + } + + if($run){ + $this->Logger->InfoCache("Loading config."); + $parts = explode(':', file_get_contents("./configuration.lona")); + $decrypted = openssl_decrypt($parts[0], AES_256_CBC, $this->EncryptionKey, 0, base64_decode($parts[1])); + $this->config = json_decode($decrypted, true); + + $this->Logger->InfoCache("Checking config."); + if(!$this->config["port"] || !$this->config["address"] || !$this->config["encryptionKey"] || !$this->config["root"]) { + $this->setup(); } - } - $this->Logger->InfoCache("Loading config."); - $parts = explode(':', file_get_contents("./configuration.lona")); - $decrypted = openssl_decrypt($parts[0], AES_256_CBC, $this->EncryptionKey, 0, base64_decode($parts[1])); - $this->config = json_decode($decrypted, true); + $this->Logger->LoadLogger(); + $this->Logger->DropCache(); - $this->Logger->InfoCache("Checking config."); - if(!$this->config["port"] || !$this->config["address"] || !$this->config["encryptionKey"] || !$this->config["root"]) { - $this->setup(); - } - - $this->Logger->LoadLogger(); - $this->Logger->DropCache(); - - $this->Logger->Info("Loading TableManager class."); - $this->TableManager = new TableManager($this); - $this->Logger->Info("Loading UserManager class."); - $this->UserManager = new UserManager($this); - $this->Logger->Info("Loading FunctionManager class."); - $this->FunctionManager = new FunctionManager($this); - - $this->Logger->Info("Loading PluginManager class."); - $this->PluginManager = new PluginManager($this); - - $pid = @ pcntl_fork(); - if( $pid == -1 ) { - throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); - } - if( $pid ) { - // parent - $this->pid = $pid; - } - else { - $this->PluginManager->LoadPlugins(); - } - - $serverpid = @ pcntl_fork(); - if( $serverpid == -1 ) { - throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); - } - if( $serverpid ) { - // parent - $this->serverpid = $serverpid; - } - else { - $this->Logger->Info("Loading Server class."); - $this->Server = new Server($this); - } - - while($this->Running){ + $this->Logger->Info("Loading TableManager class."); + $this->TableManager = new TableManager($this); + $this->Logger->Info("Loading UserManager class."); + $this->UserManager = new UserManager($this); + $this->Logger->Info("Loading FunctionManager class."); + $this->FunctionManager = new FunctionManager($this); + $this->Logger->Info("Loading PluginManager class."); + $this->PluginManager = new PluginManager($this); + if(!$this->Running){ + $this->Logger->Info("Loading Server class."); + $this->Server = new Server($this); + } } } catch (\Exception $e){ @@ -103,15 +80,19 @@ class LonaDB { } } - private function Load() : void { + public function LoadPlugins() : void { + if($this->PluginManager->Loaded) return; + $pid = @ pcntl_fork(); if( $pid == -1 ) { throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); } if( $pid ) { + // parent $this->pid = $pid; } else { + $this->PluginManager->LoadPlugins(); } } @@ -160,9 +141,12 @@ class LonaDB { } public function Stop() : void { + echo chr(27).chr(91).'H'.chr(27).chr(91).'J'; + echo "[Shutdown]\n"; + echo "Killing threads...\n"; posix_kill( $this->pid, SIGKILL ); - posix_kill( $this->serverpid, SIGKILL ); - $this->Running = false; + $this->Server->Stop(); + echo "Done!\n"; } } diff --git a/src/LonaDB/Plugins/PluginManager.php b/src/LonaDB/Plugins/PluginManager.php index 90b969a..71201f8 100644 --- a/src/LonaDB/Plugins/PluginManager.php +++ b/src/LonaDB/Plugins/PluginManager.php @@ -9,6 +9,7 @@ class PluginManager{ private LonaDB $LonaDB; private array $Plugins; private array $EnabledPlugins; + public bool $Loaded = false; public function __construct(LonaDB $lonaDB) { $this->LonaDB = $lonaDB; @@ -16,6 +17,7 @@ class PluginManager{ } public function LoadPlugins () : void { + $this->Loaded = true; if(!is_dir("plugins/")) mkdir("plugins/"); $results = scandir("plugins/"); diff --git a/src/LonaDB/Server.php b/src/LonaDB/Server.php index 4ed03bb..77b0371 100644 --- a/src/LonaDB/Server.php +++ b/src/LonaDB/Server.php @@ -18,6 +18,10 @@ class Server { public function __construct(LonaDB $lonaDB) { $this->LonaDB = $lonaDB; + + if($this->LonaDB->Running) return; + + $this->LonaDB->Running = true; $this->config = $lonaDB->config; $this->address = $this->config["address"]; @@ -51,13 +55,14 @@ class Server { }); try{ + $this->LonaDB->LoadPlugins(); $this->server->start(); } catch(e){ $this->LonaDB->Logger->Error(e); } } - + public function Stop() : void { $this->server->stop(); }