【问题标题】:Timer trigger does not trigger queue but manual entry does-Python定时器触发器不会触发队列但手动输入会-Python
【发布时间】:2021-04-13 20:49:46
【问题描述】:

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

** 初始化.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)"
  }
}

有什么我遗漏的吗?

【问题讨论】:

    标签: python-3.x azure-functions timer-trigger queuetrigger


    【解决方案1】:

    根据一些测试,问题与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)"
      }
    }
    

    函数.json

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

    【讨论】:

    • 在您测试时,这是否拉队列触发了它?它表明它已经成功运行。但是与之前不同,队列或位置队列中没有消息。队列触发器未启动
    • @wwnde 我的队列触发器在定时器触发函数发送消息到队列后触发成功。
    • 您能否在答案中添加您的主机和function.json?
    • 好的,对我来说并不简单,但感谢您的帮助
    • @wwnde 我不确定,您可以先发布问题。如果我帮不上忙,也许其他社区可以帮助你。
    【解决方案2】:

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

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

    See example 来自文档。

    【讨论】:

    • 尝试了这种方法。消息正在写入队列。但是,由于队列被填充,队列触发器没有触发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多