-
Notifications
You must be signed in to change notification settings - Fork 22
feat(scribe): Implement SQS to eliminate state machine polling. #4639
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4922fa4
feat(scribe): Implement SQS to eliminate state machine polling.
eyw520 a668222
tmp settings update
eyw520 c13b0da
update iam permissions for sqs
eyw520 7ab7824
update
eyw520 3da4386
update sotw handler
eyw520 a8d59ce
implement resume request batching
eyw520 0c3aef2
prevent oos errors
eyw520 a7d682f
fix git configure
eyw520 1b927d5
simplify git
eyw520 33c5f04
use larger tmp
eyw520 ea21ded
update sqs
eyw520 1ddbb45
add latest handler updates.
eyw520 3dcde0c
update
eyw520 4650796
Merge branch 'app' into eden/scribe-implement-sqs-polling
eyw520 06daeef
revert settings
eyw520 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import json | ||
| from typing import Any | ||
|
|
||
| import boto3 | ||
| from botocore.exceptions import ClientError | ||
|
|
||
| from ..settings import LOGGER | ||
|
|
||
|
|
||
| class SQSClient: | ||
| def __init__(self, queue_url: str): | ||
| self.queue_url = queue_url | ||
| self.sqs_client = boto3.client("sqs") | ||
| self._last_checked_count = 0 | ||
| self._cached_messages: list[dict[str, Any]] = [] | ||
|
|
||
| def receive_messages(self, max_messages: int = 10) -> list[dict[str, Any]]: | ||
| try: | ||
| response = self.sqs_client.receive_message( | ||
| QueueUrl=self.queue_url, | ||
| MaxNumberOfMessages=min(max_messages, 10), | ||
| WaitTimeSeconds=0, # Short polling for Lambda | ||
| AttributeNames=["All"], | ||
| ) | ||
|
|
||
| messages = response.get("Messages", []) | ||
| parsed_messages = [] | ||
|
|
||
| for msg in messages: | ||
| try: | ||
| body = json.loads(msg["Body"]) | ||
| parsed_messages.append( | ||
| { | ||
| "body": body, | ||
| "receipt_handle": msg["ReceiptHandle"], | ||
| "message_id": msg["MessageId"], | ||
| } | ||
| ) | ||
| except json.JSONDecodeError: | ||
| LOGGER.warning(f"Failed to parse message body as JSON: {msg['Body']}") | ||
| continue | ||
|
|
||
| if parsed_messages: | ||
| self._last_checked_count += len(parsed_messages) | ||
| LOGGER.info(f"Received {len(parsed_messages)} messages from queue") | ||
|
|
||
| return parsed_messages | ||
|
|
||
| except ClientError as e: | ||
| LOGGER.error(f"Failed to receive messages from queue: {e.response['Error']['Message']}") | ||
| return [] | ||
| except Exception as e: | ||
| LOGGER.error(f"Unexpected error receiving messages: {str(e)}", exc_info=True) | ||
| return [] | ||
|
|
||
| def delete_message(self, receipt_handle: str) -> bool: | ||
| try: | ||
| self.sqs_client.delete_message(QueueUrl=self.queue_url, ReceiptHandle=receipt_handle) | ||
| LOGGER.debug("Deleted message from queue") | ||
| return True | ||
|
|
||
| except ClientError as e: | ||
| LOGGER.error(f"Failed to delete message: {e.response['Error']['Message']}") | ||
| return False | ||
| except Exception as e: | ||
| LOGGER.error(f"Unexpected error deleting message: {str(e)}", exc_info=True) | ||
| return False | ||
|
|
||
| def has_interrupt_message(self) -> tuple[bool, str | None]: | ||
| messages = self.receive_messages(max_messages=10) | ||
|
|
||
| for msg in messages: | ||
| body = msg["body"] | ||
| if body.get("type") == "INTERRUPT": | ||
| LOGGER.info("Found INTERRUPT message in queue") | ||
| return True, msg["receipt_handle"] | ||
| else: | ||
| self._cached_messages.append(msg) | ||
|
|
||
| return False, None | ||
|
|
||
| def get_resume_messages(self) -> list[dict[str, Any]]: | ||
| resume_messages = [] | ||
|
|
||
| for msg in self._cached_messages: | ||
| body = msg["body"] | ||
| if body.get("type") == "RESUME": | ||
| resume_messages.append({"body": body, "receipt_handle": msg["receipt_handle"]}) | ||
|
|
||
| messages = self.receive_messages(max_messages=10) | ||
| for msg in messages: | ||
| body = msg["body"] | ||
| if body.get("type") == "RESUME": | ||
| resume_messages.append({"body": body, "receipt_handle": msg["receipt_handle"]}) | ||
|
|
||
| if resume_messages: | ||
| num_resume = len(resume_messages) | ||
| num_cached = len([m for m in self._cached_messages if m["body"].get("type") == "RESUME"]) | ||
| num_new = len([m for m in messages if m["body"].get("type") == "RESUME"]) | ||
| LOGGER.info(f"Found {num_resume} RESUME messages ({num_cached} cached, {num_new} new)") | ||
|
|
||
| self._cached_messages.clear() | ||
|
|
||
| return resume_messages | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.