Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a retry feature to the backup and clean commands #1684

Merged
merged 9 commits into from
Aug 9, 2023
Merged

Add a retry feature to the backup and clean commands #1684

merged 9 commits into from
Aug 9, 2023

Conversation

prestamodule
Copy link

Hello,

Due to random errors with an S3-compatible backend, I had to create a little retry mechanism for both of these operations to avoid getting spammed of failed backups/cleanups notifications when a simple retry a few seconds later would do the trick.

I believe this is what was requested in #752 as well.

Let me know if anything is missing or incomplete :-)

@@ -47,6 +51,12 @@ public function handle()
$backupJob->setFilename($this->option('filename'));
}

if ($this->option('tries')) {
$this->tries = (int)$this->option('tries');
} elseif (!empty(config('backup.backup.tries'))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike else statements. Could you refactor this to a dedicated method with early returns?

@@ -59,6 +69,15 @@ public function handle()

consoleOutput()->comment('Backup completed!');
} catch (Exception $exception) {
if ($this->tries > 1 && $this->currentTry < $this->tries) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditionals like this are not that readable. Could you refactor this to a dedicated method with a good name, that returns a boolean and uses early returns.

@@ -42,6 +52,15 @@ public function handle()

consoleOutput()->comment('Cleanup completed!');
} catch (Exception $exception) {
if ($this->tries > 1 && $this->currentTry < $this->tries) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor to a the conditional to a dedicated method.

@@ -59,6 +69,15 @@ public function handle()

consoleOutput()->comment('Backup completed!');
} catch (Exception $exception) {
if ($this->tries > 1 && $this->currentTry < $this->tries) {
if (!empty(config('backup.backup.retry_delay'))) {
sleep((int)config('backup.backup.retry_delay'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using sleep use the new sleep helper that is easier to test: https://www.amitmerchant.com/the-new-sleep-helper-in-laravel-10/

@prestamodule
Copy link
Author

Hi,

I just made some changes per your comments, let me know if you want anything else updated :-)

src/Helpers/functions.php Outdated Show resolved Hide resolved
@freekmurze
Copy link
Member

The tests are failing for this one, could you take a look

@prestamodule
Copy link
Author

I believe this is due to the fact that the Sleep helper was only introduced in Laravel 10.10, and not with the 10.0 release ?
https://github.com/laravel/framework/releases/tag/v10.10.0

@freekmurze
Copy link
Member

Feel free to update the minimum requirements, I don't mind to drop support for older versions of Laravel

@prestamodule
Copy link
Author

Hi,

Just bumped the requirements to >= 10.10.0, let me know if this is good now :-)

@prestamodule
Copy link
Author

Hey @freekmurze, is there anything else I need to add or change to this PR?
It seems the configured workflow requires your approval to run the tests following the dependency update.

@freekmurze freekmurze merged commit 70179c3 into spatie:main Aug 9, 2023
5 checks passed
@freekmurze
Copy link
Member

Thanks!

@prestamodule
Copy link
Author

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants