Skip to content

Commit 5041332

Browse files
committed
Improve logic, made it simpler
1 parent d816863 commit 5041332

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

app/Jobs/DatabaseBackupJob.php

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ private function upload_to_s3(): void
495495
$network = $this->database->destination->network;
496496
}
497497

498-
// Ensure helper image is available before using it
499498
$this->ensureHelperImageAvailable();
500499

501500
$settings = InstanceSettings::get();
@@ -508,7 +507,6 @@ private function upload_to_s3(): void
508507
$commands[] = "docker exec backup-of-{$this->backup->uuid} mc cp $this->backup_location temporary/$bucket{$this->backup_dir}/";
509508
instant_remote_process($commands, $this->server);
510509
$this->add_to_backup_output('Uploaded to S3.');
511-
ray('Uploaded to S3. '.$this->backup_location.' to s3://'.$bucket.$this->backup_dir);
512510
} catch (\Throwable $e) {
513511
$this->add_to_backup_output($e->getMessage());
514512
throw $e;
@@ -521,38 +519,33 @@ private function upload_to_s3(): void
521519
private function ensureHelperImageAvailable(): void
522520
{
523521
$settings = InstanceSettings::get();
524-
ray($settings);
525-
526522
$helperImage = config('coolify.helper_image');
527-
ray('Helper Image:', $helperImage);
528-
529-
$helperImageTag = $settings->helper_version;
530-
ray('Helper Image Tag:', $helperImageTag);
531-
532-
$fullImageName = "{$helperImage}:{$helperImageTag}";
533-
ray('Full Image Name:', $fullImageName);
534-
535-
$imageExists = instant_remote_process(["docker image inspect {$fullImageName}"], $this->server, false);
536-
ray('Image Exists:', $imageExists);
537-
538-
if (empty($imageExists)) {
539-
$this->add_to_backup_output("Helper image not found. Pulling {$fullImageName}.");
540-
ray('Helper image not found. Attempting to pull.');
541-
try {
542-
$pullResult = instant_remote_process(["docker pull {$fullImageName}"], $this->server);
543-
ray('Pull Result:', $pullResult);
544-
$this->add_to_backup_output("Helper image pulled successfully.");
545-
ray('Helper image pulled successfully.');
546-
} catch (\Exception $e) {
547-
$errorMessage = "Failed to pull helper image: " . $e->getMessage();
548-
ray('Error:', $errorMessage);
549-
$this->add_to_backup_output($errorMessage);
550-
throw new \RuntimeException($errorMessage);
551-
}
523+
$latestVersion = $settings->helper_version;
524+
$fullImageName = "{$helperImage}:{$latestVersion}";
525+
526+
$imageExists = $this->checkImageExists($fullImageName);
527+
528+
if (!$imageExists) {
529+
$this->pullHelperImage($fullImageName);
552530
} else {
553-
$message = "Helper image {$fullImageName} is available.";
554-
ray($message);
555-
$this->add_to_backup_output($message);
531+
return;
532+
}
533+
}
534+
535+
private function checkImageExists(string $fullImageName): bool
536+
{
537+
$result = instant_remote_process(["docker image inspect {$fullImageName} >/dev/null 2>&1 && echo 'exists' || echo 'not exists'"], $this->server, false);
538+
return trim($result) === 'exists';
539+
}
540+
541+
private function pullHelperImage(string $fullImageName): void
542+
{
543+
try {
544+
instant_remote_process(["docker pull {$fullImageName}"], $this->server);
545+
} catch (\Exception $e) {
546+
$errorMessage = "Failed to pull helper image: " . $e->getMessage();
547+
$this->add_to_backup_output($errorMessage);
548+
throw new \RuntimeException($errorMessage);
556549
}
557550
}
558551
}

0 commit comments

Comments
 (0)