Azure / azure-functions-python-library

Azure Functions Python SDK
MIT License
154 stars 67 forks source link

Multiple Output Messages to Service Bus #79

Closed chielpeters90 closed 3 years ago

chielpeters90 commented 3 years ago

For the Queue output binding it is possible to send multiple messages to the same output, see Azure Documentation. In a similar fashion I would like to be able to do the same for Service Bus Messages.

My use case is to implement an Azure Function based on a timer which scrapes some data from an API and sends multiple messages to the same service bus topic. In a very basic example my function would look like this

__init__.py

import typing
import azure.functions as func

def main(mytimer: func.TimerRequest, outputmessage: func.Out[typing.List[str]]):
    outputmessage.set(["Message 1","Message 2"])

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */1 * * * *"
    },
    {
      "type": "serviceBus",
      "direction": "out",
      "connection": "AzureServiceBusConnectionString",
      "name": "outputmessage",
      "topicName": "mytopic2"
    }
  ]
}

Currently this is not allowed as the ServiceBusMessageOutConverter only allows for a str or bytes type. I converted both methods encode and check_output_type_annotation to allow for collections in a similar fashion to the Queue Binding and when I run the function locally it is able to create multiple output messages to the same binding.

Is this something that can easily be added to the library (I would be willing to submit a PR) or is this approach breaking something?

ijoosong commented 3 years ago

For the time being, instead of using the Output Binding, could you try doing this with the SDK and report back? This link should have everything you need to be able to send multiple messages from within the azure function without using the output bindings!

Hazhzeng commented 3 years ago

Currently we don't have plan to support multiple service bus output message. As @ijoosong mentioned, the best bet is to use our azure-servicebus package. It provides full service bus integration. Follow the link ijoosong provided to access the example code.