1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Notifications\Notifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
6
|
|
|
use Illuminate\Notifications\Messages\SlackAttachment; |
7
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
8
|
|
|
use Spatie\Backup\Events\UnhealthyBackupWasFound as UnhealthyBackupWasFoundEvent; |
9
|
|
|
use Spatie\Backup\Notifications\BaseNotification; |
10
|
|
|
use Spatie\Backup\Tasks\Monitor\HealthCheckFailure; |
11
|
|
|
|
12
|
|
|
class UnhealthyBackupWasFound extends BaseNotification |
13
|
|
|
{ |
14
|
|
|
/** @var \Spatie\Backup\Events\UnhealthyBackupWasFound */ |
15
|
|
|
protected $event; |
16
|
|
|
|
17
|
|
|
public function toMail(): MailMessage |
18
|
|
|
{ |
19
|
|
|
$mailMessage = (new MailMessage) |
20
|
|
|
->error() |
21
|
|
|
->from(config('backup.notifications.mail.from.address', config('mail.from.address')), config('backup.notifications.mail.from.name', config('mail.from.name'))) |
22
|
|
|
->subject(trans('backup::notifications.unhealthy_backup_found_subject', ['application_name' => $this->applicationName()])) |
23
|
|
|
->line(trans('backup::notifications.unhealthy_backup_found_body', ['application_name' => $this->applicationName(), 'disk_name' => $this->diskName()])) |
24
|
|
|
->line($this->problemDescription()); |
25
|
|
|
|
26
|
|
|
$this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) { |
27
|
|
|
$mailMessage->line("{$name}: $value"); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
if ($this->failure()->wasUnexpected()) { |
31
|
|
|
$mailMessage |
32
|
|
|
->line('Health check: '.$this->failure()->healthCheck()->name()) |
33
|
|
|
->line(trans('backup::notifications.exception_message', ['message' => $this->failure()->exception()->getMessage()])) |
34
|
|
|
->line(trans('backup::notifications.exception_trace', ['trace' => $this->failure()->exception()->getTraceAsString()])); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $mailMessage; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function toSlack(): SlackMessage |
41
|
|
|
{ |
42
|
|
|
$slackMessage = (new SlackMessage) |
43
|
|
|
->error() |
44
|
|
|
->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon')) |
45
|
|
|
->to(config('backup.notifications.slack.channel')) |
46
|
|
|
->content(trans('backup::notifications.unhealthy_backup_found_subject_title', ['application_name' => $this->applicationName(), 'problem' => $this->problemDescription()])) |
47
|
|
|
->attachment(function (SlackAttachment $attachment) { |
48
|
|
|
$attachment->fields($this->backupDestinationProperties()->toArray()); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
if ($this->failure()->wasUnexpected()) { |
52
|
|
|
$slackMessage |
53
|
|
|
->attachment(function (SlackAttachment $attachment) { |
54
|
|
|
$attachment |
55
|
|
|
->title('Health check') |
56
|
|
|
->content($this->failure()->healthCheck()->name()); |
57
|
|
|
}) |
58
|
|
|
->attachment(function (SlackAttachment $attachment) { |
59
|
|
|
$attachment |
60
|
|
|
->title(trans('backup::notifications.exception_message_title')) |
61
|
|
|
->content($this->failure()->exception()->getMessage()); |
62
|
|
|
}) |
63
|
|
|
->attachment(function (SlackAttachment $attachment) { |
64
|
|
|
$attachment |
65
|
|
|
->title(trans('backup::notifications.exception_trace_title')) |
66
|
|
|
->content($this->failure()->exception()->getTraceAsString()); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $slackMessage; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function problemDescription(): string |
74
|
|
|
{ |
75
|
|
|
if ($this->failure()->wasUnexpected()) { |
76
|
|
|
return trans('backup::notifications.unhealthy_backup_found_unknown'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->failure()->exception()->getMessage(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function failure(): HealthCheckFailure |
83
|
|
|
{ |
84
|
|
|
return $this->event->backupDestinationStatus->getHealthCheckFailure(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setEvent(UnhealthyBackupWasFoundEvent $event) |
88
|
|
|
{ |
89
|
|
|
$this->event = $event; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|