I’m working on a Durable Functions setup where I have a timer trigger orchestrating a sequence of activities. However, I’m facing an issue with the logging sequence. Currently, the "Completed" log appears immediately after the timer trigger, but I want it to appear only after the entire orchestration sequence, including all sub-orchestrations, is completed. Below is a simplified version of my code:
[Function("TimerTrigger_EveryMinute")]
public async Task TimerTrigger_EveryMinute(
[TimerTrigger("0 * * * * *")] TimerInfo timerInfo,
[DurableClient] DurableTaskClient starter)
{
_logger.LogError($"Running timer trigger");
string instanceId = await starter.StartNewAsync("MainOrchestrator", null);
// Wait for the entire orchestration to complete
await starter.WaitForInstanceCompletionAsync(instanceId);
_logger.LogError($"Completed timer trigger orchestration with ID = '{instanceId}'");
}
[Function("MainOrchestrator")]
public async Task MainOrchestrator(
[OrchestrationTrigger] DurableOrchestrationContext context)
{
_logger.LogError($"Running MainOrchestrator");
var tasks = new List<Task>();
List<TransactionData> transactions = new List<TransactionData>();
// Call suborchestrator for each ongoing transaction
foreach (var transaction in transactions)
{
tasks.Add(context.CallSubOrchestratorAsync("SubOrchestrator", transaction));
}
await Task.WhenAll(tasks);
}
[Function("SubOrchestrator")]
public async Task SubOrchestrator(
[OrchestrationTrigger] DurableOrchestrationContext context, TransactionData transaction)
{
_logger.LogError($"Running SubOrchestrator");
// other logic here
}
In this setup, the logs are showing "Completed" immediately after the timer trigger logs, but I want it to appear only after all orchestrations, including sub-orchestrations, are completed.
2
Answers
The MainOrchestrator function seems to be invoked multiple times in quick succession, leading to unexpected behavior in my application. I expect it to be invoked only once per timer trigger. Could someone help me understand why this is happening and how I can ensure that the MainOrchestrator function is invoked only once per timer trigger?
I have modified your code a bit and it worked as expected.
.csproj–
I am getting below output.