Added job artifacts, dealing with cancelled battle & fixed and improved notification
All checks were successful
Push image to registry / build-image (push) Successful in 4m29s

This commit is contained in:
2025-03-20 19:16:00 +01:00
parent c5f5d94912
commit fadb4d2748
4 changed files with 133 additions and 6 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace App\Browser\Jobs\HellcaseBattles;
use Laravel\Dusk\Browser;
use function rtrim;
class HellcaseBattleScreenshot {
public const IMG_FILE_NAME = "Hellcase-battle";
public static function getImgFileAbsolutePath(): string {
return rtrim(Browser::$storeScreenshotsAt, '/') . "/HellcaseBattles/" . static::IMG_FILE_NAME;
}
public static function getImgFileProjectPath(): string {
return app_path("Browser/screenshots/HellcaseBattles/" . static::IMG_FILE_NAME);
}
public static function getImgFileExternalPath(): string {
return "screenshots/HellcaseBattles/" . static::IMG_FILE_NAME;
}
}

View File

@ -6,6 +6,7 @@ use App\Browser\JobDebugScreenshot;
use App\Browser\Jobs\Hellcase\HellcaseJob;
use App\Models\HellcaseBattle;
use App\Models\Job;
use App\Models\JobArtifact;
use App\Models\JobRun;
use App\Notification\Notifications\JobDebugNotification;
use App\Notification\Providers\AllNotification;
@ -21,6 +22,7 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce
{
private Collection $jobInfos;
private array $battlesToAdd = [];
private array $battlesSent = [];
public function __construct()
{
@ -48,8 +50,16 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce
$this->sendFinishedBattles($browser);
$this->jobRun->addArtifact(new JobArtifact([
"name" => count($this->battlesSent) . " battailles envoyées",
]));
$this->createNewBattles();
$this->jobRun->addArtifact(new JobArtifact([
"name" => count($this->battlesToAdd) . " nouvelles battailles ajoutées pour surveillage",
]));
$this->jobRun->success = true;
$this->jobRun->save();
@ -103,9 +113,13 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce
$battleLinkButton = $battle->findElement(WebDriverBy::xpath('./div//button[text() = "watch"]'));
$battleLinkButton->sendKeys("\n");
sleep(3);
$battleLink = $browser->driver->getCurrentURL();
try { // If we still are on the casebattle page, it means the battle was cancelled or something else
$browser->waitForLocation("https://hellcase.com/casebattle/", 3);
} catch (Exception $e) {
$battleLink = $browser->driver->getCurrentURL();
$this->battlesToAdd[$battleLink] = $battleValue;
$this->battlesToAdd[$battleLink] = $battleValue;
}
}
}
@ -115,11 +129,12 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce
if (!array_key_exists($battle->getUrl(), $this->battlesToAdd)) {
$browser->visit($battle->getUrl());
try {
$browser->waitForText("Started at");
sleep(2);
$browser->waitForText("Case Battle");
if ($this->findElementContainingText($browser, "Started at:") != null) { // battle is finished
// Send the battle
$this->sendBattle($browser, $battle);
} catch (Exception $e) { // Battle is not finished or error (like battle cancelled)
}
$battle->delete();
@ -128,7 +143,16 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce
}
private function sendBattle(Browser $browser, HellcaseBattle $battle) {
AllNotification::send(new JobDebugNotification($this->jobId, "Battle sent" . $battle->getUrl(), screenshotProjectPath:null));
try {
$battleHeader = $browser->driver->findElement(WebDriverBy::xpath("//*[contains(@class, 'case-battle-game__header')]"));
sleep(2); // Wait for the animations to finish
$battleHeader->takeElementScreenshot(HellcaseBattleScreenshot::getImgFileAbsolutePath());
} catch (Exception $e) {
$browser->screenshot(JobDebugScreenshot::getFileName($this->jobId));
AllNotification::send(new JobDebugNotification($this->jobId, "Failed to screenshot battle"));
}
$this->battlesSent[$battle->getUrl()] = $battle->value;
AllNotification::send(new HellcaseBattlesNofication($this->jobId, $battle));
}
private function createNewBattles() {

View File

@ -0,0 +1,43 @@
<?php
namespace App\Browser\Jobs\HellcaseBattles;
use App\Models\HellcaseBattle;
use App\Notification\Notification;
use App\Notification\Stringifiable;
use App\Notification\Stringifiable\StringifiableSimpleText;
class HellcaseBattlesNofication extends Notification {
private HellcaseBattle $battle;
public function __construct(int $jobId, HellcaseBattle $battle) {
parent::__construct($jobId);
$this->battle = $battle;
$this->setBody($this->generateBody());
}
private function generateBody() {
return new HellcaseBattlesNotificationBody($this->battle);
}
public function getTitle(): Stringifiable {
return new StringifiableSimpleText("Nouvelle bataille de caisses Hellcase");
}
/**
* @inheritDoc
*/
public function getImageProjectPath(): string|null {
return HellcaseBattleScreenshot::getImgFileProjectPath();
}
/**
* @inheritDoc
*/
public function getLinkURL(): string|null {
return $this->battle->getUrl();
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Browser\Jobs\HellcaseBattles;
use App\Models\HellcaseBattle;
use App\Notification\NotificationBody;
class HellcaseBattlesNotificationBody extends NotificationBody {
private HellcaseBattle $battle;
public function __construct(HellcaseBattle $battle) {
parent::__construct();
$this->battle = $battle;
}
/**
* @inheritDoc
*/
public function toMarkdownString(): string {
return "
- Valeur : **{$this->battle->value} €**
- Lien : **{$this->battle->getUrl()}**
";
}
/**
* @inheritDoc
*/
public function toString(): string {
return "
- Valeur : {$this->battle->value}
- Lien : {$this->battle->getUrl()}
";
}
}