From 29fb40bd160f3c4280a19381efdd5930412dd202 Mon Sep 17 00:00:00 2001 From: Joao Patricio Date: Tue, 21 Mar 2023 10:32:38 +0000 Subject: [PATCH] Refactoring: extract process handling from async job. --- .../RemoteProcess/DispatchRemoteProcess.php | 23 +++---------------- .../RemoteProcess/RunRemoteProcess.php | 6 +---- app/Data/RemoteProcessArgs.php | 12 +++++----- app/Jobs/ExecuteCoolifyProcess.php | 2 +- bootstrap/helpers.php | 20 ++++++++++++---- tests/Feature/DockerCommandsTest.php | 3 +-- 6 files changed, 27 insertions(+), 39 deletions(-) diff --git a/app/Actions/RemoteProcess/DispatchRemoteProcess.php b/app/Actions/RemoteProcess/DispatchRemoteProcess.php index 09dc2348e..c907c7a7d 100644 --- a/app/Actions/RemoteProcess/DispatchRemoteProcess.php +++ b/app/Actions/RemoteProcess/DispatchRemoteProcess.php @@ -3,32 +3,16 @@ namespace App\Actions\RemoteProcess; use App\Data\RemoteProcessArgs; -use App\Enums\ActivityTypes; -use App\Enums\ProcessStatus; use App\Jobs\ExecuteCoolifyProcess; -use Spatie\Activitylog\Contracts\Activity; +use Spatie\Activitylog\Models\Activity; class DispatchRemoteProcess { protected Activity $activity; - // TODO Left 'root' as default user instead of 'coolify' because - // there's a task at TODO.md to run docker without sudo - public function __construct( - protected string $destination, - protected string $command, - protected ?int $port = 22, - protected ?string $user = 'root', - ){ - $arguments = new RemoteProcessArgs( - destination: $this->destination, - command: $this->command, - port: $this->port, - user: $this->user, - ); - + public function __construct(RemoteProcessArgs $remoteProcessArgs){ $this->activity = activity() - ->withProperties($arguments->toArray()) + ->withProperties($remoteProcessArgs->toArray()) ->log("Awaiting command to start...\n\n"); } @@ -42,5 +26,4 @@ public function __invoke(): Activity return $this->activity; } - } diff --git a/app/Actions/RemoteProcess/RunRemoteProcess.php b/app/Actions/RemoteProcess/RunRemoteProcess.php index b0694f53d..69dc35465 100644 --- a/app/Actions/RemoteProcess/RunRemoteProcess.php +++ b/app/Actions/RemoteProcess/RunRemoteProcess.php @@ -7,7 +7,7 @@ use Illuminate\Process\ProcessResult; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Process; -use Spatie\Activitylog\Contracts\Activity; +use Spatie\Activitylog\Models\Activity; class RunRemoteProcess { @@ -73,10 +73,6 @@ protected function getCommand(): string . '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' . '-o PasswordAuthentication=no ' . '-o RequestTTY=no ' - // Quiet mode. Causes most warning and diagnostic messages to be suppressed. - // Errors are still out put. This is to silence for example, that warning - // Permanently added to the list of known hosts. - . '-q ' . "-p {$port} " . "{$user}@{$destination} " . " 'bash -se' << \\$delimiter" . PHP_EOL diff --git a/app/Data/RemoteProcessArgs.php b/app/Data/RemoteProcessArgs.php index a94e461fb..65ed0e645 100644 --- a/app/Data/RemoteProcessArgs.php +++ b/app/Data/RemoteProcessArgs.php @@ -9,11 +9,11 @@ class RemoteProcessArgs extends Data { public function __construct( - protected string $destination, - protected string $command, - protected int $port = 22, - protected string $user = 'root', - protected string $type = ActivityTypes::COOLIFY_PROCESS->value, - protected string $status = ProcessStatus::HOLDING->value, + public string $destination, + public string $command, + public int $port, + public string $user, + public string $type = ActivityTypes::COOLIFY_PROCESS->value, + public string $status = ProcessStatus::HOLDING->value, ){} } diff --git a/app/Jobs/ExecuteCoolifyProcess.php b/app/Jobs/ExecuteCoolifyProcess.php index fdb460361..9cdcaa809 100755 --- a/app/Jobs/ExecuteCoolifyProcess.php +++ b/app/Jobs/ExecuteCoolifyProcess.php @@ -8,7 +8,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use Spatie\Activitylog\Contracts\Activity; +use Spatie\Activitylog\Models\Activity; class ExecuteCoolifyProcess implements ShouldQueue { diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index 966206c43..c7517b0db 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -1,19 +1,29 @@ $destination, - 'command' => $command, + 'remoteProcessArgs' => new RemoteProcessArgs( + destination: $destination, + command: $command, + port: $port, + user: $user, + ), ])(); } } diff --git a/tests/Feature/DockerCommandsTest.php b/tests/Feature/DockerCommandsTest.php index 918025525..8fc551a04 100644 --- a/tests/Feature/DockerCommandsTest.php +++ b/tests/Feature/DockerCommandsTest.php @@ -14,12 +14,11 @@ // Assert there's no containers start with coolify_test_* $activity = remoteProcess($areThereCoolifyTestContainers, $host); - ray($activity); $containers = Output::containerList($activity->getExtraProperty('stdout')); expect($containers)->toBeEmpty(); // start a container nginx -d --name = $containerName - $activity = remoteProcess("docker run -d --name {$containerName} nginx", $host); + $activity = remoteProcess("docker run -d --rm --name {$containerName} nginx", $host); expect($activity->getExtraProperty('exitCode'))->toBe(0); // docker ps name = $container