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

2024-02-03

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

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

主机.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')

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

主机.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 * * * * *"
    }
  ]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

定时器触发不会触发队列但手动录入会触发-Python 的相关文章

随机推荐