From 58fc897ea5b2ae615c32c0084db63db3ea52f2f6 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 25 Jun 2024 10:51:32 +0200 Subject: [PATCH] fix: run user commands on high prio queue --- .../CoolifyTask/PrepareCoolifyTask.php | 15 +++++++++++++-- app/Actions/CoolifyTask/RunRemoteProcess.php | 2 +- app/Actions/Server/RunCommand.php | 19 +++++++++++++++++++ app/Enums/ActivityTypes.php | 1 + app/Jobs/CoolifyTask.php | 9 +++++---- app/Livewire/RunCommand.php | 3 ++- 6 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 app/Actions/Server/RunCommand.php diff --git a/app/Actions/CoolifyTask/PrepareCoolifyTask.php b/app/Actions/CoolifyTask/PrepareCoolifyTask.php index d4cdf64e2..686b60780 100644 --- a/app/Actions/CoolifyTask/PrepareCoolifyTask.php +++ b/app/Actions/CoolifyTask/PrepareCoolifyTask.php @@ -3,6 +3,7 @@ namespace App\Actions\CoolifyTask; use App\Data\CoolifyTaskArgs; +use App\Enums\ActivityTypes; use App\Jobs\CoolifyTask; use Spatie\Activitylog\Models\Activity; @@ -40,8 +41,18 @@ public function __construct(CoolifyTaskArgs $remoteProcessArgs) public function __invoke(): Activity { - $job = new CoolifyTask($this->activity, ignore_errors: $this->remoteProcessArgs->ignore_errors, call_event_on_finish: $this->remoteProcessArgs->call_event_on_finish, call_event_data: $this->remoteProcessArgs->call_event_data); - dispatch($job); + $job = new CoolifyTask( + activity: $this->activity, + ignore_errors: $this->remoteProcessArgs->ignore_errors, + call_event_on_finish: $this->remoteProcessArgs->call_event_on_finish, + call_event_data: $this->remoteProcessArgs->call_event_data, + ); + if ($this->remoteProcessArgs->type === ActivityTypes::COMMAND->value) { + ray('Dispatching a high priority job'); + dispatch($job)->onQueue('high'); + } else { + dispatch($job); + } $this->activity->refresh(); return $this->activity; diff --git a/app/Actions/CoolifyTask/RunRemoteProcess.php b/app/Actions/CoolifyTask/RunRemoteProcess.php index be986a76f..63e3afe2f 100644 --- a/app/Actions/CoolifyTask/RunRemoteProcess.php +++ b/app/Actions/CoolifyTask/RunRemoteProcess.php @@ -39,7 +39,7 @@ class RunRemoteProcess public function __construct(Activity $activity, bool $hide_from_output = false, bool $ignore_errors = false, $call_event_on_finish = null, $call_event_data = null) { - if ($activity->getExtraProperty('type') !== ActivityTypes::INLINE->value) { + if ($activity->getExtraProperty('type') !== ActivityTypes::INLINE->value && $activity->getExtraProperty('type') !== ActivityTypes::COMMAND->value) { throw new \RuntimeException('Incompatible Activity to run a remote command.'); } diff --git a/app/Actions/Server/RunCommand.php b/app/Actions/Server/RunCommand.php new file mode 100644 index 000000000..fce862eb0 --- /dev/null +++ b/app/Actions/Server/RunCommand.php @@ -0,0 +1,19 @@ +value); + + return $activity; + } +} diff --git a/app/Enums/ActivityTypes.php b/app/Enums/ActivityTypes.php index e2536a7f0..2d23cd98b 100644 --- a/app/Enums/ActivityTypes.php +++ b/app/Enums/ActivityTypes.php @@ -5,4 +5,5 @@ enum ActivityTypes: string { case INLINE = 'inline'; + case COMMAND = 'command'; } diff --git a/app/Jobs/CoolifyTask.php b/app/Jobs/CoolifyTask.php index 5418daa22..23e39a015 100755 --- a/app/Jobs/CoolifyTask.php +++ b/app/Jobs/CoolifyTask.php @@ -20,10 +20,11 @@ class CoolifyTask implements ShouldBeEncrypted, ShouldQueue */ public function __construct( public Activity $activity, - public bool $ignore_errors = false, - public $call_event_on_finish = null, - public $call_event_data = null - ) {} + public bool $ignore_errors, + public $call_event_on_finish, + public $call_event_data, + ) { + } /** * Execute the job. diff --git a/app/Livewire/RunCommand.php b/app/Livewire/RunCommand.php index fc7f1eefc..c2d3adeea 100644 --- a/app/Livewire/RunCommand.php +++ b/app/Livewire/RunCommand.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use App\Actions\Server\RunCommand as ServerRunCommand; use App\Models\Server; use Livewire\Component; @@ -33,7 +34,7 @@ public function runCommand() { $this->validate(); try { - $activity = remote_process([$this->command], Server::where('uuid', $this->server)->first(), ignore_errors: true); + $activity = ServerRunCommand::run(server: Server::where('uuid', $this->server)->first(), command: $this->command); $this->dispatch('activityMonitor', $activity->id); } catch (\Throwable $e) { return handleError($e, $this);