Poll transaction status until it reaches the target status or max attempts are exceeded. Args: client: The cosdata client instance collection_name: Name of the collection txn_id: Transaction ID to poll target_status: Target status to wait for (default: '
(client, collection_name, txn_id, target_status='complete',
max_attempts=10, sleep_interval=1)
| 10 | return result['status'] |
| 11 | |
| 12 | def poll_transaction_completion(client, collection_name, txn_id, target_status='complete', |
| 13 | max_attempts=10, sleep_interval=1): |
| 14 | """ |
| 15 | Poll transaction status until it reaches the target status or max attempts are exceeded. |
| 16 | |
| 17 | Args: |
| 18 | client: The cosdata client instance |
| 19 | collection_name: Name of the collection |
| 20 | txn_id: Transaction ID to poll |
| 21 | target_status: Target status to wait for (default: 'complete') |
| 22 | max_attempts: Maximum number of polling attempts |
| 23 | sleep_interval: Time to sleep between attempts in seconds |
| 24 | |
| 25 | Returns: |
| 26 | tuple: (final_status, success_boolean) |
| 27 | """ |
| 28 | for attempt in range(max_attempts): |
| 29 | try: |
| 30 | print(f"Attempt {attempt + 1}: Waiting for transaction {txn_id} to complete...") |
| 31 | |
| 32 | # Get actual transaction status |
| 33 | status = get_transaction_status(client, collection_name, txn_id) |
| 34 | |
| 35 | if status == target_status: |
| 36 | print(f"Transaction {txn_id} completed successfully") |
| 37 | return status, True |
| 38 | |
| 39 | if attempt < max_attempts - 1: |
| 40 | time.sleep(sleep_interval) |
| 41 | |
| 42 | except Exception as e: |
| 43 | print(f"Error polling transaction status: {e}") |
| 44 | if attempt < max_attempts - 1: |
| 45 | time.sleep(sleep_interval) |
| 46 | |
| 47 | print(f"Transaction {txn_id} may not have completed within {max_attempts} attempts") |
| 48 | return "unknown", False |