From 6a95653c520e49e785e248cf80af009c122a810b Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Sat, 1 Mar 2025 15:12:15 +0100 Subject: [PATCH] Added notification on job fail --- app/Browser/BrowserJob.php | 3 ++ app/Browser/JobErrorScreenshot.php | 27 ++++++++++++++ app/Notification/Notification.php | 11 +++++- .../JobErrorNotificationBody.php | 31 ++++++++++++++++ .../Notifications/JobErrorNotification.php | 37 +++++++++++++++++++ .../Providers/AllNotification.php | 3 -- .../Providers/DiscordWebHookNotification.php | 1 - 7 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 app/Browser/JobErrorScreenshot.php create mode 100644 app/Notification/NotificationBody/JobErrorNotificationBody.php create mode 100644 app/Notification/Notifications/JobErrorNotification.php diff --git a/app/Browser/BrowserJob.php b/app/Browser/BrowserJob.php index fd35779..5e40a0c 100644 --- a/app/Browser/BrowserJob.php +++ b/app/Browser/BrowserJob.php @@ -5,6 +5,8 @@ namespace App\Browser; use App\Models\Job; use App\Models\JobArtifact; use App\Models\JobRun; +use App\Notification\Notifications\JobErrorNotification; +use App\Notification\Providers\AllNotification; use Closure; use Exception; use Facebook\WebDriver\Chrome\ChromeOptions; @@ -57,6 +59,7 @@ abstract class BrowserJob implements ShouldQueue } catch (Throwable $e) { $browser->screenshot("failure-{$this->jobId}"); + AllNotification::send(new JobErrorNotification($this->jobId, $e->getMessage())); dump($e); throw $e; } finally { diff --git a/app/Browser/JobErrorScreenshot.php b/app/Browser/JobErrorScreenshot.php new file mode 100644 index 0000000..22d455f --- /dev/null +++ b/app/Browser/JobErrorScreenshot.php @@ -0,0 +1,27 @@ +job = Job::find($jobId); - $this->body = $body; + if (!isNull($body)) { + $this->body = $body; + } $this->isError = $isError; } + public function setBody(NotificationBody $body) { + $this->body = $body; + } + public function getTitle(): Stringifiable { return new StringifiableSimpleText($this->job->name); } diff --git a/app/Notification/NotificationBody/JobErrorNotificationBody.php b/app/Notification/NotificationBody/JobErrorNotificationBody.php new file mode 100644 index 0000000..c5427f6 --- /dev/null +++ b/app/Notification/NotificationBody/JobErrorNotificationBody.php @@ -0,0 +1,31 @@ +job = $job; + $this->error = $error; + } + /** + * @inheritDoc + */ + public function toMarkdownString(): string { + return "Le job \"{$this->job->name}\" a échoué avec l'erreur :\n ```" . $this->error . "``` \nScreenshot : "; + } + + /** + * @inheritDoc + */ + public function toString(): string { + return "Le job \"{$this->job->name}\" a échoué avec l'erreur :\n " . $this->error; + } +} diff --git a/app/Notification/Notifications/JobErrorNotification.php b/app/Notification/Notifications/JobErrorNotification.php new file mode 100644 index 0000000..d52ace2 --- /dev/null +++ b/app/Notification/Notifications/JobErrorNotification.php @@ -0,0 +1,37 @@ +setBody(new JobErrorNotificationBody($this->job, $error)); + } + + public function getTitle(): Stringifiable { + return new StringifiableSimpleText("Le job {$this->job->name} a échoué"); + } + + /** + * @inheritDoc + */ + public function getImageProjectPath(): string|null { + return JobErrorScreenshot::getImgFileProjectPath($this->job->id); + } + + /** + * @inheritDoc + */ + public function getLinkURL(): string|null { + return route('jobs.show', ['job' => $this->job->id]); + } +} diff --git a/app/Notification/Providers/AllNotification.php b/app/Notification/Providers/AllNotification.php index c9d15cc..05bb225 100644 --- a/app/Notification/Providers/AllNotification.php +++ b/app/Notification/Providers/AllNotification.php @@ -3,9 +3,6 @@ namespace App\Notification\Providers; use App\Notification\NotificationProvider; -use App\Notification\INotificationProvider; -use App\Models\JobInfo; -use Illuminate\Support\Facades\Cache; class AllNotification extends NotificationProvider { private const NOTIFICATIONS_PROVIDERS = [ diff --git a/app/Notification/Providers/DiscordWebHookNotification.php b/app/Notification/Providers/DiscordWebHookNotification.php index 8e504a9..d03015e 100644 --- a/app/Notification/Providers/DiscordWebHookNotification.php +++ b/app/Notification/Providers/DiscordWebHookNotification.php @@ -3,7 +3,6 @@ namespace App\Notification\Providers; use App\Notification\NotificationProvider; -use App\Notification\INotificationProvider; use App\Models\JobInfo; use Illuminate\Support\Facades\Cache;