|
| 1 | +from datetime import datetime |
| 2 | +import logging |
| 3 | +import azure.functions as func |
| 4 | +import azure.durable_functions as df |
| 5 | + |
| 6 | +bp = df.Blueprint() |
| 7 | + |
| 8 | +attempt_count = {} |
| 9 | + |
| 10 | +class CustomException(Exception): |
| 11 | + pass |
| 12 | + |
| 13 | +@bp.route(route="RethrowActivityException_HttpStart") |
| 14 | +@bp.durable_client_input(client_name="client") |
| 15 | +async def rethrow_activity_exception_http(req: func.HttpRequest, client): |
| 16 | + instance_id = await client.start_new('rethrow_activity_exception') |
| 17 | + |
| 18 | + logging.info(f"Started orchestration with ID = '{instance_id}'.") |
| 19 | + return client.create_check_status_response(req, instance_id) |
| 20 | + |
| 21 | +@bp.route(route="CatchActivityException_HttpStart") |
| 22 | +@bp.durable_client_input(client_name="client") |
| 23 | +async def catch_activity_exception_http(req: func.HttpRequest, client): |
| 24 | + instance_id = await client.start_new('catch_activity_exception') |
| 25 | + |
| 26 | + logging.info(f"Started orchestration with ID = '{instance_id}'.") |
| 27 | + return client.create_check_status_response(req, instance_id) |
| 28 | + |
| 29 | +@bp.route(route="CatchActivityExceptionFailureDetails_HttpStart") |
| 30 | +@bp.durable_client_input(client_name="client") |
| 31 | +async def catch_activity_exception_fd_http(req: func.HttpRequest, client): |
| 32 | + instance_id = await client.start_new('catch_activity_exception_failure_details') |
| 33 | + |
| 34 | + logging.info(f"Started orchestration with ID = '{instance_id}'.") |
| 35 | + return client.create_check_status_response(req, instance_id) |
| 36 | + |
| 37 | +@bp.route(route="RetryActivityException_HttpStart") |
| 38 | +@bp.durable_client_input(client_name="client") |
| 39 | +async def retry_activity_exception_http(req: func.HttpRequest, client): |
| 40 | + instance_id = await client.start_new('retry_activity_function') |
| 41 | + |
| 42 | + logging.info(f"Started orchestration with ID = '{instance_id}'.") |
| 43 | + return client.create_check_status_response(req, instance_id) |
| 44 | + |
| 45 | +@bp.route(route="CustomRetryActivityException_HttpStart") |
| 46 | +@bp.durable_client_input(client_name="client") |
| 47 | +async def custom_retry_activity_exception_http(req: func.HttpRequest, client): |
| 48 | + instance_id = await client.start_new('custom_retry_activity_function') |
| 49 | + |
| 50 | + logging.info(f"Started orchestration with ID = '{instance_id}'.") |
| 51 | + return client.create_check_status_response(req, instance_id) |
| 52 | + |
| 53 | +@bp.orchestration_trigger(context_name="context") |
| 54 | +def rethrow_activity_exception(context: df.DurableOrchestrationContext): |
| 55 | + yield context.call_activity('raise_exception', context.instance_id) |
| 56 | + |
| 57 | +@bp.orchestration_trigger(context_name="context") |
| 58 | +def catch_activity_exception(context: df.DurableOrchestrationContext): |
| 59 | + try: |
| 60 | + yield context.call_activity('raise_exception', context.instance_id) |
| 61 | + except Exception as e: |
| 62 | + logging.error(f"Caught exception: {e}") |
| 63 | + return f"Caught exception: {e}" |
| 64 | + |
| 65 | +@bp.orchestration_trigger(context_name="context") |
| 66 | +def catch_activity_exception_failure_details(context: df.DurableOrchestrationContext): |
| 67 | + try: |
| 68 | + yield context.call_activity('raise_exception', context.instance_id) |
| 69 | + except Exception as e: |
| 70 | + logging.error(f"Caught exception: {e}") |
| 71 | + return f"Caught exception: {e}" |
| 72 | + |
| 73 | +@bp.orchestration_trigger(context_name="context") |
| 74 | +def retry_activity_function(context: df.DurableOrchestrationContext): |
| 75 | + yield context.call_activity_with_retry('raise_exception', retry_options=df.RetryOptions( |
| 76 | + first_retry_interval_in_milliseconds=5000, |
| 77 | + max_number_of_attempts=3 |
| 78 | + ), input_=context.instance_id) |
| 79 | + return "Success" |
| 80 | + |
| 81 | +@bp.orchestration_trigger(context_name="context") |
| 82 | +def custom_retry_activity_function(context: df.DurableOrchestrationContext): |
| 83 | + yield context.call_activity_with_retry('raise_complex_exception', retry_options=df.RetryOptions( |
| 84 | + first_retry_interval_in_milliseconds=5000, |
| 85 | + max_number_of_attempts=3 |
| 86 | + ), input_=context.instance_id) |
| 87 | + return "Success" |
| 88 | + |
| 89 | +@bp.activity_trigger(input_name="instance") |
| 90 | +def raise_exception(instance: str) -> str: |
| 91 | + global attempt_count |
| 92 | + if instance not in attempt_count: |
| 93 | + attempt_count[instance] = 1 |
| 94 | + raise CustomException(f"This activity failed") |
| 95 | + return "This activity succeeded" |
| 96 | + |
| 97 | +@bp.activity_trigger(input_name="instance2") |
| 98 | +def raise_complex_exception(instance2: str) -> str: |
| 99 | + global attempt_count |
| 100 | + if instance2 not in attempt_count: |
| 101 | + attempt_count[instance2] = 1 |
| 102 | + raise CustomException(f"This activity failed") from Exception("More information about the failure") |
| 103 | + return "This activity succeeded" |
0 commit comments