Search code examples
python-3.xazure-functionstimer-triggerqueuetrigger

Timer trigger does not trigger queue but manual entry does-Python


I have a queue trigger which when message is manually added into queue it gets started and runs as expected. However, when message is written into queue by the following timer trigger function it fails to start. I can see the message is successfully written by the trigger.

** init.py**

import datetime
import logging
import os, uuid
from azure.storage.queue import (
        QueueClient,
        BinaryBase64EncodePolicy,
        BinaryBase64DecodePolicy
)

import os, uuid
import azure.functions as func


def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    conn_str = os.environ['AzureWebJobsStorage']

    queue_name="outqueue12"
    
    message = u"Hello234"
    queue_client = QueueClient.from_connection_string(conn_str, queue_name)
    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()
    
    queue_client.send_message(message)
    
           
    logging.info('Python timer trigger function ran at %s', utc_timestamp)

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

Is there something I am missing out?


Solution

  • According to some test, the problem is related to base64 encode. I added three lines of code:

    message_bytes = message.encode('ascii')
    base64_bytes = base64.b64encode(message_bytes)
    base64_message = base64_bytes.decode('ascii')
    

    Please refer to the whole function code below: enter image description here

    host.json

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
      }
    }
    

    function.json

    {
      "scriptFile": "__init__.py",
      "bindings": [
        {
          "name": "mytimer",
          "type": "timerTrigger",
          "direction": "in",
          "schedule": "*/20 * * * * *"
        }
      ]
    }