From 99eea783aea32ed84ee5d118b7d7db5530cc9ffc Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:45:47 +0100 Subject: [PATCH] feat: scheduled task success and failure notifications --- app/Jobs/ScheduledTaskJob.php | 3 +++ app/Models/Application.php | 2 +- app/Models/Service.php | 2 +- app/Notifications/ScheduledTask/TaskFailed.php | 4 ++-- app/Notifications/ScheduledTask/TaskSuccess.php | 10 +++++----- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/Jobs/ScheduledTaskJob.php b/app/Jobs/ScheduledTaskJob.php index 4c20a4382..90a10f3e9 100644 --- a/app/Jobs/ScheduledTaskJob.php +++ b/app/Jobs/ScheduledTaskJob.php @@ -10,6 +10,7 @@ use App\Models\Server; use App\Models\Service; use App\Models\Team; use App\Notifications\ScheduledTask\TaskFailed; +use App\Notifications\ScheduledTask\TaskSuccess; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -111,6 +112,8 @@ class ScheduledTaskJob implements ShouldQueue 'message' => $this->task_output, ]); + $this->team?->notify(new TaskSuccess($this->task, $this->task_output)); + return; } } diff --git a/app/Models/Application.php b/app/Models/Application.php index d1efd3f33..82ad0c2d1 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -327,7 +327,7 @@ class Application extends BaseModel return null; } - public function failedTaskLink($task_uuid) + public function taskLink($task_uuid) { if (data_get($this, 'environment.project.uuid')) { $route = route('project.application.scheduled-tasks', [ diff --git a/app/Models/Service.php b/app/Models/Service.php index 6d3d2024b..117677d53 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -1140,7 +1140,7 @@ class Service extends BaseModel return null; } - public function failedTaskLink($task_uuid) + public function taskLink($task_uuid) { if (data_get($this, 'environment.project.uuid')) { $route = route('project.service.scheduled-tasks', [ diff --git a/app/Notifications/ScheduledTask/TaskFailed.php b/app/Notifications/ScheduledTask/TaskFailed.php index 878eae5bc..56c410ecb 100644 --- a/app/Notifications/ScheduledTask/TaskFailed.php +++ b/app/Notifications/ScheduledTask/TaskFailed.php @@ -16,9 +16,9 @@ class TaskFailed extends CustomEmailNotification { $this->onQueue('high'); if ($task->application) { - $this->url = $task->application->failedTaskLink($task->uuid); + $this->url = $task->application->taskLink($task->uuid); } elseif ($task->service) { - $this->url = $task->service->failedTaskLink($task->uuid); + $this->url = $task->service->taskLink($task->uuid); } } diff --git a/app/Notifications/ScheduledTask/TaskSuccess.php b/app/Notifications/ScheduledTask/TaskSuccess.php index aaf9cf14b..fc79aea37 100644 --- a/app/Notifications/ScheduledTask/TaskSuccess.php +++ b/app/Notifications/ScheduledTask/TaskSuccess.php @@ -8,7 +8,7 @@ use App\Notifications\Dto\DiscordMessage; use App\Notifications\Dto\SlackMessage; use Illuminate\Notifications\Messages\MailMessage; -class TaskFailed extends CustomEmailNotification +class TaskSuccess extends CustomEmailNotification { public ?string $url = null; @@ -16,9 +16,9 @@ class TaskFailed extends CustomEmailNotification { $this->onQueue('high'); if ($task->application) { - $this->url = $task->application->failedTaskLink($task->uuid); + $this->url = $task->application->taskLink($task->uuid); } elseif ($task->service) { - $this->url = $task->service->failedTaskLink($task->uuid); + $this->url = $task->service->taskLink($task->uuid); } } @@ -31,7 +31,7 @@ class TaskFailed extends CustomEmailNotification { $mail = new MailMessage; $mail->subject("Coolify: Scheduled task ({$this->task->name}) succeeded."); - $mail->view('emails.scheduled-task-failed', [ + $mail->view('emails.scheduled-task-success', [ 'task' => $this->task, 'url' => $this->url, 'output' => $this->output, @@ -43,7 +43,7 @@ class TaskFailed extends CustomEmailNotification public function toDiscord(): DiscordMessage { $message = new DiscordMessage( - title: ':check_mark: Scheduled task succeeded', + title: ':white_check_mark: Scheduled task succeeded', description: "Scheduled task ({$this->task->name}) succeeded.", color: DiscordMessage::successColor(), );