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

Send one notification on backup failure #1707

Merged
merged 14 commits into from
Aug 30, 2023
Merged

Conversation

joshbonnick
Copy link
Contributor

@joshbonnick joshbonnick commented Aug 25, 2023

Fixes #1662

Summary

This PR addresses an issue with exception handling in BackupJob.

As both the BackupCommand command and BackupJob are handling exceptions the result is both send the failure notification when they deem the process to have failed.

With these changes BackupCommand will exclusively handle notifying the user if a failure scenario occurs. Due to the command being able to retry itself it makes the most sense to allow it to determine when it considers the process to have failed.


Before these changes the copyToBackupDestinations method of BackupJob would fire the backup failed event and pass the destination as a parameter.

$this->sendNotification(new BackupHasFailed($exception, $backupDestination));

To ensure behavior remains the same, I have added the BackupFailed exception which acts as a decorator with a public $backupDestination property which is used to set the same property in the backup failed event.

The backup destination property is set by chaining on the exception.

throw BackupFailed::from($exception)->destination($backupDestination);

The notification receives the original exception for the user to debug.

? new BackupHasFailed($exception->getPrevious(), $exception->backupDestination)

Possible Concerns

  • BackupJob still handles success notifications and informative (including error) output to the console, we would need to completely decouple the job from the command to change this behavior, which I do not believe has any benefit to the package.
  • PHPStan raised an issue with getPrevious() returning Throwable|null as BackupHasFailed event accepts Exception - to fix this an @method has been added to BackupFailed. This can be replaced with a previous(): Exception method if you prefer.

@joshbonnick joshbonnick marked this pull request as draft August 25, 2023 16:15
@joshbonnick joshbonnick marked this pull request as ready for review August 25, 2023 17:21
@freekmurze freekmurze self-requested a review August 28, 2023 07:43
@freekmurze freekmurze merged commit 6fd549b into spatie:main Aug 30, 2023
5 checks passed
@freekmurze
Copy link
Member

Thanks!

@joshbonnick joshbonnick deleted the patch-2 branch August 30, 2023 14:41
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.

I receive two email notifications whenever a backup fails
2 participants