Refactoring: extract process handling from async job.

This commit is contained in:
Joao Patricio 2023-03-21 09:56:49 +00:00
parent 83a7ef21f3
commit cf121062a1
6 changed files with 34 additions and 14 deletions

View File

@ -1,12 +1,13 @@
<?php
namespace App\Services;
namespace App\Actions\RemoteProcess;
use App\Enums\ActivityTypes;
use App\Enums\ProcessStatus;
use App\Jobs\ExecuteCoolifyProcess;
use Spatie\Activitylog\Contracts\Activity;
class CoolifyProcess
class DispatchRemoteProcess
{
protected Activity $activity;
@ -20,12 +21,12 @@ public function __construct(
){
$this->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 @@ public function __invoke(): Activity
$this->activity->refresh();
ray($this->activity->id);
return $this->activity;
}

View File

@ -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
{

View File

@ -0,0 +1,8 @@
<?php
namespace App\Enums;
enum ActivityTypes: string
{
case COOLIFY_PROCESS = 'coolify_process';
}

View File

@ -2,7 +2,7 @@
namespace App\Jobs;
use App\Actions\RemoteProcess\RemoteProcess;
use App\Actions\RemoteProcess\RunRemoteProcess;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -26,7 +26,7 @@ public function __construct(
*/
public function handle(): void
{
$remoteProcess = resolve(RemoteProcess::class, [
$remoteProcess = resolve(RunRemoteProcess::class, [
'activity' => $this->activity,
]);

View File

@ -1,6 +1,6 @@
<?php
use App\Services\CoolifyProcess;
use App\Actions\RemoteProcess\DispatchRemoteProcess;
use Spatie\Activitylog\Contracts\Activity;
if (! function_exists('remoteProcess')) {
@ -11,7 +11,7 @@
*/
function remoteProcess($command, $destination): Activity
{
return resolve(CoolifyProcess::class, [
return resolve(DispatchRemoteProcess::class, [
'destination' => $destination,
'command' => $command,
])();

5
tests/Unit/RulesTest.php Normal file
View File

@ -0,0 +1,5 @@
<?php
test('globals')
->expect(['dd', 'dump', 'ray'])
->not->toBeUsed();