如何通过计时器触发器调用Durable函数?

时间:2017-10-17 13:49:29

标签: azure azure-functions serverless-architecture serverless

我是Durable函数(Orchestration函数)的新手,并且根据Microsoft文档看过示例应用程序。所以我很少有疑问。

示例:

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

调用它我使用postman发出HTTP POST请求所以请求处理成功但是当我配置不同的动词如HTTP GET时,它被NotFound响应“在控制台中出错”以及使用来自浏览器的http请求向它发出的请求响应“控制台中出现“NotFound”错误。为什么会发生这种情况?

我可以使用计时器触发器azure函数调用任何Orchestration函数吗?

如果不是为什么?

更新

有关问题的一些其他详细信息

    [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触发器的请求吗为什么因为我的持久性函数有长时间运行的进程所以如果在计时器中调用orchestration函数触发器本身因此可能有定时器触发超时的原因所以我为什么要尝试遵循这种方法。可以通过上面的代码调用吗?

3 个答案:

答案 0 :(得分:2)

  

当我配置不同的动词如HTTP GET时,它在控制台中响应了NotFound“错误以及向浏览器发出的http请求发出的请求在控制台中响应”NotFound“错误。为什么会发生这种情况?

因为您指定的功能仅在POST上触发:

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

要启用GET,请将get添加到methods参数。

  

我可以使用计时器触发器azure函数调用任何Orchestration函数吗?

您可以使用类似于HTTP功能的OrchestrationClient输入绑定来定义定时器触发的函数。样品声明:

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

答案 1 :(得分:2)

这是一般的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}'.");
}

有关计时器触发器的更多信息,请参阅此文档:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer

答案 2 :(得分:0)

更多信息:

“IDurableClient”是“IDurableEntityClient”和“IDurableOrchastrationClient”的联合。它具有其他两个的所有方法。

实体方法:

CleanEntityStorageAsync(Boolean, Boolean, CancellationToken)
ListEntitiesAsync(EntityQuery, CancellationToken)
ReadEntityStateAsync(EntityId, String, String)
SignalEntityAsync(EntityId, DateTime, String, Object, String, String)
SignalEntityAsync(EntityId, String, Object, String, String)
SignalEntityAsync(EntityId, Action)
SignalEntityAsync(EntityId, DateTime, Action)
SignalEntityAsync(String, Action)
SignalEntityAsync(String, DateTime, Action)

编排方法:

CreateCheckStatusResponse(HttpRequest, String, Boolean)
CreateCheckStatusResponse(HttpRequestMessage, String, Boolean) CreateHttpManagementPayload(String)
GetStatusAsync(String, Boolean, Boolean, Boolean) ListInstancesAsync(OrchestrationStatusQueryCondition, CancellationToken)
PurgeInstanceHistoryAsync(DateTime, Nullable, IEnumerable)
PurgeInstanceHistoryAsync(String)
RaiseEventAsync(String, String, Object)
RaiseEventAsync(String, String, String, Object, String)
RestartAsync(String, Boolean)
StartNewAsync(String, String)
StartNewAsync(String, String, T)
StartNewAsync(String, T)
TerminateAsync(String, String)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequest, 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