@ValdoG Thanks for getting back. Please note that the CancellationToken
provides a means to cancel general asynchronous processing in .NET.
The Azure Functions has built-in support for CancellationToken
bindings.
Task cancellation | Microsoft Docs
Cancellation tokens - Azure Functions C# developer reference | Microsoft Docs
CancellationToken
, which can be obtained by binding, is embedded in the application lifecycle and can be used to easily detect shutdowns.
public class Function1
{
[FunctionName("Function1")]
public async Task Run([TimerTrigger("0 */5 * * * *")] TimerInfo timer, ILogger log, CancellationToken cancellationToken)
{
try
{
log.LogInformation("Function executing");
// Simulate time-consuming processes
await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
log.LogInformation("Function executed");
}
catch (OperationCanceledException)
{
log.LogWarning("Function cancelled");
}
}
}
Processes that take a long time are simulated with Task.Delay. Note that Task.Delay is passed a CancellationToken.
If you send Ctrl+C to send a shutdown signal while a Function is running, the time-consuming process is canceled and a OperationCanceledException exception is caught, and the log is output.
The same process as when Ctrl+C is sent will be performed when Azure Functions are restarted or deployed, so by handling CancellationToken correctly, you can implement the termination process without breaking the integrity of your application.
Could you please update the code as above and test ? More Info here
Also did you explore the options to use the Durable Functions with terminating instances to check if that serves your requirement ?