coolify/app/Notifications/ScheduledTask/TaskFailed.php
Andras Bacsai 0303f529d3 feat: add UUIDs and URLs to webhook notifications
- Add resource UUIDs (application_uuid, database_uuid, server_uuid, task_uuid) to all webhook notifications
- Standardize URL field naming from various formats (resource_url, task_url, server_url) to consistent 'url' field
- Include parent resource UUIDs for scheduled tasks (application_uuid or service_uuid)
- Add direct URLs to Coolify resources for all notification types
- Update UI to show "Webhook URL (POST)" label for clarity

This enables webhook consumers to:
- Uniquely identify resources using UUIDs used throughout Coolify UI
- Directly link back to Coolify resource pages via the url field

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-10 18:41:46 +02:00

142 lines
3.8 KiB
PHP

<?php
namespace App\Notifications\ScheduledTask;
use App\Models\ScheduledTask;
use App\Notifications\CustomEmailNotification;
use App\Notifications\Dto\DiscordMessage;
use App\Notifications\Dto\PushoverMessage;
use App\Notifications\Dto\SlackMessage;
use Illuminate\Notifications\Messages\MailMessage;
class TaskFailed extends CustomEmailNotification
{
public ?string $url = null;
public function __construct(public ScheduledTask $task, public string $output)
{
$this->onQueue('high');
if ($task->application) {
$this->url = $task->application->taskLink($task->uuid);
} elseif ($task->service) {
$this->url = $task->service->taskLink($task->uuid);
}
}
public function via(object $notifiable): array
{
return $notifiable->getEnabledChannels('scheduled_task_failure');
}
public function toMail(): MailMessage
{
$mail = new MailMessage;
$mail->subject("Coolify: [ACTION REQUIRED] Scheduled task ({$this->task->name}) failed.");
$mail->view('emails.scheduled-task-failed', [
'task' => $this->task,
'url' => $this->url,
'output' => $this->output,
]);
return $mail;
}
public function toDiscord(): DiscordMessage
{
$message = new DiscordMessage(
title: ':cross_mark: Scheduled task failed',
description: "Scheduled task ({$this->task->name}) failed.",
color: DiscordMessage::errorColor(),
);
if ($this->url) {
$message->addField('Scheduled task', '[Link]('.$this->url.')');
}
return $message;
}
public function toTelegram(): array
{
$message = "Coolify: Scheduled task ({$this->task->name}) failed with output: {$this->output}";
if ($this->url) {
$buttons[] = [
'text' => 'Open task in Coolify',
'url' => (string) $this->url,
];
}
return [
'message' => $message,
];
}
public function toPushover(): PushoverMessage
{
$message = "Scheduled task ({$this->task->name}) failed<br/>";
if ($this->output) {
$message .= "<br/><b>Error Output:</b>{$this->output}";
}
$buttons = [];
if ($this->url) {
$buttons[] = [
'text' => 'Open task in Coolify',
'url' => (string) $this->url,
];
}
return new PushoverMessage(
title: 'Scheduled task failed',
level: 'error',
message: $message,
buttons: $buttons,
);
}
public function toSlack(): SlackMessage
{
$title = 'Scheduled task failed';
$description = "Scheduled task ({$this->task->name}) failed.";
if ($this->output) {
$description .= "\n\n*Error Output:* {$this->output}";
}
if ($this->url) {
$description .= "\n\n*Task URL:* {$this->url}";
}
return new SlackMessage(
title: $title,
description: $description,
color: SlackMessage::errorColor()
);
}
public function toWebhook(): array
{
$data = [
'success' => false,
'message' => 'Scheduled task failed',
'event' => 'task_failed',
'task_name' => $this->task->name,
'task_uuid' => $this->task->uuid,
'output' => $this->output,
];
if ($this->task->application) {
$data['application_uuid'] = $this->task->application->uuid;
} elseif ($this->task->service) {
$data['service_uuid'] = $this->task->service->uuid;
}
if ($this->url) {
$data['url'] = $this->url;
}
return $data;
}
}