Home > Back-end >  Can't add more than one item to Azure Queue in function app
Can't add more than one item to Azure Queue in function app

Time:05-02

I have a TimerTrigger function that runs every couple minutes and adds multiple items to a queue, which a QueueTrigger function should process. But each time the QueueTrigger function is firing only once.

my TimerTrigger function:

def main(mytimer: func.TimerRequest, msg: func.Out[str]) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    msg.set("1")
    msg.set("2")

QueueTrigger function:

def main(msg: func.QueueMessage) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))

function.json of the TimerTrigger:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */2 * * * *"
    },
    {
      "name": "msg",
      "type": "queue",
      "direction": "out",
      "queueName": "js-queue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

function.json of the QueueTrigger:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "js-queue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}

How can I fix this?

CodePudding user response:

There is no intrinsic way of connecting to the queue storage using azure time trigger i.e. the queue storage will treat the time trigger as any other console application or any other piece of program which is trying to connect to the storage and add message.

Thus, we have to add the messages the classical way by using connection strings.

connectionstring = ""
queuename = ""    
messages = ""
queueClient = QueueClient.from_connection_string(connectionString, queueName)
queueClient.send_message(message)

Thus, when queue trigger will start only once during the start and remain idle because no messages are added and thus it will not be triggered.

Refer the documentation on timetrigger and queuetrigger and how to add message to queue using python.

  • Related