ignore command center errors

This commit is contained in:
Andras Bacsai 2023-06-07 15:39:08 +02:00
parent 9c055f2149
commit 6e084db3d9
6 changed files with 32 additions and 24 deletions

View File

@ -14,9 +14,11 @@
class PrepareCoolifyTask class PrepareCoolifyTask
{ {
protected Activity $activity; protected Activity $activity;
protected CoolifyTaskArgs $remoteProcessArgs;
public function __construct(CoolifyTaskArgs $remoteProcessArgs) public function __construct(CoolifyTaskArgs $remoteProcessArgs)
{ {
$this->remoteProcessArgs = $remoteProcessArgs;
if ($remoteProcessArgs->model) { if ($remoteProcessArgs->model) {
$properties = $remoteProcessArgs->toArray(); $properties = $remoteProcessArgs->toArray();
unset($properties['model']); unset($properties['model']);
@ -36,7 +38,7 @@ public function __construct(CoolifyTaskArgs $remoteProcessArgs)
public function __invoke(): Activity public function __invoke(): Activity
{ {
$job = new CoolifyTask($this->activity); $job = new CoolifyTask($this->activity, ignore_errors: $this->remoteProcessArgs->ignore_errors);
dispatch($job); dispatch($job);
$this->activity->refresh(); $this->activity->refresh();
return $this->activity; return $this->activity;

View File

@ -17,26 +17,26 @@ class RunRemoteProcess
{ {
public Activity $activity; 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; protected int $counter = 1;
/** /**
* Create a new job instance. * 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) { 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->activity = $activity;
$this->hideFromOutput = $hideFromOutput; $this->hide_from_output = $hide_from_output;
$this->isFinished = $isFinished; $this->is_finished = $is_finished;
$this->ignoreErrors = $ignoreErrors; $this->ignore_errors = $ignore_errors;
} }
public function __invoke(): ProcessResult public function __invoke(): ProcessResult
{ {
$this->timeStart = hrtime(true); $this->time_start = hrtime(true);
$status = ProcessStatus::IN_PROGRESS; $status = ProcessStatus::IN_PROGRESS;
@ -60,10 +60,10 @@ public function __invoke(): ProcessResult
if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) { if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) {
$status = ProcessStatus::ERROR; $status = ProcessStatus::ERROR;
} else { } 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; $status = ProcessStatus::FINISHED;
} }
if ($processResult->exitCode() != 0 && !$this->ignoreErrors) { if ($processResult->exitCode() != 0 && !$this->ignore_errors) {
$status = ProcessStatus::ERROR; $status = ProcessStatus::ERROR;
} }
} }
@ -76,7 +76,7 @@ public function __invoke(): ProcessResult
]); ]);
$this->activity->save(); $this->activity->save();
if ($processResult->exitCode() != 0 && !$this->ignoreErrors) { if ($processResult->exitCode() != 0 && !$this->ignore_errors) {
throw new \RuntimeException($processResult->errorOutput()); throw new \RuntimeException($processResult->errorOutput());
} }
@ -105,17 +105,17 @@ protected function getCommand(): string
protected function handleOutput(string $type, string $output) protected function handleOutput(string $type, string $output)
{ {
if ($this->hideFromOutput) { if ($this->hide_from_output) {
return; return;
} }
$this->currentTime = $this->elapsedTime(); $this->current_time = $this->elapsedTime();
$this->activity->description = $this->encodeOutput($type, $output); $this->activity->description = $this->encodeOutput($type, $output);
if ($this->isAfterLastThrottle()) { if ($this->isAfterLastThrottle()) {
// Let's write to database. // Let's write to database.
DB::transaction(function () { DB::transaction(function () {
$this->activity->save(); $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() protected function isAfterLastThrottle()
{ {
// If DB was never written, then we immediately decide we have to write. // 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 true;
} }
return ($this->currentTime - $this->throttleIntervalMS) > $this->lastWriteAt; return ($this->current_time - $this->throttle_interval_ms) > $this->last_write_at;
} }
protected function elapsedTime(): int protected function elapsedTime(): int
{ {
$timeMs = (hrtime(true) - $this->timeStart) / 1_000_000; $timeMs = (hrtime(true) - $this->time_start) / 1_000_000;
return intval($timeMs); return intval($timeMs);
} }

View File

@ -21,6 +21,7 @@ public function __construct(
public ?string $type_uuid = null, public ?string $type_uuid = null,
public ?Model $model = null, public ?Model $model = null,
public string $status = ProcessStatus::QUEUED->value, public string $status = ProcessStatus::QUEUED->value,
public bool $ignore_errors = false,
) { ) {
} }
} }

View File

@ -26,7 +26,7 @@ public function runCommand()
{ {
try { try {
$this->validate(); $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); $this->emit('newMonitorActivity', $activity->id);
} catch (\Exception $e) { } catch (\Exception $e) {
return general_error_handler($e); return general_error_handler($e);

View File

@ -19,6 +19,7 @@ class CoolifyTask implements ShouldQueue
*/ */
public function __construct( public function __construct(
public Activity $activity, public Activity $activity,
public bool $ignore_errors = false,
) { ) {
} }
@ -27,8 +28,10 @@ public function __construct(
*/ */
public function handle(): void public function handle(): void
{ {
$remote_process = resolve(RunRemoteProcess::class, [ $remote_process = resolve(RunRemoteProcess::class, [
'activity' => $this->activity, 'activity' => $this->activity,
'ignore_errors' => $this->ignore_errors,
]); ]);
$remote_process(); $remote_process();

View File

@ -21,6 +21,7 @@ function remote_process(
string $type = ActivityTypes::INLINE->value, string $type = ActivityTypes::INLINE->value,
?string $type_uuid = null, ?string $type_uuid = null,
?Model $model = null, ?Model $model = null,
bool $ignore_errors = false
): Activity { ): Activity {
$command_string = implode("\n", $command); $command_string = implode("\n", $command);
@ -42,6 +43,7 @@ function remote_process(
type: $type, type: $type,
type_uuid: $type_uuid, type_uuid: $type_uuid,
model: $model, model: $model,
ignore_errors: $ignore_errors
), ),
])(); ])();
} }