Azure / azure-functions-durable-python

Python library for using the Durable Functions bindings.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

context.signal_entity causes sub orchestrator to never finish.

Goldziher opened this issue Β· comments

πŸ› Describe the bug
Using context.signal_entity causes a sub-orchestrator to never finish. That is, it prevents StopIteration from being raised.

πŸ€” Expected behavior
Signalling an entity does not cause the generator to fail

β˜• Steps to reproduce
Create an entity and signal it from within a sub orchestrator. This will cause it to not finish. Now switch it to use context.call_entity, which works as expected.

Hi, @Goldziher Do you have durable functions app that can help me check the logs or any samples that can help me reproduce at my local end?

Hi, @Goldziher Do you have durable functions app that can help me check the logs or any samples that can help me reproduce at my local end?

I'm afraid only closed source

No worries. Can you help me specify the scenario using with context.signal_entity? Like calling this in a sub-orchestrator and thus make it never finish.

No worries. Can you help me specify the scenario using with context.signal_entity? Like calling this in a sub-orchestrator and thus make it never finish.

Certainly. I'll post some snippets later

Ok, here is some code that works as is, but if you change call_activity to signal_activity it blocks indefinitely:

def file_download_orchestrator(
    context: DurableOrchestrationContext,
) -> Generator[TaskBase, None, NotificationAttachment | None]:
    """This is a sub-orchestrator function that downloads a file from Google Drive, saves it to blob storage and updates
    the state of the durable entity.

    Args:
        context: The durable orchestration context.

    Returns:
        A generator.
    """
    directory_path, file_datum = cast(tuple[str, FileData], context.get_input())
    # 1. download file
    file_blob_or_error = cast(
        FileBlob | NotificationAttachment,
        (yield context.call_activity(FILE_DOWNLOAD_ACTIVITY, (directory_path, file_datum))),
    )

    if not all(k in file_blob_or_error for k in FileBlob.__annotations__):
        logger.warning("File %s download failed. Skipping file persistence and state update.", file_datum["id"])
        return cast(NotificationAttachment, file_blob_or_error)

    file_blob = cast(FileBlob, file_blob_or_error)

    # 2. save file to blob storage
    yield context.call_activity(SAVE_BLOB_DATA_ACTIVITY, file_blob)
    # 3. update state
    # note: we are using context.call_entity rather than context.signal_entity, because signal_entity causes blocking
    yield context.signal_entity(
        EntityId(STORE_HANDLER_ENTITY, file_blob["drive_id"]),
        EntityStateOperation.SET.value,
        (file_blob["file_id"], file_blob["modified_time"]),
    )
    logger.info("File %s was downloaded and persisted successfully", file_blob["file_id"])
    return None

And this is the entity handler:

from enum import StrEnum
from typing import cast

from azure.durable_functions import DurableEntityContext


class EntityStateOperation(StrEnum):
    """An enum for the durable entity state operations."""

    GET = "get"
    DEL = "del"
    SET = "set"


def store_handler(context: DurableEntityContext) -> None:
    """Handler for the durable entity. A durable entity is created for each drive ID.

    Args:
        context: The durable entity context, injected by the runtime.

    Returns:
        None
    """
    state = context.get_state(dict)
    match context.operation_name:
        case EntityStateOperation.GET.value:
            context.set_result(state)
        case EntityStateOperation.DEL.value:
            state.pop(cast(str, context.get_input()), None)
            context.set_state(state)
        case EntityStateOperation.SET.value:
            file_id, modified_date = cast(tuple[str, int], context.get_input())
            context.set_state({**state, file_id: modified_date})
        case _:
            # this should never happen
            raise ValueError(f"Invalid operation: {context.operation_name}")