Added notification on job fail
All checks were successful
Push image to registry / build-image (push) Successful in 5m20s
All checks were successful
Push image to registry / build-image (push) Successful in 5m20s
This commit is contained in:
@ -5,6 +5,8 @@ namespace App\Browser;
|
|||||||
use App\Models\Job;
|
use App\Models\Job;
|
||||||
use App\Models\JobArtifact;
|
use App\Models\JobArtifact;
|
||||||
use App\Models\JobRun;
|
use App\Models\JobRun;
|
||||||
|
use App\Notification\Notifications\JobErrorNotification;
|
||||||
|
use App\Notification\Providers\AllNotification;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Facebook\WebDriver\Chrome\ChromeOptions;
|
use Facebook\WebDriver\Chrome\ChromeOptions;
|
||||||
@ -57,6 +59,7 @@ abstract class BrowserJob implements ShouldQueue
|
|||||||
}
|
}
|
||||||
catch (Throwable $e) {
|
catch (Throwable $e) {
|
||||||
$browser->screenshot("failure-{$this->jobId}");
|
$browser->screenshot("failure-{$this->jobId}");
|
||||||
|
AllNotification::send(new JobErrorNotification($this->jobId, $e->getMessage()));
|
||||||
dump($e);
|
dump($e);
|
||||||
throw $e;
|
throw $e;
|
||||||
} finally {
|
} finally {
|
||||||
|
27
app/Browser/JobErrorScreenshot.php
Normal file
27
app/Browser/JobErrorScreenshot.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ namespace App\Notification;
|
|||||||
|
|
||||||
use App\Models\Job;
|
use App\Models\Job;
|
||||||
use App\Notification\Stringifiable\StringifiableSimpleText;
|
use App\Notification\Stringifiable\StringifiableSimpleText;
|
||||||
|
use function PHPUnit\Framework\isNull;
|
||||||
|
|
||||||
abstract class Notification {
|
abstract class Notification {
|
||||||
|
|
||||||
@ -12,12 +13,18 @@ abstract class Notification {
|
|||||||
|
|
||||||
public bool $isError;
|
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->job = Job::find($jobId);
|
||||||
$this->body = $body;
|
if (!isNull($body)) {
|
||||||
|
$this->body = $body;
|
||||||
|
}
|
||||||
$this->isError = $isError;
|
$this->isError = $isError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setBody(NotificationBody $body) {
|
||||||
|
$this->body = $body;
|
||||||
|
}
|
||||||
|
|
||||||
public function getTitle(): Stringifiable {
|
public function getTitle(): Stringifiable {
|
||||||
return new StringifiableSimpleText($this->job->name);
|
return new StringifiableSimpleText($this->job->name);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
37
app/Notification/Notifications/JobErrorNotification.php
Normal file
37
app/Notification/Notifications/JobErrorNotification.php
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
@ -3,9 +3,6 @@
|
|||||||
namespace App\Notification\Providers;
|
namespace App\Notification\Providers;
|
||||||
|
|
||||||
use App\Notification\NotificationProvider;
|
use App\Notification\NotificationProvider;
|
||||||
use App\Notification\INotificationProvider;
|
|
||||||
use App\Models\JobInfo;
|
|
||||||
use Illuminate\Support\Facades\Cache;
|
|
||||||
|
|
||||||
class AllNotification extends NotificationProvider {
|
class AllNotification extends NotificationProvider {
|
||||||
private const NOTIFICATIONS_PROVIDERS = [
|
private const NOTIFICATIONS_PROVIDERS = [
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
namespace App\Notification\Providers;
|
namespace App\Notification\Providers;
|
||||||
|
|
||||||
use App\Notification\NotificationProvider;
|
use App\Notification\NotificationProvider;
|
||||||
use App\Notification\INotificationProvider;
|
|
||||||
use App\Models\JobInfo;
|
use App\Models\JobInfo;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user