From fec63ff2494b8740f5b4cfa284cb038b2000c5ef Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Sun, 2 Mar 2025 10:03:27 +0100 Subject: [PATCH] Added Debug notifications --- app/Browser/BrowserJob.php | 2 +- app/Browser/JobDebugScreenshot.php | 27 ++++++++++ app/Browser/JobErrorScreenshot.php | 2 +- app/Browser/Jobs/Hellcase/HellcaseJob.php | 7 ++- .../JobDebugNotificationBody.php | 53 +++++++++++++++++++ .../Notifications/JobDebugNotification.php | 40 ++++++++++++++ 6 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 app/Browser/JobDebugScreenshot.php create mode 100644 app/Notification/NotificationBody/JobDebugNotificationBody.php create mode 100644 app/Notification/Notifications/JobDebugNotification.php diff --git a/app/Browser/BrowserJob.php b/app/Browser/BrowserJob.php index 5e40a0c..a1994b4 100644 --- a/app/Browser/BrowserJob.php +++ b/app/Browser/BrowserJob.php @@ -58,7 +58,7 @@ abstract class BrowserJob implements ShouldQueue // throw $e; } catch (Throwable $e) { - $browser->screenshot("failure-{$this->jobId}"); + $browser->screenshot(JobErrorScreenshot::getFileName($this->jobId)); AllNotification::send(new JobErrorNotification($this->jobId, $e->getMessage())); dump($e); throw $e; diff --git a/app/Browser/JobDebugScreenshot.php b/app/Browser/JobDebugScreenshot.php new file mode 100644 index 0000000..cc28557 --- /dev/null +++ b/app/Browser/JobDebugScreenshot.php @@ -0,0 +1,27 @@ +driver->findElement(WebDriverBy::xpath('//div[./*[contains(text(), "Or sign in with QR")]]')); } catch (\Exception $e) { - AllNotification::send(new JobErrorNotification($this->jobId, "Le QR code de la page de connexion de Steam n'a pas été trouvé")); + $browser->takeScreenshot(JobDebugScreenshot::getFileName($this->jobId)); + AllNotification::send(new JobDebugNotification($this->jobId, "Le QR code de la page de connexion de Steam n'a pas été trouvé")); throw $e; } @@ -206,6 +209,8 @@ class HellcaseJob extends BrowserJob implements ShouldBeUniqueUntilProcessing $hours = $availibleInButton->getText(); // If the text is like "in 26 sec." we need to put one minute if (!str_contains($hours, "hr")) { + $browser->takeScreenshot(JobDebugScreenshot::getFileName($this->jobId)); + AllNotification::send(new JobDebugNotification($this->jobId, "I hate niggers")); // $this->reschedule(1); sleep(60); return $this->getDailyFree($browser); diff --git a/app/Notification/NotificationBody/JobDebugNotificationBody.php b/app/Notification/NotificationBody/JobDebugNotificationBody.php new file mode 100644 index 0000000..f668f69 --- /dev/null +++ b/app/Notification/NotificationBody/JobDebugNotificationBody.php @@ -0,0 +1,53 @@ +job = $job; + $this->title = $title; + $this->body = $body; + $this->error = $error; + $this->hasScreenshot = $hasScreenshot; + } + + private function constructString(bool $inMarkdown = false) { + $mdBody = ""; + if ($this->body !== null) { + $mdBody .= $this->body; + } + if ($this->error !== null) { + $errorWrapper = $inMarkdown ? "```" : ""; + $mdBody .= " :\n" . $errorWrapper . $this->error . $errorWrapper; + } + if ($inMarkdown && $this->hasScreenshot) { + $mdBody .= "\nScreenshot : "; + } + return $mdBody; + } + + /** + * @inheritDoc + */ + public function toMarkdownString(): string { + return $this->constructString(true); + } + + /** + * @inheritDoc + */ + public function toString(): string { + return $this->constructString(); + } +} diff --git a/app/Notification/Notifications/JobDebugNotification.php b/app/Notification/Notifications/JobDebugNotification.php new file mode 100644 index 0000000..dadb933 --- /dev/null +++ b/app/Notification/Notifications/JobDebugNotification.php @@ -0,0 +1,40 @@ +title = $title; + $this->screenShotProjectPath = $screenshotProjectPath; + $this->setBody(new JobDebugNotificationBody($this->job, $this->title, $body, $error, $this->screenShotProjectPath != null)); + } + + public function getTitle(): Stringifiable { + return new StringifiableSimpleText($this->title ?? "DEBUG Job {$this->job->name}"); + } + + /** + * @inheritDoc + */ + public function getImageProjectPath(): string|null { + return $this->screenShotProjectPath; + } + + /** + * @inheritDoc + */ + public function getLinkURL(): string|null { + return route('jobs.show', ['job' => $this->job->id]); + } +}