def cleanup_sqs_records(records: list, logger): base_err_msg = ( "Unable to delete successful records messages from SQS queue. AWS should " "still handle this automatically when the lambda finishes executing, but " "this may result in successful messages being sent to the DLQ if any " "other messages fail.") try: Message = namedtuple("Message", ["message_id", "receipt_handle"]) messages = defaultdict(list) for record in records: m = Message( message_id=mindictive.get_nested(record, ["messageId"]), receipt_handle=mindictive.get_nested(record, ["receiptHandle"]), ) messages[mindictive.get_nested(record, ["eventSourceARN"])].append(m) for k in messages.keys(): queue_url = sqs.get_queue_url(k) sqs.batch_delete_messages( queue_url, [{ "Id": m.message_id, "ReceiptHandle": m.receipt_handle } for m in messages], ) except KeyError as e: logger.warning( f"{base_err_msg} If you're testing, this is not an issue. {utils.exception_to_str(e)}" ) except Exception as e: logger.warning(f"{base_err_msg} {utils.exception_to_str(e)}")
def check_status(response, code=2, keys=["ResponseMetadata", "HTTPStatusCode"]): """Check status of an AWS API response.""" status = mindictive.get_nested(response, keys) assert status // 100 == code return status
def get_queue_arn(queue_url): return mindictive.get_nested( utils.call( lpipe.contrib.boto3.client("sqs").get_queue_attributes, QueueUrl=queue_url, AttributeNames=["QueueArn"], ), ["Attributes", "QueueArn"], )
def get_event_source(event_source_type: EventSourceType, record): if event_source_type in ( EventSourceType.RAW, EventSourceType.KINESIS, EventSourceType.SQS, ): return mindictive.get_nested(record, ["event_source_arn"], None) warnings.warn( f"Unable to fetch event_source for {event_source_type} record.") return None
def get_event_source(queue_type: QueueType, record): if queue_type in (QueueType.RAW, QueueType.KINESIS, QueueType.SQS): return mindictive.get_nested(record, ["event_source_arn"], None) warnings.warn(f"Unable to fetch event_source for {queue_type} record.") return None
def test_get_raises(self, fixture_name, fixture): with pytest.raises(fixture["raises"]): mindictive.get_nested(fixture["obj"], **fixture["kwargs"])
def test_get(self, fixture_name, fixture): val = mindictive.get_nested(fixture["obj"], **fixture["kwargs"]) if "type" in fixture: assert isinstance(val, fixture["type"]) assert val == fixture["output"]