Azure Functions - Durable functions in Python v2 - Orchestration function starts new every time

Egon 0 Reputation points
2024-08-30T21:19:18.9666667+00:00

Hey guys, i am new to azure functions. Maybe one expert can have a quick look at the code and give me some advice. I want to use a simple durable function whith python v2 in vs code.
I have the azure extensions, Azure functions core tools, etc. installed.
I wrote the following example of reading a book (which is not working):

The Problem: The ochestration function does not seem to pick up right after the activity function returns. It starts from the beginning.

How it should work:
http_start trigger: Starts the orchestrator function
orchestrator function: Manages the book reading process
activity function: flips the page

import azure.functions as func
import azure.durable_functions as df
import logging.config
import logging

app = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.orchestration_trigger(context_name="context")
def orchestrator_reading_a_book(context: df.DurableOrchestrationContext):
    x = None
    book = "Example_book"
    page = 1
    logging.warning(f'Head: Your Book: {book} at page {page}')
    instance_id = context.instance_id
    logging.warning(f'instande.id: {instance_id}')
    while page < 4:
        x = yield context.call_activity("flip_page", {
            "book": book,
            "page": page,
        })
        book = x.get("book")
        page = x.get("page")

        logging.warning(f'Lower: Your Book: {book} at page {page}')
    logging.warning("You finished reading the book")        
    return("You finished reading the book")

@app.activity_trigger(input_name="context")
def flip_page(context):
    if context:
        book = context["book"]
        page = context["page"]
        page += 1 
        logging.warning(f"You flipped the page to page {page}")
    else:
        logging.warning(f"Page has not been flipped")   
    return {"book": book, "page": page}
    

# An HTTP-triggered function with a Durable Functions client binding
@app.route(route="orchestrators/{functionName}")
@app.durable_client_input(client_name="client")
async def http_start(req: func.HttpRequest, client):
    function_name = req.route_params.get('functionName')
    
    instance_id = await client.start_new(function_name)
    response = client.create_check_status_response(req, instance_id)
    return response
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,890 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 10,036 Reputation points
    2024-09-02T19:13:04.7966667+00:00

    Hello Egon,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    Regarding your explanations and questions, I understand that your orchestration function does not pick up rightly after the activity function returns and it starts from the beginning.

    Your Azure Durable Function code seems correct, but a few things you can check and adjust to ensure that your orchestrator function picks up right after the activity function returns and processes correctly. I made slight adjustment to your code as shown here:

    import azure.functions as func
    import azure.durable_functions as df
    import logging.config
    import logging
    app = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
    @app.orchestration_trigger(context_name="context")
    def orchestrator_reading_a_book(context: df.DurableOrchestrationContext):
        x = None
        book = "Example_book"
        page = 1
        logging.warning(f'Head: Your Book: {book} at page {page}')
        instance_id = context.instance_id
        logging.warning(f'instance_id: {instance_id}')
        while page < 4:
            x = yield context.call_activity("flip_page", {
                "book": book,
                "page": page,
            })
            book = x.get("book")
            page = x.get("page")
            logging.warning(f'Lower: Your Book: {book} at page {page}')
        logging.warning("You finished reading the book")        
        return("You finished reading the book")
    @app.activity_trigger(input_name="context")
    def flip_page(context):
        if context:
            book = context["book"]
            page = context["page"]
            page += 1 
            logging.warning(f"You flipped the page to page {page}")
        else:
            logging.warning(f"Page has not been flipped")   
        return {"book": book, "page": page}
    # An HTTP-triggered function with a Durable Functions client binding
    @app.route(route="orchestrators/{functionName}")
    @app.durable_client_input(client_name="client")
    async def http_start(req: func.HttpRequest, client):
        function_name = req.route_params.get('functionName')
        
        instance_id = await client.start_new(function_name)
        response = client.create_check_status_response(req, instance_id)
        return response
    

    So, look deeper into your configuration and environment:

    • Verify that the Durable Functions extension is installed and properly configured in your VS Code environment.
    • Ensure that the function names in your call_activity method match the actual function names.
    • Ensure that logging is properly configured to capture the logs.
    • Fix the typo in the logging statement for instance_id.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.