coolify/app/Notifications/Database/BackupFailed.php

109 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2023-08-10 19:00:02 +00:00
<?php
namespace App\Notifications\Database;
use App\Models\ScheduledDatabaseBackup;
use App\Notifications\CustomEmailNotification;
use App\Notifications\Dto\DiscordMessage;
2024-12-11 14:54:11 +00:00
use App\Notifications\Dto\PushoverMessage;
2024-11-12 21:37:55 +00:00
use App\Notifications\Dto\SlackMessage;
2024-12-09 15:57:15 +00:00
use Illuminate\Notifications\Messages\MailMessage;
2024-06-10 20:43:34 +00:00
class BackupFailed extends CustomEmailNotification
2023-08-10 19:00:02 +00:00
{
2023-09-01 13:52:18 +00:00
public string $name;
2024-06-10 20:43:34 +00:00
2023-09-01 13:52:18 +00:00
public string $frequency;
2023-08-10 19:00:02 +00:00
public function __construct(ScheduledDatabaseBackup $backup, public $database, public $output, public $database_name)
2023-08-10 19:00:02 +00:00
{
$this->onQueue('high');
2023-09-01 13:52:18 +00:00
$this->name = $database->name;
$this->frequency = $backup->frequency;
2023-08-10 19:00:02 +00:00
}
public function via(object $notifiable): array
{
2024-12-09 15:57:15 +00:00
return $notifiable->getEnabledChannels('backup_failure');
2023-08-10 19:00:02 +00:00
}
public function toMail(): MailMessage
{
$mail = new MailMessage;
$mail->subject("Coolify: [ACTION REQUIRED] Database Backup FAILED for {$this->database->name}");
$mail->view('emails.backup-failed', [
2023-09-01 13:52:18 +00:00
'name' => $this->name,
'database_name' => $this->database_name,
2023-09-01 13:52:18 +00:00
'frequency' => $this->frequency,
'output' => $this->output,
]);
2024-06-10 20:43:34 +00:00
return $mail;
2023-08-10 19:00:02 +00:00
}
public function toDiscord(): DiscordMessage
2023-08-10 19:00:02 +00:00
{
$message = new DiscordMessage(
2024-10-21 20:40:43 +00:00
title: ':cross_mark: Database backup failed',
description: "Database backup for {$this->name} (db:{$this->database_name}) has FAILED.",
color: DiscordMessage::errorColor(),
isCritical: true,
);
$message->addField('Frequency', $this->frequency, true);
$message->addField('Output', $this->output);
return $message;
2023-08-10 19:00:02 +00:00
}
2024-06-10 20:43:34 +00:00
2023-09-06 12:31:38 +00:00
public function toTelegram(): array
{
2024-04-29 07:38:45 +00:00
$message = "Coolify: Database backup for {$this->name} (db:{$this->database_name}) with frequency of {$this->frequency} was FAILED.\n\nReason:\n{$this->output}";
2024-06-10 20:43:34 +00:00
2023-09-06 12:31:38 +00:00
return [
2024-06-10 20:43:34 +00:00
'message' => $message,
2023-09-06 12:31:38 +00:00
];
}
2024-11-12 21:37:55 +00:00
2024-12-11 14:54:11 +00:00
public function toPushover(): PushoverMessage
{
return new PushoverMessage(
title: 'Database backup failed',
level: 'error',
message: "Database backup for {$this->name} (db:{$this->database_name}) was FAILED<br/><br/><b>Frequency:</b> {$this->frequency} .<br/><b>Reason:</b> {$this->output}",
);
}
2024-11-12 21:37:55 +00:00
public function toSlack(): SlackMessage
{
2024-12-09 15:57:15 +00:00
$title = 'Database backup failed';
2024-11-12 21:37:55 +00:00
$description = "Database backup for {$this->name} (db:{$this->database_name}) has FAILED.";
$description .= "\n\n*Frequency:* {$this->frequency}";
$description .= "\n\n*Error Output:* {$this->output}";
2024-11-12 21:37:55 +00:00
return new SlackMessage(
title: $title,
description: $description,
color: SlackMessage::errorColor()
);
}
public function toWebhook(): array
{
$url = base_url().'/project/'.data_get($this->database, 'environment.project.uuid').'/environment/'.data_get($this->database, 'environment.uuid').'/database/'.$this->database->uuid;
return [
'success' => false,
'message' => 'Database backup failed',
'event' => 'backup_failed',
'database_name' => $this->name,
'database_uuid' => $this->database->uuid,
'database_type' => $this->database_name,
'frequency' => $this->frequency,
'error_output' => $this->output,
'url' => $url,
];
}
2023-08-10 19:00:02 +00:00
}