Skip to content

Commit

Permalink
fix: fixed backup failure email not being sent
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 5, 2024
1 parent 7c17704 commit 924b670
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
8 changes: 0 additions & 8 deletions app/Services/Backup/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,6 @@ public function sendEmailNotificationOfTaskFailure(BackupTask $backupTask, strin
}
}

public function handleFailure(BackupTask $backupTask, string &$logOutput, string $errorMessage): void
{
$this->logError('Handling failure for backup task.', ['backup_task_id' => $backupTask->getAttribute('id'), 'error' => $errorMessage]);

$logOutput .= "\n" . $errorMessage;
$this->sendEmailNotificationOfTaskFailure($backupTask, $errorMessage);
}

/**
* @throws SFTPConnectionException
*/
Expand Down
1 change: 1 addition & 0 deletions app/Services/Backup/Tasks/AbstractBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected function finalizeSuccessfulBackup(): void
protected function handleBackupFailure(Exception $exception): void
{
$this->logOutput .= 'Error in backup process: ' . $exception->getMessage() . "\n";
$this->sendEmailNotificationOfTaskFailure($this->backupTask, $exception->getMessage());
Log::error("Error in backup process for task {$this->backupTask->id}: " . $exception->getMessage(), ['exception' => $exception]);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Services/Backup/Tasks/AbstractBackupTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Tests\Unit\Services\Backup\Tasks;

use App\Mail\BackupTaskFailed;
use App\Models\BackupTask;
use App\Models\BackupTaskLog;
use App\Services\Backup\Contracts\SFTPInterface;
use App\Services\Backup\Tasks\AbstractBackupTask;
use Exception;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Mail;
use Mockery;
use ReflectionClass;

Expand Down Expand Up @@ -102,11 +104,14 @@ protected function performBackup(): void
});

it('handles backup failure', function (): void {
Mail::fake();
$method = $this->reflection->getMethod('handleBackupFailure');

$exception = new Exception('Test exception');
$method->invoke($this->abstractBackupTask, $exception);

Mail::assertQueued(BackupTaskFailed::class);

expect($this->abstractBackupTask->getLogOutput())
->toContain('Error in backup process: Test exception');
});
Expand Down

0 comments on commit 924b670

Please sign in to comment.