skip to Main Content
[Function("TimerTrigger_EveryMinute")]
public async Task TimerTrigger_EveryMinute(
[TimerTrigger("0 * * * * *")] TimerInfo timerInfo,
[DurableClient] DurableTaskClient starter)
{
    _logger.LogError($"Running timer trigger");

    string instanceId = await starter.ScheduleNewOrchestrationInstanceAsync("ORCHESTRATOR_TRIGGER_NAME");
    await starter.WaitForInstanceCompletionAsync(instanceId);

    _logger.LogInformation($"Completed orchestration with ID = '{instanceId}'");
}


 [Function("ORCHESTRATOR_TRIGGER_NAME")]
 public async Task Run(
     [OrchestrationTrigger] TaskOrchestrationContext context)
 {
    //long running function

     await Task.WhenAll(response);
 }

**what i need:-

1)after calling Orchestration i need timeout at 30sec,so after timeout it should end/exit.
or
2)is there any way we can do timeout for this WaitForInstanceCompletionAsync().**

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for your answer. What if there are more than one ActivityTrigger,will it work?. I provided more code below.

    [Function("TimerTrigger_EveryMinute")]
    public async Task TimerTrigger_EveryMinute(
    [TimerTrigger("0 * * * * *")] TimerInfo timerInfo,
    [DurableClient] DurableTaskClient starter)
    {
        _logger.LogError($"Running timer trigger");
    
        string instanceId = await starter.ScheduleNewOrchestrationInstanceAsync("ORCHESTRATOR_TRIGGER_NAME");
        await starter.WaitForInstanceCompletionAsync(instanceId);
    
        _logger.LogInformation($"Completed orchestration with ID = '{instanceId}'");
    }
    
    
     [Function("ORCHESTRATOR_TRIGGER_NAME")]
     public async Task Run(
         [OrchestrationTrigger] TaskOrchestrationContext context)
     {
        var parallelTasks = new List<Task<int>>();
    
        for (int i = 0; i < 5; i++)
        {
            Task<int> task = context.CallActivityAsync<int>("getdata", i);
            parallelTasks.Add(task);
        }
    
        await Task.WhenAll(parallelTasks);
     }
    

  2. After calling the Orchestration function, you can set the timeout to 30 seconds in the following way-

    [Function("ORCHESTRATOR_TRIGGER_NAME")]
    public static async Task<bool> Run(
        [OrchestrationTrigger] TaskOrchestrationContext context)
    {
        TimeSpan timeout = TimeSpan.FromSeconds(30);
        DateTime deadline = context.CurrentUtcDateTime.Add(timeout);
    
        using (var cts = new CancellationTokenSource())
        {
            Task activityTask = context.CallActivityAsync(your activity function);
            Task timeoutTask = context.CreateTimer(deadline, cts.Token);
    
            Task winner = await Task.WhenAny(activityTask, timeoutTask);
            if (winner == activityTask)
            {
                // success case
                cts.Cancel();
                return true;
            }
            else
            {
                // timeout case
                return false;
            }
        }
    

    You can refer to this ms docs for more details.

    You can also set the timeout at function level by adding { "functionTimeout": "00:00:30" } in the host.json file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search