如何通过定时器触发器调用 Durable 函数?
How to invoke Durable function by timer trigger?
我是 Durable 函数(Orchestration 函数)的新手,看过 Microsoft 的示例应用程序 documentation.So 我几乎没有怀疑。
示例:
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post",
Route = "orchestrators/{functionName}")] HttpRequestMessage req,
[OrchestrationClient] DurableOrchestrationClient starter,
string functionName,
TraceWriter log)
{
// Function input comes from the request content.
dynamic eventData = await req.Content.ReadAsAsync<object>();
string instanceId = await starter.StartNewAsync(functionName, eventData);
log.Info($"Started orchestration with ID = '{instanceId}'.");
return starter.CreateCheckStatusResponse(req, instanceId);
}
为了调用它,我使用邮递员发出了 HTTP POST 请求,因此请求已成功处理,但是当我配置不同的动词(如 HTTP GET)时,它在控制台中以 NotFound 错误响应,并通过 http 向其发出请求来自浏览器的请求在控制台中响应 "NotFound" 错误。为什么会这样?
我可以在计时器触发器 azure 函数中调用任何编排函数吗?
如果不是为什么?
更新:
关于问题的一些额外细节
[FunctionName("TimerTrigger")]
public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
{//this runs for every 5minutes
using (HttpClient client = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", "")
});
//making request to above function by http trigger
var result = await client.PostAsync("http://localhost:7071/orchestrators/E1_HelloSequence", content);
}
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
return;
}
我可以通过计时器触发对 http 触发器的请求吗,为什么因为我的持久函数有很长的 运行 过程,所以如果在计时器触发器本身中调用编排函数,那么可能会出现计时器触发超时,这就是为什么我我正在尝试遵循这个 approach.Is 可以通过上面的代码调用吗?
when I configured different verb like HTTP GET it was responded with NotFound" error in console as well as request made to it with http request from browser responded with "NotFound" error in console .Why this happened?
因为您指定您的函数仅在 POST 上触发:
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post",
Route = "orchestrators/{functionName}")] HttpRequestMessage req,
要启用 GET,请将 get
添加到 methods
参数。
Can I invoke any Orchestration function with in timer trigger azure function?
您可以使用类似于 HTTP 函数的 OrchestrationClient
输入绑定来定义计时器触发函数。声明样本:
public static async Task Run(
[TimerTrigger("0 */1 * * * *")] TimerInfo info,
[OrchestrationClient] DurableOrchestrationClient starter)
这是一般的 Azure Functions 行为。 GET 不起作用的原因是示例中的函数仅配置为使用 POST。见函数签名中的[HttpTrigger]
属性:
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post",
Route = "orchestrators/{functionName}")]
如果你想支持GET,那么相应地改变methods
参数:
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "get",
Route = "orchestrators/{functionName}")]
请注意 Visual Studio 似乎有一个缓存错误,在本地调试时,对路由信息所做的更改未正确保存。我在这里打开了一个 GitHub 问题来跟踪它:https://github.com/Azure/Azure-Functions/issues/552
有关 HTTP 触发器的详细信息,请参阅此文档:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook
如果您想使用计时器触发器来触发持久函数,则只需更改触发器类型即可。例如:
[FunctionName("ScheduledStart")]
public static async Task RunScheduled(
[TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
[OrchestrationClient] DurableOrchestrationClient starter,
TraceWriter log)
{
string functionName = "E1_HelloSequence";
string instanceId = await starter.StartNewAsync(functionName, null);
log.Info($"Started orchestration with ID = '{instanceId}'.");
}
编辑:如果您使用的是 Durable v2.x,则语法如下所示:
[FunctionName("ScheduledStart")]
public static async Task RunScheduled(
[TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
[DurableClient] IDurableClient starter,
ILogger log)
{
string functionName = "E1_HelloSequence";
string instanceId = await starter.StartNewAsync(functionName, null);
log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
}
有关计时器触发器的更多信息,请参阅此文档:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer
更多信息:
“IDurableClient”是“IDurableEntityClient”和“IDurableOrchastrationClient”的联合体。它具有其他两个的所有方法。
实体方法:
CleanEntityStorageAsync(布尔值、布尔值、CancellationToken)
ListEntitiesAsync(EntityQuery,CancellationToken)
ReadEntityStateAsync(EntityId,字符串,字符串)
SignalEntityAsync(实体 ID、日期时间、字符串、对象、字符串、字符串)
SignalEntityAsync(EntityId、字符串、对象、字符串、字符串)
SignalEntityAsync(EntityId,操作)
SignalEntityAsync(实体 ID、日期时间、操作)
SignalEntityAsync(字符串,动作)
SignalEntityAsync(字符串、日期时间、操作)
编排方法:
CreateCheckStatusResponse(HttpRequest、字符串、布尔值)
CreateCheckStatusResponse(HttpRequestMessage、字符串、布尔值)CreateHttpManagementPayload(字符串)
GetStatusAsync(字符串、布尔值、布尔值、布尔值)ListInstancesAsync(OrchestrationStatusQueryCondition、CancellationToken)
PurgeInstanceHistoryAsync(DateTime, Nullable, IEnumerable)
PurgeInstanceHistoryAsync(字符串)
RaiseEventAsync(字符串,字符串,对象)
RaiseEventAsync(字符串、字符串、字符串、对象、字符串)
RestartAsync(字符串,布尔值)
StartNewAsync(字符串,字符串)
StartNewAsync(字符串,字符串,T)
StartNewAsync(字符串,T)
TerminateAsync(字符串,字符串)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequest、字符串、可为空、可为空、布尔值)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequestMessage、字符串、可为空、可为空、布尔值)
Microsoft 文档提供了一个“永恒的工作”协调器的示例,用于协调需要定期但永远完成的工作:https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?tabs=csharp#periodic-work-example
[FunctionName("EternalWorkOrchestrator")]
public static async Task Run([OrchestrationTrigger] DurableOrchestrationContext context)
{
await context.CallActivityAsync("DoWork", null);
// sleep for one hour between doing work
DateTime nextJobStart = context.CurrentUtcDateTime.AddHours(1);
await context.CreateTimer(nextJobStart, CancellationToken.None);
context.ContinueAsNew(null);
}
我是 Durable 函数(Orchestration 函数)的新手,看过 Microsoft 的示例应用程序 documentation.So 我几乎没有怀疑。
示例:
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post",
Route = "orchestrators/{functionName}")] HttpRequestMessage req,
[OrchestrationClient] DurableOrchestrationClient starter,
string functionName,
TraceWriter log)
{
// Function input comes from the request content.
dynamic eventData = await req.Content.ReadAsAsync<object>();
string instanceId = await starter.StartNewAsync(functionName, eventData);
log.Info($"Started orchestration with ID = '{instanceId}'.");
return starter.CreateCheckStatusResponse(req, instanceId);
}
为了调用它,我使用邮递员发出了 HTTP POST 请求,因此请求已成功处理,但是当我配置不同的动词(如 HTTP GET)时,它在控制台中以 NotFound 错误响应,并通过 http 向其发出请求来自浏览器的请求在控制台中响应 "NotFound" 错误。为什么会这样?
我可以在计时器触发器 azure 函数中调用任何编排函数吗?
如果不是为什么?
更新:
关于问题的一些额外细节
[FunctionName("TimerTrigger")]
public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
{//this runs for every 5minutes
using (HttpClient client = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", "")
});
//making request to above function by http trigger
var result = await client.PostAsync("http://localhost:7071/orchestrators/E1_HelloSequence", content);
}
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
return;
}
我可以通过计时器触发对 http 触发器的请求吗,为什么因为我的持久函数有很长的 运行 过程,所以如果在计时器触发器本身中调用编排函数,那么可能会出现计时器触发超时,这就是为什么我我正在尝试遵循这个 approach.Is 可以通过上面的代码调用吗?
when I configured different verb like HTTP GET it was responded with NotFound" error in console as well as request made to it with http request from browser responded with "NotFound" error in console .Why this happened?
因为您指定您的函数仅在 POST 上触发:
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post",
Route = "orchestrators/{functionName}")] HttpRequestMessage req,
要启用 GET,请将 get
添加到 methods
参数。
Can I invoke any Orchestration function with in timer trigger azure function?
您可以使用类似于 HTTP 函数的 OrchestrationClient
输入绑定来定义计时器触发函数。声明样本:
public static async Task Run(
[TimerTrigger("0 */1 * * * *")] TimerInfo info,
[OrchestrationClient] DurableOrchestrationClient starter)
这是一般的 Azure Functions 行为。 GET 不起作用的原因是示例中的函数仅配置为使用 POST。见函数签名中的[HttpTrigger]
属性:
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post",
Route = "orchestrators/{functionName}")]
如果你想支持GET,那么相应地改变methods
参数:
[HttpTrigger(AuthorizationLevel.Anonymous, methods: "get",
Route = "orchestrators/{functionName}")]
请注意 Visual Studio 似乎有一个缓存错误,在本地调试时,对路由信息所做的更改未正确保存。我在这里打开了一个 GitHub 问题来跟踪它:https://github.com/Azure/Azure-Functions/issues/552
有关 HTTP 触发器的详细信息,请参阅此文档:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook
如果您想使用计时器触发器来触发持久函数,则只需更改触发器类型即可。例如:
[FunctionName("ScheduledStart")]
public static async Task RunScheduled(
[TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
[OrchestrationClient] DurableOrchestrationClient starter,
TraceWriter log)
{
string functionName = "E1_HelloSequence";
string instanceId = await starter.StartNewAsync(functionName, null);
log.Info($"Started orchestration with ID = '{instanceId}'.");
}
编辑:如果您使用的是 Durable v2.x,则语法如下所示:
[FunctionName("ScheduledStart")]
public static async Task RunScheduled(
[TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
[DurableClient] IDurableClient starter,
ILogger log)
{
string functionName = "E1_HelloSequence";
string instanceId = await starter.StartNewAsync(functionName, null);
log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
}
有关计时器触发器的更多信息,请参阅此文档:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer
更多信息:
“IDurableClient”是“IDurableEntityClient”和“IDurableOrchastrationClient”的联合体。它具有其他两个的所有方法。
实体方法:
CleanEntityStorageAsync(布尔值、布尔值、CancellationToken)
ListEntitiesAsync(EntityQuery,CancellationToken)
ReadEntityStateAsync(EntityId,字符串,字符串)
SignalEntityAsync(实体 ID、日期时间、字符串、对象、字符串、字符串)
SignalEntityAsync(EntityId、字符串、对象、字符串、字符串)
SignalEntityAsync(EntityId,操作)
SignalEntityAsync(实体 ID、日期时间、操作)
SignalEntityAsync(字符串,动作)
SignalEntityAsync(字符串、日期时间、操作)
编排方法:
CreateCheckStatusResponse(HttpRequest、字符串、布尔值)
CreateCheckStatusResponse(HttpRequestMessage、字符串、布尔值)CreateHttpManagementPayload(字符串)
GetStatusAsync(字符串、布尔值、布尔值、布尔值)ListInstancesAsync(OrchestrationStatusQueryCondition、CancellationToken)
PurgeInstanceHistoryAsync(DateTime, Nullable, IEnumerable)
PurgeInstanceHistoryAsync(字符串)
RaiseEventAsync(字符串,字符串,对象)
RaiseEventAsync(字符串、字符串、字符串、对象、字符串)
RestartAsync(字符串,布尔值)
StartNewAsync(字符串,字符串)
StartNewAsync(字符串,字符串,T)
StartNewAsync(字符串,T)
TerminateAsync(字符串,字符串)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequest、字符串、可为空、可为空、布尔值)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequestMessage、字符串、可为空、可为空、布尔值)
Microsoft 文档提供了一个“永恒的工作”协调器的示例,用于协调需要定期但永远完成的工作:https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?tabs=csharp#periodic-work-example
[FunctionName("EternalWorkOrchestrator")]
public static async Task Run([OrchestrationTrigger] DurableOrchestrationContext context)
{
await context.CallActivityAsync("DoWork", null);
// sleep for one hour between doing work
DateTime nextJobStart = context.CurrentUtcDateTime.AddHours(1);
await context.CreateTimer(nextJobStart, CancellationToken.None);
context.ContinueAsNew(null);
}