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
{
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;

View File

@ -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);
}

View File

@ -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,
) {
}
}

View File

@ -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);

View File

@ -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();

View File

@ -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
),
])();
}