From 6e084db3d971f08da5bfc18b280c81c82ac2db86 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 7 Jun 2023 15:39:08 +0200 Subject: [PATCH] ignore command center errors --- .../CoolifyTask/PrepareCoolifyTask.php | 6 ++- app/Actions/CoolifyTask/RunRemoteProcess.php | 42 +++++++++---------- app/Data/CoolifyTaskArgs.php | 1 + app/Http/Livewire/RunCommand.php | 2 +- app/Jobs/CoolifyTask.php | 3 ++ bootstrap/helpers/remoteProcess.php | 2 + 6 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/Actions/CoolifyTask/PrepareCoolifyTask.php b/app/Actions/CoolifyTask/PrepareCoolifyTask.php index c67a8bcb3..263cd329e 100644 --- a/app/Actions/CoolifyTask/PrepareCoolifyTask.php +++ b/app/Actions/CoolifyTask/PrepareCoolifyTask.php @@ -14,9 +14,11 @@ class PrepareCoolifyTask { protected Activity $activity; - + protected CoolifyTaskArgs $remoteProcessArgs; public function __construct(CoolifyTaskArgs $remoteProcessArgs) { + $this->remoteProcessArgs = $remoteProcessArgs; + if ($remoteProcessArgs->model) { $properties = $remoteProcessArgs->toArray(); unset($properties['model']); @@ -36,7 +38,7 @@ public function __construct(CoolifyTaskArgs $remoteProcessArgs) public function __invoke(): Activity { - $job = new CoolifyTask($this->activity); + $job = new CoolifyTask($this->activity, ignore_errors: $this->remoteProcessArgs->ignore_errors); dispatch($job); $this->activity->refresh(); return $this->activity; diff --git a/app/Actions/CoolifyTask/RunRemoteProcess.php b/app/Actions/CoolifyTask/RunRemoteProcess.php index 45040b717..70b40b48b 100644 --- a/app/Actions/CoolifyTask/RunRemoteProcess.php +++ b/app/Actions/CoolifyTask/RunRemoteProcess.php @@ -17,26 +17,26 @@ class RunRemoteProcess { public Activity $activity; - public bool $hideFromOutput; + public bool $hide_from_output; - public bool $isFinished; + public bool $is_finished; - public bool $ignoreErrors; + public bool $ignore_errors; - protected $timeStart; + protected $time_start; - protected $currentTime; + protected $current_time; - protected $lastWriteAt = 0; + protected $last_write_at = 0; - protected $throttleIntervalMS = 500; + protected $throttle_interval_ms = 500; protected int $counter = 1; /** * Create a new job instance. */ - public function __construct(Activity $activity, bool $hideFromOutput = false, bool $isFinished = false, bool $ignoreErrors = false) + public function __construct(Activity $activity, bool $hide_from_output = false, bool $is_finished = false, bool $ignore_errors = false) { if ($activity->getExtraProperty('type') !== ActivityTypes::INLINE->value && $activity->getExtraProperty('type') !== ActivityTypes::DEPLOYMENT->value) { @@ -44,14 +44,14 @@ public function __construct(Activity $activity, bool $hideFromOutput = false, bo } $this->activity = $activity; - $this->hideFromOutput = $hideFromOutput; - $this->isFinished = $isFinished; - $this->ignoreErrors = $ignoreErrors; + $this->hide_from_output = $hide_from_output; + $this->is_finished = $is_finished; + $this->ignore_errors = $ignore_errors; } public function __invoke(): ProcessResult { - $this->timeStart = hrtime(true); + $this->time_start = hrtime(true); $status = ProcessStatus::IN_PROGRESS; @@ -60,10 +60,10 @@ public function __invoke(): ProcessResult if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) { $status = ProcessStatus::ERROR; } else { - if (($processResult->exitCode() == 0 && $this->isFinished) || $this->activity->properties->get('status') === ProcessStatus::FINISHED->value) { + if (($processResult->exitCode() == 0 && $this->is_finished) || $this->activity->properties->get('status') === ProcessStatus::FINISHED->value) { $status = ProcessStatus::FINISHED; } - if ($processResult->exitCode() != 0 && !$this->ignoreErrors) { + if ($processResult->exitCode() != 0 && !$this->ignore_errors) { $status = ProcessStatus::ERROR; } } @@ -76,7 +76,7 @@ public function __invoke(): ProcessResult ]); $this->activity->save(); - if ($processResult->exitCode() != 0 && !$this->ignoreErrors) { + if ($processResult->exitCode() != 0 && !$this->ignore_errors) { throw new \RuntimeException($processResult->errorOutput()); } @@ -105,17 +105,17 @@ protected function getCommand(): string protected function handleOutput(string $type, string $output) { - if ($this->hideFromOutput) { + if ($this->hide_from_output) { return; } - $this->currentTime = $this->elapsedTime(); + $this->current_time = $this->elapsedTime(); $this->activity->description = $this->encodeOutput($type, $output); if ($this->isAfterLastThrottle()) { // Let's write to database. DB::transaction(function () { $this->activity->save(); - $this->lastWriteAt = $this->currentTime; + $this->last_write_at = $this->current_time; }); } } @@ -165,16 +165,16 @@ public static function decodeOutput(?Activity $activity = null): string protected function isAfterLastThrottle() { // If DB was never written, then we immediately decide we have to write. - if ($this->lastWriteAt === 0) { + if ($this->last_write_at === 0) { return true; } - return ($this->currentTime - $this->throttleIntervalMS) > $this->lastWriteAt; + return ($this->current_time - $this->throttle_interval_ms) > $this->last_write_at; } protected function elapsedTime(): int { - $timeMs = (hrtime(true) - $this->timeStart) / 1_000_000; + $timeMs = (hrtime(true) - $this->time_start) / 1_000_000; return intval($timeMs); } diff --git a/app/Data/CoolifyTaskArgs.php b/app/Data/CoolifyTaskArgs.php index 2a8865b24..1b30126f0 100644 --- a/app/Data/CoolifyTaskArgs.php +++ b/app/Data/CoolifyTaskArgs.php @@ -21,6 +21,7 @@ public function __construct( public ?string $type_uuid = null, public ?Model $model = null, public string $status = ProcessStatus::QUEUED->value, + public bool $ignore_errors = false, ) { } } diff --git a/app/Http/Livewire/RunCommand.php b/app/Http/Livewire/RunCommand.php index 474e91cdc..ea5d5596e 100755 --- a/app/Http/Livewire/RunCommand.php +++ b/app/Http/Livewire/RunCommand.php @@ -26,7 +26,7 @@ public function runCommand() { try { $this->validate(); - $activity = remote_process([$this->command], Server::where('uuid', $this->server)->first()); + $activity = remote_process([$this->command], Server::where('uuid', $this->server)->first(), ignore_errors: true); $this->emit('newMonitorActivity', $activity->id); } catch (\Exception $e) { return general_error_handler($e); diff --git a/app/Jobs/CoolifyTask.php b/app/Jobs/CoolifyTask.php index df4b0e8cc..56914a216 100755 --- a/app/Jobs/CoolifyTask.php +++ b/app/Jobs/CoolifyTask.php @@ -19,6 +19,7 @@ class CoolifyTask implements ShouldQueue */ public function __construct( public Activity $activity, + public bool $ignore_errors = false, ) { } @@ -27,8 +28,10 @@ public function __construct( */ public function handle(): void { + $remote_process = resolve(RunRemoteProcess::class, [ 'activity' => $this->activity, + 'ignore_errors' => $this->ignore_errors, ]); $remote_process(); diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php index 62bb325f0..9c63aa438 100644 --- a/bootstrap/helpers/remoteProcess.php +++ b/bootstrap/helpers/remoteProcess.php @@ -21,6 +21,7 @@ function remote_process( string $type = ActivityTypes::INLINE->value, ?string $type_uuid = null, ?Model $model = null, + bool $ignore_errors = false ): Activity { $command_string = implode("\n", $command); @@ -42,6 +43,7 @@ function remote_process( type: $type, type_uuid: $type_uuid, model: $model, + ignore_errors: $ignore_errors ), ])(); }