I am running an existing c# .net app as a triggered(OnDemand, not Continuous with queues/triggers/functions) webjob. Sometimes it is long running, like 30+ minutes. Works great.
[Edited] If I try to stop the webjob, by setting WEBJOBS_STOPPED to 1 in the Application Settings of my App Services, then the webjob will be Aborted by Azure/Kudu if it does not gracefully detect the shutdown and terminate itself in time (5 seconds? 30 seconds?)
I have tried using WebJobsShutdownWatcher() as several examples illustrate, and as Joey indicated, but it STILL does not detect the shutdown, and Azure/Kudu aborts it anyway. I have even manually coded the checking for the existence of the shutdown file (see WebJobsShutdownWatcher() code in GitHub), but that file does not appear to be created for my triggered/OnDemand webjob, so my code detects nothing.
My original question was: Does WebJobsShutdownWatcher() work for Triggered(OnDemand) webjobs? Or only for Continuous webjobs? I suspected WebJobsShutdownWatcher only works for Continuous webjobs, so I wanted to ask here before I spend more time trying to debug it.
I thought that Joey's answer worked, but I fooled myself because it appeared that the console app terminated because of the shutdown, but I now think it terminated because I had not coded it correctly. Anyway, even with the .Register() or checking .Token.IsCancellationRequested, my console app does not appear to get notified, so Azure/Kudu aborts it once I set WEBJOBS_STOPPED to 1.
So I am beginning to think perhaps there is other Azure code I must add to my simple console app, I dunno, like like JobHost initialization, and host.RunAndBlock(), or something like that, for Azure to do it's thing and fire the anonymous function I register with .Register, or set the .Token.IsCancellationRequested flag.
I have included my entire simple test program, all suggestions welcome, though I will say I'm hoping for minimal changes to my vanilla .NET console app, I don't really WANT to create any more stuff than I have to, like JobHost(), or have triggered functions that I have to configure in Azure to fire. Just a simple console app, with a settings.job with a cron schedule, that runs and stops, runs and stops, and if it detects a webjob shutdown, terminates early to avoid the Azure/Kudu Abort.
class Program
{
static Boolean runFlag1 = true;
static Boolean runFlag2 = true;
static void Main(string[] args)
{
var watcher = new WebJobsShutdownWatcher();
var cancellationToken = watcher.Token;
cancellationToken.Register(() =>
{
Console.WriteLine("Webjob Shutdown signaled.");
runFlag1 = false;
});
var t = Task.Run(() =>
{
Console.WriteLine("Beginning webjob loop...");
while (runFlag1 && runFlag2)
{
Thread.Sleep(1000);
Thread.Yield();
if (watcher.Token.IsCancellationRequested)
{
Console.WriteLine("");
Console.WriteLine("Webjob IsCancellationRequested=true");
runFlag2 = false;
}
}
Console.WriteLine("");
Console.WriteLine("Ending webjob loop, runFlag1={0} runFlag2={1}", runFlag1, runFlag2);
});
t.Wait();
Console.WriteLine("Exited Task. runFlag1={0} runFlag2={1}", runFlag1, runFlag2);
}
}
Thanks, Bo