From 720bb8c4786d52e7881927b67a53c65fbf181534 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 11 Dec 2023 09:02:53 +0100 Subject: [PATCH] fix: database ui is realtime based --- app/Actions/Database/StartMariadb.php | 2 +- app/Actions/Database/StartMongodb.php | 2 +- app/Actions/Database/StartMysql.php | 2 +- app/Actions/Database/StartPostgresql.php | 2 +- app/Actions/Database/StartRedis.php | 2 +- app/Actions/Database/StopDatabase.php | 1 + app/Events/DatabaseStatusChanged.php | 34 +++++++++++++++++++++++ app/Livewire/Project/Database/Heading.php | 9 +++++- 8 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 app/Events/DatabaseStatusChanged.php diff --git a/app/Actions/Database/StartMariadb.php b/app/Actions/Database/StartMariadb.php index 21fcdb8a5..5ea86a551 100644 --- a/app/Actions/Database/StartMariadb.php +++ b/app/Actions/Database/StartMariadb.php @@ -105,7 +105,7 @@ class StartMariadb $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml pull"; $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d"; $this->commands[] = "echo '{$database->name} started.'"; - return remote_process($this->commands, $database->destination->server); + return remote_process($this->commands, $database->destination->server, callEventOnFinish: 'DatabaseStatusChanged'); } private function generate_local_persistent_volumes() diff --git a/app/Actions/Database/StartMongodb.php b/app/Actions/Database/StartMongodb.php index e0197a7bc..9ce35bbc2 100644 --- a/app/Actions/Database/StartMongodb.php +++ b/app/Actions/Database/StartMongodb.php @@ -121,7 +121,7 @@ class StartMongodb $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml pull"; $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d"; $this->commands[] = "echo '{$database->name} started.'"; - return remote_process($this->commands, $database->destination->server); + return remote_process($this->commands, $database->destination->server,callEventOnFinish: 'DatabaseStatusChanged'); } private function generate_local_persistent_volumes() diff --git a/app/Actions/Database/StartMysql.php b/app/Actions/Database/StartMysql.php index 76e8af619..d37821332 100644 --- a/app/Actions/Database/StartMysql.php +++ b/app/Actions/Database/StartMysql.php @@ -105,7 +105,7 @@ class StartMysql $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml pull"; $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d"; $this->commands[] = "echo '{$database->name} started.'"; - return remote_process($this->commands, $database->destination->server); + return remote_process($this->commands, $database->destination->server,callEventOnFinish: 'DatabaseStatusChanged'); } private function generate_local_persistent_volumes() diff --git a/app/Actions/Database/StartPostgresql.php b/app/Actions/Database/StartPostgresql.php index 97ae9da0e..99fb56ba4 100644 --- a/app/Actions/Database/StartPostgresql.php +++ b/app/Actions/Database/StartPostgresql.php @@ -131,7 +131,7 @@ class StartPostgresql $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml pull"; $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d"; $this->commands[] = "echo '{$database->name} started.'"; - return remote_process($this->commands, $database->destination->server); + return remote_process($this->commands, $database->destination->server, callEventOnFinish: 'DatabaseStatusChanged'); } private function generate_local_persistent_volumes() diff --git a/app/Actions/Database/StartRedis.php b/app/Actions/Database/StartRedis.php index fcb87b891..4e3773726 100644 --- a/app/Actions/Database/StartRedis.php +++ b/app/Actions/Database/StartRedis.php @@ -115,7 +115,7 @@ class StartRedis $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml pull"; $this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d"; $this->commands[] = "echo '{$database->name} started.'"; - return remote_process($this->commands, $database->destination->server); + return remote_process($this->commands, $database->destination->server, callEventOnFinish: 'DatabaseStatusChanged'); } private function generate_local_persistent_volumes() diff --git a/app/Actions/Database/StopDatabase.php b/app/Actions/Database/StopDatabase.php index 4f6a8c6c2..e6316ecc9 100644 --- a/app/Actions/Database/StopDatabase.php +++ b/app/Actions/Database/StopDatabase.php @@ -2,6 +2,7 @@ namespace App\Actions\Database; +use App\Events\DatabaseStatusChanged; use App\Models\StandaloneMariadb; use App\Models\StandaloneMongodb; use App\Models\StandaloneMysql; diff --git a/app/Events/DatabaseStatusChanged.php b/app/Events/DatabaseStatusChanged.php new file mode 100644 index 000000000..9c1e54cfd --- /dev/null +++ b/app/Events/DatabaseStatusChanged.php @@ -0,0 +1,34 @@ +user()->id ?? null; + } + if (is_null($userId)) { + throw new \Exception("User id is null"); + } + $this->userId = $userId; + } + + public function broadcastOn(): array + { + return [ + new PrivateChannel("custom.{$this->userId}"), + ]; + } +} diff --git a/app/Livewire/Project/Database/Heading.php b/app/Livewire/Project/Database/Heading.php index 3e5442677..1c94f9573 100644 --- a/app/Livewire/Project/Database/Heading.php +++ b/app/Livewire/Project/Database/Heading.php @@ -8,6 +8,7 @@ use App\Actions\Database\StartMysql; use App\Actions\Database\StartPostgresql; use App\Actions\Database\StartRedis; use App\Actions\Database\StopDatabase; +use App\Events\DatabaseStatusChanged; use App\Jobs\ContainerStatusJob; use Livewire\Component; @@ -16,7 +17,13 @@ class Heading extends Component public $database; public array $parameters; - protected $listeners = ['activityFinished']; + public function getListeners() + { + $userId = auth()->user()->id; + return [ + "echo-private:custom.{$userId},DatabaseStatusChanged" => 'activityFinished', + ]; + } public function activityFinished() {