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