Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Refactoring: extract process handling from async job.

Joao Patricio29fb40bd160f
diff --git a/app/Actions/RemoteProcess/DispatchRemoteProcess.php b/app/Actions/RemoteProcess/DispatchRemoteProcess.php
index 09dc2348e2..c907c7a7de 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 @@ class DispatchRemoteProcess
return $this->activity;
}
-
}
diff --git a/app/Actions/RemoteProcess/RunRemoteProcess.php b/app/Actions/RemoteProcess/RunRemoteProcess.php
index b0694f53d0..69dc354657 100644
--- a/app/Actions/RemoteProcess/RunRemoteProcess.php
+++ b/app/Actions/RemoteProcess/RunRemoteProcess.php
@@ -7,7 +7,7 @@ use App\Enums\ProcessStatus;
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 @@ class RunRemoteProcess
. '-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 <host and key type> 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 a94e461fb6..65ed0e6451 100644
--- a/app/Data/RemoteProcessArgs.php
+++ b/app/Data/RemoteProcessArgs.php
@@ -9,11 +9,11 @@ use Spatie\LaravelData\Data;
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 fdb460361c..9cdcaa809a 100755
--- a/app/Jobs/ExecuteCoolifyProcess.php
+++ b/app/Jobs/ExecuteCoolifyProcess.php
@@ -8,7 +8,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
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 966206c439..c7517b0db2 100644
--- a/bootstrap/helpers.php
+++ b/bootstrap/helpers.php
@@ -1,19 +1,29 @@
<?php
use App\Actions\RemoteProcess\DispatchRemoteProcess;
+use App\Data\RemoteProcessArgs;
use Spatie\Activitylog\Contracts\Activity;
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 asynchronously into a machine to run the command(s).
+ * @TODO Change 'root' to 'coolify' when it's able to run Docker commands without sudo
*
*/
- function remoteProcess($command, $destination): Activity
+ function remoteProcess(
+ string $command,
+ string $destination,
+ ?int $port = 22,
+ ?string $user = 'root',
+ ): Activity
{
return resolve(DispatchRemoteProcess::class, [
- 'destination' => $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 918025525a..8fc551a04e 100644
--- a/tests/Feature/DockerCommandsTest.php
+++ b/tests/Feature/DockerCommandsTest.php
@@ -14,12 +14,11 @@ it('starts a docker container correctly', function () {
// 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