Refactoring: extract process handling from async job.

This commit is contained in:
Joao Patricio 2023-03-21 09:31:16 +00:00
parent be351232d3
commit d0d33da493
5 changed files with 15 additions and 18 deletions

View File

@ -23,21 +23,21 @@ public function runCommand()
{ {
$this->isKeepAliveOn = true; $this->isKeepAliveOn = true;
$this->activity = coolifyProcess($this->command, 'testing-host'); $this->activity = remoteProcess($this->command, 'testing-host');
} }
public function runSleepingBeauty() public function runSleepingBeauty()
{ {
$this->isKeepAliveOn = true; $this->isKeepAliveOn = true;
$this->activity = coolifyProcess('x=1; while [ $x -le 40 ]; do sleep 0.1 && echo "Welcome $x times" $(( x++ )); done', 'testing-host'); $this->activity = remoteProcess('x=1; while [ $x -le 40 ]; do sleep 0.1 && echo "Welcome $x times" $(( x++ )); done', 'testing-host');
} }
public function runDummyProjectBuild() public function runDummyProjectBuild()
{ {
$this->isKeepAliveOn = true; $this->isKeepAliveOn = true;
$this->activity = coolifyProcess(<<<EOT $this->activity = remoteProcess(<<<EOT
cd projects/dummy-project cd projects/dummy-project
~/.docker/cli-plugins/docker-compose build --no-cache ~/.docker/cli-plugins/docker-compose build --no-cache
EOT, 'testing-host'); EOT, 'testing-host');

View File

@ -7,7 +7,6 @@
use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Process\ProcessResult;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Spatie\Activitylog\Contracts\Activity; use Spatie\Activitylog\Contracts\Activity;

View File

@ -3,7 +3,6 @@
namespace App\Services; namespace App\Services;
use App\Jobs\ExecuteCoolifyProcess; use App\Jobs\ExecuteCoolifyProcess;
use Illuminate\Process\ProcessResult;
use Spatie\Activitylog\Contracts\Activity; use Spatie\Activitylog\Contracts\Activity;
class CoolifyProcess class CoolifyProcess
@ -36,8 +35,11 @@ public function __invoke(): Activity
dispatch($job); dispatch($job);
$this->activity->refresh();
ray($this->activity->id);
return $this->activity; return $this->activity;
} }
} }

View File

@ -3,21 +3,17 @@
use App\Services\CoolifyProcess; use App\Services\CoolifyProcess;
use Spatie\Activitylog\Contracts\Activity; use Spatie\Activitylog\Contracts\Activity;
if (! function_exists('coolifyProcess')) { if (! function_exists('remoteProcess')) {
/** /**
* Run a Coolify Process, which SSH's into a machine to run the command(s). * Run a Coolify Process, which SSH's into a machine to run the command(s).
* *
*/ */
function coolifyProcess($command, $destination): Activity function remoteProcess($command, $destination): Activity
{ {
$process = resolve(CoolifyProcess::class, [ return resolve(CoolifyProcess::class, [
'destination' => $destination, 'destination' => $destination,
'command' => $command, 'command' => $command,
]); ])();
$activityLog = $process();
return $activityLog;
} }
} }

View File

@ -13,21 +13,21 @@
$host = 'testing-host'; $host = 'testing-host';
// Assert there's no containers start with coolify_test_* // Assert there's no containers start with coolify_test_*
$activity = coolifyProcess($areThereCoolifyTestContainers, $host); $activity = remoteProcess($areThereCoolifyTestContainers, $host);
ray($activity); ray($activity);
$containers = Output::containerList($activity->getExtraProperty('stdout')); $containers = Output::containerList($activity->getExtraProperty('stdout'));
expect($containers)->toBeEmpty(); expect($containers)->toBeEmpty();
// start a container nginx -d --name = $containerName // start a container nginx -d --name = $containerName
$activity = coolifyProcess("docker run -d --name {$containerName} nginx", $host); $activity = remoteProcess("docker run -d --name {$containerName} nginx", $host);
expect($activity->getExtraProperty('exitCode'))->toBe(0); expect($activity->getExtraProperty('exitCode'))->toBe(0);
// docker ps name = $container // docker ps name = $container
$activity = coolifyProcess($areThereCoolifyTestContainers, $host); $activity = remoteProcess($areThereCoolifyTestContainers, $host);
$containers = Output::containerList($activity->getExtraProperty('stdout')); $containers = Output::containerList($activity->getExtraProperty('stdout'));
expect($containers->where('Names', $containerName)->count())->toBe(1); expect($containers->where('Names', $containerName)->count())->toBe(1);
// Stop testing containers // Stop testing containers
$activity = coolifyProcess("docker stop $(docker ps --filter='name={$coolifyNamePrefix}*' -q)", $host); $activity = remoteProcess("docker stop $(docker ps --filter='name={$coolifyNamePrefix}*' -q)", $host);
expect($activity->getExtraProperty('exitCode'))->toBe(0); expect($activity->getExtraProperty('exitCode'))->toBe(0);
}); });