事件网格触发函数运行成功后一直触发?
Event Grid-triggered Function Keeps Triggering After Function Successfully Runs?
我有一个由事件网格事件触发的 Azure 函数。仅当将 blob 上传到存储帐户时才会创建事件网格事件。
现在已部署并且运行良好,但由于某种原因,即使已成功处理,函数仍会被同一事件触发?
示例:
昨天,8次测试成功;都好:
今天查看日志,函数一直在执行!
错误:“Blob 不存在”
我在昨天上次测试后删除了 blob。为什么事件网格仍在运行?
代码片段:
def main(event: func.EventGridEvent):
result = json.dumps({
'id' : event.id,
'data' : event.get_json(),
'topic' : event.topic,
'subject' : event.subject,
'event_type' : event.event_type
})
logging.info('EventGrid trigger processing an event: %s', result)
credential = DefaultAzureCredential()
download_start_time = datetime.datetime.now()
logging.info(f'######## Starting downloading blob from storage at ' + str(download_start_time) + ' ########')
# =============================
# Download blob from storage container:
# =============================
blob_client = BlobClient.from_blob_url(event.get_json()["url"], credential)
blob_data = blob_client.download_blob().readall()
blob_byte_stream = io.BytesIO(blob_data)
编辑 1:这种情况仍在发生,这次有点不同。
- 现在,事件网格在成功传递消息和函数 运行
后继续触发
如何调试?
我终于弄清楚问题出在哪里...在使用 Azure 存储资源管理器测试 blob 上传时,使用 BlobClient.from_blob_url
方法效果很好。但是在使用 Azure 数据工厂时,使用了不同的 API,并且 EventGrid 消息中的 data.url
属性 不是实际的 blob url(而是包含 dfs
blob
).
奇怪的是,在我向支持团队提出这个问题后不久,一个新的 blobUrl
属性 被添加到 EventGrid data
对象。
在我的代码中,我只是将“url”更改为“blobUrl”,方法就成功了。 (我还改进了 Python 代码的错误处理,以适应将来出现的此类错误。)
Documented EventGrid 消息(截至 2020 年 12 月 10 日):
- 没有
blobUrl
属性
[{
"topic": "/subscriptions/{subscription-id}/resourceGroups/Storage/providers/Microsoft.Storage/storageAccounts/my-storage-account",
"subject": "/blobServices/default/containers/my-file-system/blobs/new-file.txt",
"eventType": "Microsoft.Storage.BlobCreated",
"eventTime": "2017-06-26T18:41:00.9584103Z",
"id": "831e1650-001e-001b-66ab-eeb76e069631",
"data": {
"api": "CreateFile",
"clientRequestId": "6d79dbfb-0e37-4fc4-981f-442c9ca65760",
"requestId": "831e1650-001e-001b-66ab-eeb76e000000",
"eTag": "\"0x8D4BCC2E4835CD0\"",
"contentType": "text/plain",
"contentLength": 0,
"contentOffset": 0,
"blobType": "BlockBlob",
"url": "https://my-storage-account.dfs.core.windows.net/my-file-system/new-file.txt",
"sequencer": "00000000000004420000000000028963",
"storageDiagnostics": {
"batchId": "b68529f3-68cd-4744-baa4-3c0498ec19f0"
}
},
"dataVersion": "2",
"metadataVersion": "1"
}]
现在通过的实际 EventGrid 消息:
- 通知
blobUrl
已添加到架构
{
"id": "long-string",
"data": {
"api": "CreateFile",
"requestId": "long-string",
"eTag": "0x8D89B4FF7150079",
"contentType": "application/octet-stream",
"contentLength": 0,
"contentOffset": 0,
"blobType": "BlockBlob",
"blobProperties": [{
"acl": [{
"access": "u::rw,u:long-string:rwx,u:long-string:rwx,g::rx,g:long-string:rx,m::rw,o::",
"permission": "0660",
"owner": "long-string",
"group": "$superuser"
}]
}],
"blobUrl": "https://myfunction.blob.core.windows.net/container/20201208/730420201208080239.csv",
"url": "https://myfunction.dfs.core.windows.net/container/20201208/730420201208080239.csv",
"sequencer": "0000000000000000000000000000692c00000000000e1a99",
"identity": "long-string",
"storageDiagnostics": {
"batchId": "long-string"
}
},
"topic": "/subscriptions/long-string/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/storageAccount",
"subject": "/blobServices/default/containers/container/blobs/20201208/730420201208080239.csv",
"event_type": "Microsoft.Storage.BlobCreated"
}
这里还有一个警告...请注意上面消息中的 Content-Length
CreateFile
API 不是指示 blob 已创建的实际消息。
FlushWithClose
API是
文档中有关于此的注释。所以我还必须设置一个 EventGrid 高级过滤器,它仅在生成 FlushWithClose
个事件时触发 (!)
我有一个由事件网格事件触发的 Azure 函数。仅当将 blob 上传到存储帐户时才会创建事件网格事件。
现在已部署并且运行良好,但由于某种原因,即使已成功处理,函数仍会被同一事件触发?
示例:
昨天,8次测试成功;都好:
今天查看日志,函数一直在执行!
错误:“Blob 不存在”
我在昨天上次测试后删除了 blob。为什么事件网格仍在运行?
代码片段:
def main(event: func.EventGridEvent):
result = json.dumps({
'id' : event.id,
'data' : event.get_json(),
'topic' : event.topic,
'subject' : event.subject,
'event_type' : event.event_type
})
logging.info('EventGrid trigger processing an event: %s', result)
credential = DefaultAzureCredential()
download_start_time = datetime.datetime.now()
logging.info(f'######## Starting downloading blob from storage at ' + str(download_start_time) + ' ########')
# =============================
# Download blob from storage container:
# =============================
blob_client = BlobClient.from_blob_url(event.get_json()["url"], credential)
blob_data = blob_client.download_blob().readall()
blob_byte_stream = io.BytesIO(blob_data)
编辑 1:这种情况仍在发生,这次有点不同。
- 现在,事件网格在成功传递消息和函数 运行 后继续触发
如何调试?
我终于弄清楚问题出在哪里...在使用 Azure 存储资源管理器测试 blob 上传时,使用 BlobClient.from_blob_url
方法效果很好。但是在使用 Azure 数据工厂时,使用了不同的 API,并且 EventGrid 消息中的 data.url
属性 不是实际的 blob url(而是包含 dfs
blob
).
奇怪的是,在我向支持团队提出这个问题后不久,一个新的 blobUrl
属性 被添加到 EventGrid data
对象。
在我的代码中,我只是将“url”更改为“blobUrl”,方法就成功了。 (我还改进了 Python 代码的错误处理,以适应将来出现的此类错误。)
Documented EventGrid 消息(截至 2020 年 12 月 10 日):
- 没有
blobUrl
属性
[{
"topic": "/subscriptions/{subscription-id}/resourceGroups/Storage/providers/Microsoft.Storage/storageAccounts/my-storage-account",
"subject": "/blobServices/default/containers/my-file-system/blobs/new-file.txt",
"eventType": "Microsoft.Storage.BlobCreated",
"eventTime": "2017-06-26T18:41:00.9584103Z",
"id": "831e1650-001e-001b-66ab-eeb76e069631",
"data": {
"api": "CreateFile",
"clientRequestId": "6d79dbfb-0e37-4fc4-981f-442c9ca65760",
"requestId": "831e1650-001e-001b-66ab-eeb76e000000",
"eTag": "\"0x8D4BCC2E4835CD0\"",
"contentType": "text/plain",
"contentLength": 0,
"contentOffset": 0,
"blobType": "BlockBlob",
"url": "https://my-storage-account.dfs.core.windows.net/my-file-system/new-file.txt",
"sequencer": "00000000000004420000000000028963",
"storageDiagnostics": {
"batchId": "b68529f3-68cd-4744-baa4-3c0498ec19f0"
}
},
"dataVersion": "2",
"metadataVersion": "1"
}]
现在通过的实际 EventGrid 消息:
- 通知
blobUrl
已添加到架构
{
"id": "long-string",
"data": {
"api": "CreateFile",
"requestId": "long-string",
"eTag": "0x8D89B4FF7150079",
"contentType": "application/octet-stream",
"contentLength": 0,
"contentOffset": 0,
"blobType": "BlockBlob",
"blobProperties": [{
"acl": [{
"access": "u::rw,u:long-string:rwx,u:long-string:rwx,g::rx,g:long-string:rx,m::rw,o::",
"permission": "0660",
"owner": "long-string",
"group": "$superuser"
}]
}],
"blobUrl": "https://myfunction.blob.core.windows.net/container/20201208/730420201208080239.csv",
"url": "https://myfunction.dfs.core.windows.net/container/20201208/730420201208080239.csv",
"sequencer": "0000000000000000000000000000692c00000000000e1a99",
"identity": "long-string",
"storageDiagnostics": {
"batchId": "long-string"
}
},
"topic": "/subscriptions/long-string/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/storageAccount",
"subject": "/blobServices/default/containers/container/blobs/20201208/730420201208080239.csv",
"event_type": "Microsoft.Storage.BlobCreated"
}
这里还有一个警告...请注意上面消息中的 Content-Length
CreateFile
API 不是指示 blob 已创建的实际消息。FlushWithClose
API是
文档中有关于此的注释。所以我还必须设置一个 EventGrid 高级过滤器,它仅在生成 FlushWithClose
个事件时触发 (!)