简体   繁体   English

如何通过定时器触发器调用 Durable 功能?

[英]How to invoke Durable function by timer trigger?

I am new to Durable function(Orchestration function) and seen sample application as per Microsoft documentation.So I have few doubts.我是 Durable 功能(编排功能)的新手,并且按照 Microsoft 文档看过示例应用程序。所以我几乎没有怀疑。

example:例子:

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);
        } 

to invoke it I made HTTP POST request using postman so request processed successfully but 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?为了调用它,我使用邮递员发出了 HTTP POST 请求,因此请求处理成功,但是当我配置不同的动词(如 HTTP GET)时,它在控制台中以 NotFound”错误响应,以及使用来自浏览器的 http 请求向它发出的请求以“NotFound”错误响应在控制台中。为什么会这样?

Can I invoke any Orchestration function with in timer trigger azure function?我可以使用计时器触发器天蓝色函数调用任何编排函数吗?

If not why?如果不是为什么?

UPDATE:更新:

Some additional details about question关于问题的一些额外细节

    [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;
            }

can I make request to http trigger by timer triggred why because my durable function has long running process so if invoke orchestration function in timer trigger itself so there might be possibility of timer triggered timeout so that why I am trying to follow this approach.Is it possible to invoke by above code?我可以通过定时器触发请求 http 触发器为什么因为我的持久函数有很长的运行过程所以如果在定时器触发器本身中调用编排函数所以可能有定时器触发超时的可能性,所以我试图遵循这种方法。是吗可以通过上面的代码调用吗?

This is general Azure Functions behavior.这是一般的 Azure Functions 行为。 The reason GET doesn't work is because the function in the sample is only configured to work with POST. GET 不起作用的原因是示例中的函数仅配置为与 POST 一起使用。 See the [HttpTrigger] attribute in the function signature:见函数签名中的[HttpTrigger]属性:

[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", 
        Route = "orchestrators/{functionName}")]

If you want to support GET, then change the methods parameter accordingly:如果要支持 GET,则相应地更改methods参数:

[HttpTrigger(AuthorizationLevel.Anonymous, methods: "get", 
        Route = "orchestrators/{functionName}")]

Note that Visual Studio seems to have a caching bug where making changes to route information is not properly saved when debugging locally.请注意,Visual Studio 似乎有一个缓存错误,在本地调试时无法正确保存对路由信息的更改。 I opened a GitHub issue to track that here: https://github.com/Azure/Azure-Functions/issues/552我在这里打开了一个 GitHub 问题来跟踪它: https : //github.com/Azure/Azure-Functions/issues/552

For more information on HTTP triggers, see this documentation: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook有关 HTTP 触发器的详细信息,请参阅此文档: https : //docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook

If you want to trigger a Durable Function using a timer trigger, then just change the trigger type.如果您想使用定时器触发器来触发 Durable Function,那么只需更改触发器类型即可。 For example:例如:

[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}'.");
}

EDIT: If you're using Durable v2.x, then the syntax looks like this:编辑:如果您使用的是 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}'.");
}

For more information on Timer triggers, see this documentation: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer有关计时器触发器的详细信息,请参阅此文档: https : //docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer

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?当我配置不同的动词(如 HTTP GET)时,它在控制台中以 NotFound” 错误响应,以及使用来自浏览器的 http 请求向它发出的请求在控制台中以“NotFound”错误响应。为什么会发生这种情况?

Because you specified your function to be triggered on POST only:因为您指定了仅在 POST 上触发的函数:

[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", 
Route = "orchestrators/{functionName}")] HttpRequestMessage req,

To enable GET, add get to methods parameter.要启用 GET,请将get添加到methods参数。

Can I invoke any Orchestration function with in timer trigger azure function?我可以使用计时器触发器天蓝色函数调用任何编排函数吗?

You can define timer-triggered function with OrchestrationClient input binding similar to your HTTP function.您可以使用类似于 HTTP 函数的OrchestrationClient输入绑定来定义计时器触发的函数。 Sample declaration:样本声明:

public static async Task Run(
    [TimerTrigger("0 */1 * * * *")] TimerInfo info,
    [OrchestrationClient] DurableOrchestrationClient starter)

More info:更多信息:

"IDurableClient" is a union of "IDurableEntityClient" and "IDurableOrchastrationClient". “IDurableClient”是“IDurableEntityClient”和“IDurableOrchastrationClient”的并集。 It has all the methods of the other two.它具有其他两个的所有方法。

Entity Methods:实体方法:

