Skip to content

Commit

Permalink
Refresh with fresh sequence token (#56)
Browse files Browse the repository at this point in the history
* Refresh with fresh sequence token

* reformats phpdoc to fit within 140 characters
  • Loading branch information
mstovicek authored and maxbanton committed Oct 5, 2018
1 parent 4034b71 commit 419c94d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Handler/CloudWatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,17 @@ private function addToBuffer(array $record)
private function flushBuffer()
{
if (!empty($this->buffer)) {
// send items
$this->send($this->buffer);
// send items, retry once with a fresh sequence token
try {
$this->send($this->buffer);
} catch (\Aws\CloudWatchLogs\Exception\CloudWatchLogsException $e) {
$this->refreshSequenceToken();
$this->send($this->buffer);
}

// clear buffer
$this->buffer = [];

// clear data amount
$this->currentDataAmount = 0;
}
Expand Down Expand Up @@ -240,6 +247,9 @@ private function formatRecords(array $entry)
* - A batch of log events in a single request cannot span more than 24 hours. Otherwise, the operation fails.
*
* @param array $entries
*
* @throws \Aws\CloudWatchLogs\Exception\CloudWatchLogsException Thrown by putLogEvents for example in case of an
* invalid sequence token
*/
private function send(array $entries)
{
Expand Down Expand Up @@ -305,6 +315,13 @@ function ($group) {
}
}

$this->refreshSequenceToken();

$this->initialized = true;
}

private function refreshSequenceToken()
{
// fetch existing streams
$existingStreams =
$this
Expand Down

3 comments on commit 419c94d

@floranpagliai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, the error still happening to me when we have multiple concurrent processes that try to call flushBuffer.

Instead of retry once, maybe it should be retrying multiple times?

@maxbanton
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @floranpagliai.
I don't want to add cyclic function calls since it's not a good idea.
This library is not very well suited for multiple concurrent processes flow due to AWS API limitation.
There are discussion about it #28 (comment).

Regards.

@floranpagliai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I understand, thank you for linking me this conversation, it's helpful.

Regards.

Please sign in to comment.