Home > Software engineering >  Python Durable Function doesn't call Activity
Python Durable Function doesn't call Activity

Time:11-08

I have 3 Azure Functions where I am trying to orchestrate. For some reason, the activity function isn't getting invoked.

The flow is as below

F1 (Service Bus) -> F2 (Orchestrator) -> F3 (Activity)

F1

This is the function invoked by Service Bus message. This is working fine.

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


async def main(msg: func.ServiceBusMessage, starter: str):
    client = df.DurableOrchestrationClient(starter)
    json_body = json.loads(msg.get_body().decode('utf-8'))
    instance_id = await client.start_new('F2', None, json_body)

    logging.info(f"Started orchestration with ID = '{instance_id}'")

The config is

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "queueN",
      "connection": "processConnectionString"
    },
    {
      "name": "starter",
      "type": "durableClient",
      "direction": "in"
    }
  ]
}

F2

This is the orchestrator function and is invoked as well; however the execution is Awaited at the line result = yield context.call_activity('F3', input_json)

import logging
import json

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


def orchestrator_function(context: df.DurableOrchestrationContext):
    input_json = context.get_input()
    result = yield context.call_activity('F3', input_json)
    return result

main = df.Orchestrator.create(orchestrator_function)

The config is,

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "context",
      "type": "orchestrationTrigger",
      "direction": "in"
    }
  ]
}

F3

The final function where the activity should be performed. This is NOT getting invoked from F2

import logging

def main(input_json) -> str:
    logging.info('Input processed message : %s', input_json)
    return "Hello!"

The config is,

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "input_json",
      "type": "activityTrigger",
      "direction": "in"
    }
  ]
}

Using Azure Function 4 runtime and Python 3.7 (due to legacy reasons). UPDATE: Tried with Python 3.9 and doesn't work either

xxxxxx: Function 'F2 (Orchestrator)' awaited. IsReplay: False. State: Awaited. HubName: pyproxxxxx. AppName: py-proxxxxx. SlotName: Production. ExtensionVersion: 2.8.1. SequenceNumber: 11.

What could be the issue?

CodePudding user response:

When I try to run this code, I get a warning on the terminal reading

[2022-11-07T20:56:23.947Z] The 'F3' function is in error: The binding name input_json is invalid. Please assign a valid name to the binding.

So I think the issue is that the parameter name input_json is not a valid identifier in Azure Functions. I got it to work by renaming input_json to inputJson (i.e turning snake_case to camelCase).

So that all leads me to suspect that the issue is that snake_case is not parsed properly by the Azure Functions runtime, which I recognize is not idiomatic for Python devs. I'll pass on this feedback to the Python team.

  • Related