Skip to content

Commit

Permalink
Merge pull request #146 from scoutapp/only-attempt-to-connect-if-moni…
Browse files Browse the repository at this point in the history
…toring-enabled

Do not preemptively try to connect if monitoring is disabled
  • Loading branch information
Chris Schneider committed Dec 17, 2019
2 parents a02c7ee + 5e5d9b2 commit 0896f86
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Agent.php
Expand Up @@ -117,7 +117,10 @@ private function warnIfConfigValueIsNotSet(string $configKey) : void

private static function createConnectorFromConfig(Config $config) : SocketConnector
{
return new SocketConnector($config->get(ConfigKey::CORE_AGENT_SOCKET_PATH));
return new SocketConnector(
$config->get(ConfigKey::CORE_AGENT_SOCKET_PATH),
$config->get(ConfigKey::MONITORING_ENABLED)
);
}

public static function fromConfig(Config $config, LoggerInterface $logger, ?CacheInterface $cache = null, ?Connector $connector = null) : self
Expand Down
6 changes: 5 additions & 1 deletion src/Connector/SocketConnector.php
Expand Up @@ -40,12 +40,16 @@ final class SocketConnector implements Connector
/** @var string */
private $socketPath;

public function __construct(string $socketPath)
public function __construct(string $socketPath, bool $preEmptivelyAttemptToConnect)
{
$this->socketPath = $socketPath;

$this->socket = socket_create(AF_UNIX, SOCK_STREAM, 0);

if (! $preEmptivelyAttemptToConnect) {
return;
}

// Pre-emptive attempt to connect, strictly speaking `__construct` should not have side-effects, so if this
// fails then swallow it. The `Agent` goes on to call connect() anyway, and handles launching of the core agent.
try {
Expand Down
4 changes: 3 additions & 1 deletion tests/Integration/AgentTest.php
Expand Up @@ -71,7 +71,9 @@ public function testLoggingIsSent() : void
'monitor' => true,
]);

$connector = new MessageCapturingConnectorDelegator(new SocketConnector($config->get(ConfigKey::CORE_AGENT_SOCKET_PATH)));
$connector = new MessageCapturingConnectorDelegator(
new SocketConnector($config->get(ConfigKey::CORE_AGENT_SOCKET_PATH), true)
);

$_SERVER['REQUEST_URI'] = '/fake-path';

Expand Down

0 comments on commit 0896f86

Please sign in to comment.