check_query_status.py 742 B

123456789101112131415161718192021222324252627
  1. import boto3
  2. from decorators import with_logging
  3. client = boto3.client("athena")
  4. @with_logging
  5. def handler(event, context):
  6. execution_retries_left = event["ExecutionRetriesLeft"]
  7. execution_details = client.get_query_execution(QueryExecutionId=event["QueryId"])[
  8. "QueryExecution"
  9. ]
  10. state = execution_details["Status"]["State"]
  11. needs_retry = state == "FAILED" or state == "CANCELLED"
  12. if needs_retry:
  13. execution_retries_left -= 1
  14. result = {
  15. **event,
  16. "State": state,
  17. "Reason": execution_details["Status"].get("StateChangeReason", "n/a"),
  18. "Statistics": execution_details["Statistics"],
  19. "ExecutionRetriesLeft": execution_retries_left,
  20. }
  21. return result