CleanEntityStorageAsync(Boolean, Boolean, CancellationToken) CleanEntityStorageAsync(Boolean, Boolean, CancellationToken)
ListEntitiesAsync(EntityQuery, CancellationToken) ListEntitiesAsync(EntityQuery, CancellationToken)
ReadEntityStateAsync(EntityId, String, String) ReadEntityStateAsync(EntityId, String, String)
SignalEntityAsync(EntityId, DateTime, String, Object, String, String) SignalEntityAsync(EntityId, DateTime, String, Object, String, String)
SignalEntityAsync(EntityId, String, Object, String, String) SignalEntityAsync(EntityId, String, Object, String, String)
SignalEntityAsync(EntityId, Action) SignalEntityAsync(EntityId, Action)
SignalEntityAsync(EntityId, DateTime, Action) SignalEntityAsync(EntityId, DateTime, Action)
SignalEntityAsync(String, Action) SignalEntityAsync(字符串,动作)
SignalEntityAsync(String, DateTime, Action) SignalEntityAsync(字符串,日期时间,操作)

Orchestration Methods:编排方法:

CreateCheckStatusResponse(HttpRequest, String, Boolean) CreateCheckStatusResponse(HttpRequest, String, Boolean)
CreateCheckStatusResponse(HttpRequestMessage, String, Boolean) CreateHttpManagementPayload(String) CreateCheckStatusResponse(HttpRequestMessage, String, Boolean) CreateHttpManagementPayload(String)
GetStatusAsync(String, Boolean, Boolean, Boolean) ListInstancesAsync(OrchestrationStatusQueryCondition, CancellationToken) GetStatusAsync(String, Boolean, Boolean, Boolean) ListInstancesAsync(OrchestrationStatusQueryCondition, CancellationToken)
PurgeInstanceHistoryAsync(DateTime, Nullable, IEnumerable) PurgeInstanceHistoryAsync(DateTime, Nullable, IEnumerable)
PurgeInstanceHistoryAsync(String) PurgeInstanceHistoryAsync(字符串)
RaiseEventAsync(String, String, Object) RaiseEventAsync(字符串,字符串,对象)
RaiseEventAsync(String, String, String, Object, String) RaiseEventAsync(字符串,字符串,字符串,对象,字符串)
RestartAsync(String, Boolean) RestartAsync(字符串,布尔值)
StartNewAsync(String, String) StartNewAsync(字符串,字符串)
StartNewAsync(String, String, T) StartNewAsync(字符串,字符串,T)
StartNewAsync(String, T) StartNewAsync(String, T)
TerminateAsync(String, String) TerminateAsync(字符串,字符串)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequest, String, Nullable, Nullable, Boolean) WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequest, String, Nullable, Nullable, Boolean)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequestMessage, String, Nullable, Nullable, Boolean) WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequestMessage, String, Nullable, Nullable, Boolean)

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.webjobs.extensions.durabletask?view=azure-dotnet https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.webjobs.extensions.durabletask?view=azure-dotnet

The Microsoft Docs provide an example for an "eternal work" orchestrator to orchestrate work that needs to be done periodically but forever: https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-eternal-orchestrations?tabs=csharp#periodic-work-example Microsoft Docs 为“永恒工作”编排器提供了一个示例,用于编排需要定期但永远完成的工作: 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 azure 持久函数中取消正在运行的触发函数? - How to cancel a running trigger function within azure durable functions? 没有 HttpTrigger 的 Azure 持久函数调用(自动启动) - Azure Durable Function Invoke without HttpTrigger (Autostart) 如何将POST参数传递给持久函数,然后将此参数传递给计时器触发的函数 - How to pass a POST parameter to a Durable Function and then pass this param to a Timer Triggered function 如何调用 Azure HTTP 触发器函数 - How to invoke an Azure HTTP trigger function Azure function 定时器触发器 - Azure function timer trigger Durable Azure Function Orchestrator 是否会释放计时器锁定? - Does Durable Azure Function Orchestrator release lock on timer? Azure 持久函数编排触发器不会触发 - Azure Durable Function Orchestration Trigger Doesn't Fire Azure Durable Function:通过将复杂对象从触发器传递到协调器来实现 JsonSerializationException - Azure Durable Function: JsonSerializationException by passing complex object from trigger to orchestrator 如何为 python 中的多个并行运行 Azure 持久功能设置计时器? - How set timer for multiple parallel running Azure durable functions in python? Azure 使用定时器触发器获取和设置时间戳信息的持久实体函数 - Azure Durable entity functions to get & set timestamp info using timer trigger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM