我有一个带有Web挂钩活动的Azure数据工厂V2,该活动用于通过HTTP触发器调用Azure函数,代码如下。
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Interact with query parameters or the body of the request.
$callBackUri = $Request.Body.callBackUri
Write-Output "CallBack url is : $callBackUri"
# Need to return Http 202 Accepted here
# This is the issue, it does not actually return from this point at the moment
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::Accepted
Body = $body
}) -Clobber
# some long running processing
$seconds = 60
Write-Output "Returned http 202. Sleeping for $seconds seconds.."
Start-Sleep -Second $seconds
Write-Output "Sleep complete."
# Invoke the callback
Invoke-WebRequest -Uri $callBackUri -Method POST -ContentType 'application/json' -Body "This is a callback"
该功能应该接收HTTP请求,立即返回HTTP 202 Accepted响应,然后继续处理。该过程完成后,该功能需要在callBackUri上调用POST,以向Web挂钩活动指示处理已完成。
但是,该函数不会返回202,而是会完成它的长时间运行过程,然后返回203。我知道最初已设置输出绑定,并且仅在执行整个脚本后才返回。
有没有解决的办法?我只是想实现此目的:https://mrpaulandrew.com/2019/06/18/azure-data-factory-web-hook-vs-web-activity/
答案 0 :(得分:0)
我尝试了, 开始作业 , 开始线程作业 , 调用了具有所有变体的命令 ,尝试调用异步REST请求,而无需等待功能应用程序(触发并忘记),但是失败。
在我看来这是合乎逻辑的,因为如果允许这样做,人们将只在后台线程上运行所有内容而无需等待它们完成,这将违背Function app-serverless的目的。
我解决的方法是使用相同的 Invoke-RestRequest 并在1秒钟的超时时间内尝试尝试抑制超时异常。这导致请求立即超时,Function应用程序将完成。
代码如下:
$Body = @{
callbackUri = $Request.Body.callBackUri;
} | ConvertTo-Json
try{
# This API will be responsible for issuing the call back after it has finished the long running process
$output = Invoke-RestMethod -Method Post -Body $Body -Uri $funcapp2Url -ContentType 'application/json' -TimeoutSec 1
}
catch {
Write-Output $_
}
# Return HTTP 202 immediately
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::Accepted
Body = "Wait for callback"
}) -Clobber