The WebJobs SDK supports passing in a CancellationToken
argument to a function which is used by the SDK to notify running functions of a host shutdown. I have noticed though that running functions are not notified if the app/function is replaced by a new version. This is a problem if functions are designed to run for a longer period of time, since you could end up having "old" logic running without knowing it.
For example, if you create a simple function that will run forever (or until cancelled):
using System;
using System.Threading;
public static async Task Run(string input, TraceWriter log, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
token.ThrowIfCancellationRequested();
log.Info($"Input: {input}");
await Task.Delay(2000, token).ConfigureAwait(false);
}
}
If you run that, and then update the code in some way and redeploy (or save, if you're in the Azure Functions portal), you'll see that the function is recompiled but the running function instance is not stopped and will continue executing the previous version of the code until you disable it manually.
Is there some way of detecting a redeployment inside a running function?