Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function - Python - ServiceBus Output Binding - Setting Custom Properties

I have an Azure Function written in Python that has an Service Bus (Topic) output binding. The function is triggered by another queue, we process some files from a blobl storage and then put another message in a queue.

My function.json file looks like that:

{
"bindings": [
    {
    "type": "serviceBus",
    "connection": "Omnibus_Input_Send_Servicebus",
    "name": "outputMessage",
    "queueName": "validation-output-queue",
    "accessRights": "send",
    "direction": "out"
    }
],
"disabled": false
}

In my function, I can send a message to another queue like that:

with open(os.environ['outputMessage'], 'w') as output_message:
    output_message.write('This is my output test message !')

It is working fine. Now I'd like to send a message to a topic. I've created a subscription with an SQLFilter and I need to set some custom properties to the BrokeredMessage.

From the azure sdk for python, I've found that I can add custom properties like that (I've installed the azure module using pip):

from azure.servicebus import Message
sent_msg = Message(b'This is the third message',
    broker_properties={'Label': 'M3'},
    custom_properties={'Priority': 'Medium',
                        'Customer': 'ABC'}
)

My new function.json file looks like that:

{
"bindings": [
    {
    "type": "serviceBus",
    "connection": "Omnibus_Input_Send_Servicebus",
    "name": "outputMessage",
    "topicName": "validation-output-topic",
    "accessRights": "send",
    "direction": "out"
    }
],
"disabled": false
}

And I've modify my function like that:

from azure.servicebus import Message
sent_msg = Message(b'This is the third message',
    broker_properties={'Label': 'M3'},
    custom_properties={'Priority': 'Medium',
                        'Customer': 'ABC'}
)

with open(os.environ['outputMessage'], 'w') as output_message:
    output_message.write(sent_msg)

When I run the function, I get this exception:

TypeError: expected a string or other character buffer object

I tried to use the buffer and the memoryview function but still get another exception:

TypeError: cannot make memory view because object does not have the buffer interface

I am wondering if the actual binding supports BrokeredMessage and how to deal with it ?

like image 778
Thomas Avatar asked Oct 10 '17 01:10

Thomas


People also ask

Can an azure function have multiple output bindings?

In the conventional In process Azure functions, the function is a class library that run in the same process as the Host Process. With the in process functions, you could easily support multiple output binding by marking the ouput parameter as out .

What is output binding in Azure functions?

An input binding is the data that your function receives. An output binding is the data that your function sends. Unlike a trigger, a function can have multiple input and output bindings.


2 Answers

The ServiceBus output binding for Python (and other script languages) only supports a simple string mapping, where the string you specify becomes the content of the BrokeredMessage created behind the scenes. To set any extended properties or do anything more sophisticated, you'll have to drop down to using the Azure Python SDK yourself in your function.

like image 138
mathewc Avatar answered Sep 25 '22 15:09

mathewc


In the same situation, where I need to add user properties in the output service bus queue/topic, I used azure.servicebus.ServiceBusClient directly.

sb.Message class has a user_properties setter:

def main(
    httpreq: func.HttpRequest,
    context: func.Context  ):

sbClient : sb.ServiceBusClient = sb.ServiceBusClient.from_connection_string( os.getenv("AzureWebJobsServiceBus") )
topicClient : sb.TopicClient = sbClient.get_topic('scoring-testtopic')

message = sb.Message( httpreq.get_body().decode( 'UTF-8' ))

message.user_properties = { 
    '@AzureWebJobsParentId' : context.invocation_id,
    'Prom' : '31000001'
    }

topicClient.send( message )
like image 34
Anthony Brenelière Avatar answered Sep 25 '22 15:09

Anthony Brenelière