Skip to content

Commit

Permalink
handle exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Jul 16, 2019
1 parent 12815d4 commit 67c1c45
Showing 1 changed file with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Monolog\Handler\AbstractHandler;
use Monolog\Logger;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand Down Expand Up @@ -116,10 +117,53 @@ private function sendToElasticsearch(array $records)

$this->responses->attach($response);

foreach ($this->client->stream($this->responses, 0) as $response => $chunk) {
if (!$chunk->isTimeout() && $chunk->isFirst()) {
try {
$this->wait(0.0, false);
} catch (ExceptionInterface $e) {
error_log(sprintf("Could not push logs to elasticsearch:\n%s", (string) $e));
}
}

public function __destruct()
{
try {
$this->wait(null, true);
} catch (ExceptionInterface $e) {
error_log(sprintf("Could not push logs to elasticsearch:\n%s", (string) $e));
}
}

private function wait(?float $timeout, bool $errorOnTimeout)
{
$e = null;
$remainingTimeout = $timeout;

if (!$errorOnTimeout && $remainingTimeout) {
$startTime = microtime(true);
}

foreach ($this->client->stream($this->responses, $remainingTimeout) as $response => $chunk) {
try {
if ($chunk->isTimeout() && !$errorOnTimeout) {
continue;
}
if (!$chunk->isFirst() && !$chunk->isLast()) {
continue;
}
if ($chunk->isLast()) {
$this->responses->detach($response);
}
} catch (ExceptionInterface $e) {
$this->responses->detach($response);
} finally {
if (!$errorOnTimeout && $remainingTimeout) {
$remainingTimeout = max(0.0, $timeout - microtime(true) + $startTime);
}
}
}

if ($e) {
throw $e;
}
}
}

0 comments on commit 67c1c45

Please sign in to comment.