From e599add55e4cb7a4c3ae3b3c9414964ec66b4ae4 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Tue, 3 Dec 2019 11:58:20 +0000 Subject: [PATCH] Only cache sending of metadata, not registration too --- src/Agent.php | 67 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/Agent.php b/src/Agent.php index efb918ce..e63bf166 100644 --- a/src/Agent.php +++ b/src/Agent.php @@ -7,6 +7,7 @@ use Closure; use DateTimeImmutable; use DateTimeZone; +use Exception; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Psr\SimpleCache\CacheInterface; @@ -35,7 +36,7 @@ final class Agent implements ScoutApmAgent { - private const CACHE_KEY_REGISTRATION_COMPLETE = 'scout_registration_completed'; + private const CACHE_KEY_METADATA_SENT = 'scout_metadata_sent'; /** @var Config */ private $config; @@ -331,29 +332,18 @@ public function send() : bool } try { - if (! $this->registrationIsComplete()) { - if (! $this->connector->sendCommand(new RegisterMessage( - (string) $this->config->get(ConfigKey::APPLICATION_NAME), - (string) $this->config->get(ConfigKey::APPLICATION_KEY), - $this->config->get(ConfigKey::API_VERSION) - ))) { - $this->logger->debug('Send command returned false for RegisterMessage'); - - return false; - } - - if (! $this->connector->sendCommand(new Metadata( - new DateTimeImmutable('now', new DateTimeZone('UTC')), - $this->config - ))) { - $this->logger->debug('Send command returned false for Metadata'); - - return false; - } - - $this->markRegistrationComplete(); + if (! $this->connector->sendCommand(new RegisterMessage( + (string) $this->config->get(ConfigKey::APPLICATION_NAME), + (string) $this->config->get(ConfigKey::APPLICATION_KEY), + $this->config->get(ConfigKey::API_VERSION) + ))) { + $this->logger->debug('Send command returned false for RegisterMessage'); + + return false; } + $this->sendMetadataIfRequired(); + $this->request->stopIfRunning(); if (! $this->connector->sendCommand($this->request)) { @@ -376,18 +366,41 @@ public function send() : bool } } - private function registrationIsComplete() : bool + /** + * @throws Exception + */ + private function sendMetadataIfRequired() : void + { + if ($this->metadataWasSent()) { + $this->logger->debug('Skipping metadata send, already sent'); + + return; + } + + if (! $this->connector->sendCommand(new Metadata( + new DateTimeImmutable('now', new DateTimeZone('UTC')), + $this->config + ))) { + $this->logger->debug('Send command returned false for Metadata'); + + return; + } + + $this->markMetadataSent(); + } + + private function metadataWasSent() : bool { - return (bool) $this->cache->get(self::CACHE_KEY_REGISTRATION_COMPLETE, false); + return (bool) $this->cache->get(self::CACHE_KEY_METADATA_SENT, false); } - private function markRegistrationComplete() : void + private function markMetadataSent() : void { - if ($this->registrationIsComplete()) { + if ($this->metadataWasSent()) { return; } - $this->cache->set(self::CACHE_KEY_REGISTRATION_COMPLETE, true); + $this->cache->set(self::CACHE_KEY_METADATA_SENT, true); } /**