Azure Queue Storage with multiple functions not running any functions NOT in function_app.py

96 Views Asked by At

I have 2 functions in my Azure function app using Queue Trigger: queue_trigger1, queue_trigger2. Running locally in VS Code.

The first function, queue_trigger1 located in function_app.py will send a message to queue_2, located in function_app2.py:

@app.queue_trigger(arg_name="azqueue", queue_name="queue_1",
                               connection="QueueConnectionString") 
def queue_trigger1(azqueue: func.QueueMessage):
    # do stuff, then send message to queue_2 to do more stuff
    queue_client = QueueClient.from_connection_string(connect_str, queue_name="queue_2")
    queue_client.send_message(azqueue.get_body().decode('utf-8'))

Running func start by itself will not run any code at all. So I did func start --functions queue_trigger1 and it successfully runs all the code in queue_trigger1, AND sends the message successfully to queue_2.

However, it ends there. Queue_2 is not triggered and nothing is done with the sent message, which is understandable because I only specified queue_trigger1 in my command.

I have tried running the code with func start --functions queue_trigger1 queue-trigger2 but then nothing runs again.

I realized that the naming of the function_app.py is the only thing that is recognized. If I instead put my queue_trigger2 code in function_app.py and run func start --functions queue-trigger2, then my queue_trigger2 code will then process the sent message (but I have to do this manually. I want queue_trigger2 to run automatically once message is sent from queue_trigger1)

Is this the wrong assumption? Is there a command I can use for me to run all the functions I have in my function app all at once while also separating my functions in different files?

1

There are 1 best solutions below

0
Dasari Kamali On

In the Function V2 programming model outlined in the Microsoft Doc, streamlining folder and file management involves adding multiple Azure Function triggers within the same function_app.py file, along with multiple input and output bindings.

I tried the below code to trigger messages from queue1 to queue2 .

Code :

import os
import logging
import azure.functions as func
import base64
from azure.storage.queue import QueueClient

queue_connection_string = os.environ["QueueConnectionString"]

app = func.FunctionApp()
queue1_name = "queue1"
queue2_name = "queue2"

queue1_client = QueueClient.from_connection_string(queue_connection_string, queue1_name)
queue2_client = QueueClient.from_connection_string(queue_connection_string, queue2_name)

@app.queue_trigger(arg_name="azqueue", queue_name="queue1", connection="QueueConnectionString")
def queue_trigger_1(azqueue: func.QueueMessage):
    message_body = azqueue.get_body().decode('utf-8')
    logging.info('Python Queue trigger for queue1 processed a message: %s', message_body)

    base64_encoded_message = base64.b64encode(message_body.encode('utf-8')).decode('utf-8')
    queue2_client.send_message(base64_encoded_message)

@app.queue_trigger(arg_name="azqueue", queue_name="queue2", connection="QueueConnectionString")
def queue_trigger_2(azqueue: func.QueueMessage):
    try:
        base64_decoded_message = base64.b64decode(azqueue.get_body()).decode('utf-8')
        logging.info('Python Queue trigger for queue2 processed a message: %s', base64_decoded_message)
    except UnicodeDecodeError:
        logging.warning("Unable to decode message body to UTF-8. Raw bytes: %s", azqueue.get_body())

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<connec_string>",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "QueueConnectionString": "<connec_string>"
  }
}

Output:

It ran successfully and it triggers the message from queue1 to queue2.

enter image description here enter image description here