From b3b91d3f8f3e5a82deb1d95bf1f9652f4aad6c19 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 3 Jun 2025 12:45:36 -0400 Subject: [PATCH] Log job requests --- cli/src/Command/Queue.php | 8 +++++++- cli/src/Service/Login.php | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cli/src/Command/Queue.php b/cli/src/Command/Queue.php index 8dbf632..6193dff 100644 --- a/cli/src/Command/Queue.php +++ b/cli/src/Command/Queue.php @@ -3,6 +3,7 @@ namespace Incoviba\Command; use DateTimeImmutable; use DateTimeZone; +use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Log\LoggerInterface; use Symfony\Component\Console; @@ -58,7 +59,12 @@ class Queue extends Command { $uri = "/api/queue/run/{$job_id}"; $output->writeln("GET {$uri}"); - $response = $this->client->get($uri); + try { + $response = $this->client->get($uri); + } catch (ClientExceptionInterface $exception) { + $this->logger->error($exception->getMessage()); + return Console\Command\Command::FAILURE; + } $output->writeln("Response Code: {$response->getStatusCode()}"); return ((int) floor($response->getStatusCode() / 100) === 2) ? Console\Command\Command::SUCCESS : Console\Command\Command::FAILURE; diff --git a/cli/src/Service/Login.php b/cli/src/Service/Login.php index ca5beb5..32c4357 100644 --- a/cli/src/Service/Login.php +++ b/cli/src/Service/Login.php @@ -24,7 +24,9 @@ class Login 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'] ]); } catch (ClientExceptionInterface $exception) { - $this->logger->error($exception); + $this->logger->error($exception, [ + 'username' => $this->username + ]); return ''; } @@ -46,6 +48,11 @@ class Login file_put_contents($this->tokenFilename, $data['token']); return $data['token']; } + + /** + * @return string + * @throws Exception + */ public function retrieveToken(): string { if (!file_exists($this->tokenFilename)) { @@ -61,7 +68,9 @@ class Login 'headers' => ['Authorization' => "Bearer {$token}"] ]); } catch (ClientExceptionInterface $exception) { - $this->logger->error($exception); + $this->logger->error($exception, [ + 'token' => $token + ]); return false; } return $response->getStatusCode() === 200; @@ -69,18 +78,20 @@ class Login public function getKey(string $apiKey, string $separator = 'g'): string { try { - $token = $this->retrieveToken(); - if (!$this->tryToken(implode('', [md5($apiKey), $separator, $token]))) { + $savedToken = $this->retrieveToken(); + $token = implode('', [md5($apiKey), $separator, $savedToken]); + if (!$this->tryToken($token)) { throw new Exception('Token not valid'); } } catch (Exception $exception) { $this->logger->notice($exception); - $token = $this->login(); - if ($token === '') { + $savedToken = $this->login(); + if ($savedToken === '') { unlink($this->tokenFilename); return ''; } + $token = implode('', [md5($apiKey), $separator, $savedToken]); } - return implode('', [md5($apiKey), $separator, $token]); + return $token; } }