I have a long-running timer-based Azure function (yes, not good form for a long-running function, but...) The function timeout has been configured for 5 minutes in host.json, and the schedule starts the task every five minutes during a "run window" (i.e., 7PM to 8PM).
Since the function could time out, I've implemented cancellation by periodically checking with ThrowIfCancellationRequested - the catch logs that the function is being cancelled and re-throws.
What I have found is that the first invocation runs as expected. When the timeout occurs, the framework notifies that the timeout has been exceeded, that cancellation has been initiated, and that the function failed.
- 2024-09-27T22:20:00.108 [Information] Executing 'StatementTask' (Reason='Timer fired at 2024-09-27T15:20:00.0952840-07:00', Id=1ff7645f-c2ed-4978-bed4-2458d94da172)
- 2024-09-27T22:20:00.173 [Information] StatementTask started at: 9/27/2024 3:20:00 PM
- ... <log messages during execution>
- 2024-09-27T22:25:00.160 [Error] Timeout value of 00:05:00 exceeded by function 'StatementTask' (Id: '1ff7645f-c2ed-4978-bed4-2458d94da172'). Initiating cancellation.
- 2024-09-27T22:25:00.181 [Error] Executed 'StatementTask' (Failed, Id=1ff7645f-c2ed-4978-bed4-2458d94da172, Duration=300082ms)Timeout value of 00:05:00 was exceeded by function: StatementTask
The timer then starts the next invocation of the task, but it immediately is terminated - it's like the cancellation token was the same between the two invocation!
- 2024-09-27T22:25:00.223 [Information] Executing 'StatementTask' (Reason='Timer fired at 2024-09-27T15:25:00.2224013-07:00', Id=4ca19cf2-446a-4f8c-8ac8-8f5d42e61378)
- 2024-09-27T22:25:00.223 [Information] StatementTask started at: 9/27/2024 3:25:00 PM
- 2024-09-27T22:25:00.297 [Warning] 4ca19cf2-446a-4f8c-8ac8-8f5d42e61378 - StatementTask was cancelled - terminating
- 2024-09-27T22:25:00.303 [Error] Executed 'StatementTask' (Failed, Id=4ca19cf2-446a-4f8c-8ac8-8f5d42e61378, Duration=81ms)System.OperationCanceledException : The operation was canceled.
After this, the timer kicks off the invocation at the scheduled time, and the cycle repeats.
Any suggestions on how to prevent the invocation immediately after the timeout from being cancelled as it starts?