From cf121062a1886baa5f5f0892767538b8f8288e6a Mon Sep 17 00:00:00 2001 From: Joao Patricio Date: Tue, 21 Mar 2023 09:56:49 +0000 Subject: [PATCH] Refactoring: extract process handling from async job. --- .../RemoteProcess/DispatchRemoteProcess.php} | 11 +++++------ .../{RemoteProcess.php => RunRemoteProcess.php} | 16 ++++++++++++---- app/Enums/ActivityTypes.php | 8 ++++++++ app/Jobs/ExecuteCoolifyProcess.php | 4 ++-- bootstrap/helpers.php | 4 ++-- tests/Unit/RulesTest.php | 5 +++++ 6 files changed, 34 insertions(+), 14 deletions(-) rename app/{Services/CoolifyProcess.php => Actions/RemoteProcess/DispatchRemoteProcess.php} (87%) rename app/Actions/RemoteProcess/{RemoteProcess.php => RunRemoteProcess.php} (89%) create mode 100644 app/Enums/ActivityTypes.php create mode 100644 tests/Unit/RulesTest.php diff --git a/app/Services/CoolifyProcess.php b/app/Actions/RemoteProcess/DispatchRemoteProcess.php similarity index 87% rename from app/Services/CoolifyProcess.php rename to app/Actions/RemoteProcess/DispatchRemoteProcess.php index 102a94a74..c8a81699a 100644 --- a/app/Services/CoolifyProcess.php +++ b/app/Actions/RemoteProcess/DispatchRemoteProcess.php @@ -1,12 +1,13 @@ activity = activity() ->withProperties([ - 'type' => 'COOLIFY_PROCESS', + 'type' => ActivityTypes::COOLIFY_PROCESS, + 'status' => ProcessStatus::HOLDING, 'user' => $this->user, 'destination' => $this->destination, 'port' => $this->port, 'command' => $this->command, - 'status' => ProcessStatus::HOLDING, ]) ->log("Awaiting command to start...\n\n"); } @@ -38,8 +39,6 @@ class CoolifyProcess $this->activity->refresh(); - ray($this->activity->id); - return $this->activity; } diff --git a/app/Actions/RemoteProcess/RemoteProcess.php b/app/Actions/RemoteProcess/RunRemoteProcess.php similarity index 89% rename from app/Actions/RemoteProcess/RemoteProcess.php rename to app/Actions/RemoteProcess/RunRemoteProcess.php index 24c17dd8d..b0694f53d 100644 --- a/app/Actions/RemoteProcess/RemoteProcess.php +++ b/app/Actions/RemoteProcess/RunRemoteProcess.php @@ -2,14 +2,17 @@ namespace App\Actions\RemoteProcess; +use App\Enums\ActivityTypes; use App\Enums\ProcessStatus; use Illuminate\Process\ProcessResult; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Process; use Spatie\Activitylog\Contracts\Activity; -class RemoteProcess +class RunRemoteProcess { + public Activity $activity; + protected $timeStart; protected $currentTime; @@ -25,9 +28,14 @@ class RemoteProcess /** * Create a new job instance. */ - public function __construct( - public Activity $activity, - ){} + public function __construct(Activity $activity) + { + if ($activity->getExtraProperty('type') !== ActivityTypes::COOLIFY_PROCESS->value) { + throw new \RuntimeException('Incompatible Activity to run a remote command.'); + } + + $this->activity = $activity; + } public function __invoke(): ProcessResult { diff --git a/app/Enums/ActivityTypes.php b/app/Enums/ActivityTypes.php new file mode 100644 index 000000000..52a35e2f8 --- /dev/null +++ b/app/Enums/ActivityTypes.php @@ -0,0 +1,8 @@ + $this->activity, ]); diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index e4d1f51a7..966206c43 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -1,6 +1,6 @@ $destination, 'command' => $command, ])(); diff --git a/tests/Unit/RulesTest.php b/tests/Unit/RulesTest.php new file mode 100644 index 000000000..02e14ba48 --- /dev/null +++ b/tests/Unit/RulesTest.php @@ -0,0 +1,5 @@ +expect(['dd', 'dump', 'ray']) + ->not->toBeUsed();