Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry batch delivery with exception handling #32

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions watchtower/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, log_group=__name__, stream_name=None, use_queues=True, send_i
_idempotent_create(self.cwl_client.create_log_group,
logGroupName=self.log_group)

def _submit_batch(self, batch, stream_name):
def _submit_batch(self, batch, stream_name, max_retries=5):
if len(batch) < 1:
return
sorted_batch = sorted(batch, key=itemgetter('timestamp'), reverse=False)
Expand All @@ -86,15 +86,16 @@ def _submit_batch(self, batch, stream_name):
if self.sequence_tokens[stream_name] is not None:
kwargs["sequenceToken"] = self.sequence_tokens[stream_name]

try:
response = self.cwl_client.put_log_events(**kwargs)
except ClientError as e:
if e.response.get("Error", {}).get("Code") in ("DataAlreadyAcceptedException",
"InvalidSequenceTokenException"):
kwargs["sequenceToken"] = e.response["Error"]["Message"].rsplit(" ", 1)[-1]
for retry in range(max_retries):
try:
response = self.cwl_client.put_log_events(**kwargs)
else:
raise
break
except ClientError as e:
if e.response.get("Error", {}).get("Code") in ("DataAlreadyAcceptedException",
"InvalidSequenceTokenException"):
kwargs["sequenceToken"] = e.response["Error"]["Message"].rsplit(" ", 1)[-1]
else:
raise

if "rejectedLogEventsInfo" in response:
# TODO: make this configurable/non-fatal
Expand Down