定时器触发器不触发队列,但手动输入会-Python

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

我有一个队列触发器,当手动将消息添加到队列中时,它会启动并按预期运行。但是,当消息通过以下计时器触发器函数写入队列时,它无法启动。可以看到触发器写入消息成功

** 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)"
  }
}

有什么我遗漏的吗?

根据一些测试,问题与base64编码有关。我添加了三行代码:

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

完整功能代码请参考以下:

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 * * * * *"
    }
  ]
}

如果您不想使用 base64 编码(我个人不建议这样做),那么设置策略就没有意义了,只需删除以下行,您的原始代码就可以工作了:

    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()

See example 来自文档。