2023-08-10 19:00:02 +00:00
< ? php
namespace App\Notifications\Database ;
use App\Models\ScheduledDatabaseBackup ;
2024-11-26 09:19:05 +00:00
use App\Notifications\CustomEmailNotification ;
2024-10-01 19:46:56 +00:00
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
2024-11-26 09:19:05 +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
2024-04-29 07:38:45 +00:00
public function __construct ( ScheduledDatabaseBackup $backup , public $database , public $output , public $database_name )
2023-08-10 19:00:02 +00:00
{
2024-11-22 10:16:01 +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
{
2024-07-24 12:27:21 +00:00
$mail = new MailMessage ;
2024-12-09 15:57:15 +00:00
$mail -> subject ( " Coolify: [ACTION REQUIRED] Database Backup FAILED for { $this -> database -> name } " );
2023-09-01 13:52:18 +00:00
$mail -> view ( 'emails.backup-failed' , [
'name' => $this -> name ,
2024-04-25 10:09:46 +00:00
'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
2023-08-10 19:00:02 +00:00
return $mail ;
}
2024-10-01 19:46:56 +00:00
public function toDiscord () : DiscordMessage
2023-08-10 19:00:02 +00:00
{
2024-10-01 19:46:56 +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. " ,
2024-10-01 19:46:56 +00:00
color : DiscordMessage :: errorColor (),
isCritical : true ,
);
2024-10-21 20:40:43 +00:00
$message -> addField ( 'Frequency' , $this -> frequency , true );
2024-10-01 19:46:56 +00:00
$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 \n Reason: \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:** \n { $this -> output } " ;
return new SlackMessage (
title : $title ,
description : $description ,
color : SlackMessage :: errorColor ()
);
}
2023-08-10 19:00:02 +00:00
}