Added notification on job fail
All checks were successful
Push image to registry / build-image (push) Successful in 5m20s

This commit is contained in:
2025-03-01 15:12:15 +01:00
parent 025711e09d
commit 6a95653c52
7 changed files with 107 additions and 6 deletions

View File

@ -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 {

View File

@ -0,0 +1,27 @@
<?php
namespace App\Browser;
use Laravel\Dusk\Browser;
use function rtrim;
class JobErrorScreenshot {
public const IMG_FILE_NAME = "failure-";
private static function getFileName(int $jobId): string {
return static::IMG_FILE_NAME . $jobId . ".png";
}
public static function getImgFileAbsolutePath(int $jobId): string {
return rtrim(Browser::$storeScreenshotsAt, '/') . "/" . static::getFileName($jobId);
}
public static function getImgFileProjectPath(int $jobId): string {
return app_path("Browser/screenshots/" . static::getFileName($jobId));
}
public static function getImgFileExternalPath(int $jobId): string {
return "screenshots/" . static::getFileName($jobId);
}
}

View File

@ -4,6 +4,7 @@ namespace App\Notification;
use App\Models\Job;
use App\Notification\Stringifiable\StringifiableSimpleText;
use function PHPUnit\Framework\isNull;
abstract class Notification {
@ -12,12 +13,18 @@ abstract class Notification {
public bool $isError;
public function __construct(int $jobId, NotificationBody $body, bool $isError = false) {
public function __construct(int $jobId, NotificationBody $body = null, bool $isError = false) {
$this->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);
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Notification\NotificationBody;
use App\Models\Job;
use App\Notification\NotificationBody;
use App\Notification\Stringifiable;
class JobErrorNotificationBody extends NotificationBody {
private Job $job;
private string $error;
public function __construct(Job $job, $error) {
$this->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;
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Notification\Notifications;
use App\Browser\JobErrorScreenshot;
use App\Models\Job;
use App\Notification\Notification;
use App\Notification\NotificationBody\JobErrorNotificationBody;
use App\Notification\Stringifiable;
use App\Notification\Stringifiable\StringifiableSimpleText;
use Illuminate\Support\Facades\Log;
class JobErrorNotification extends Notification {
public function __construct(int $jobId, string $error) {
parent::__construct($jobId, isError:true);
$this->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]);
}
}

View File

@ -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 = [

View File

@ -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;