1

i'm new to Azure Webjobs. I was trying to achieve the GraceFul Shutdown. while using the WebJobsShutdownWatcher Class.

Public static void Main()
{
    try
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }

        var watcher = new WebJobsShutdownWatcher();
        Task.Run(() =>
        {
            bool isCancelled = false;
            while (!isCancelled)
           {
               if (watcher.Token.IsCancellationRequested)
               {
                   Console.WriteLine("WebJob cancellation Token Requested!");
                   isCancelled = true;
                }
            }
        }, watcher.Token).Wait();

        var host = new JobHost();
         The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
    catch (Exception)
    {
        Console.WriteLine("Error");
    }
}

To Achieve the GraceFul ShutDown, i have stop the Webjob and again Hosted on azure. After Hosting on Azure, the Queues are not getting Trigger. when i Debug the Code the Control is Stop at the WebJobsShutdownWatcher Class. What did i Done Wrong ?

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
SaTyA
  • 79
  • 1
  • 10
  • You should remove the Wait() on your Task.Run() otherwise the code after is never hit. – Thomas Sep 11 '17 at 22:35
  • You can also see these posts: https://stackoverflow.com/questions/35166010/azure-triggered-webjob-detecting-when-webjob-stops/ https://stackoverflow.com/questions/36760241/intercept-azure-function-host-shutdown-flush-application-insights-telemetryclie – Thomas Sep 11 '17 at 22:37

1 Answers1

2

As Thomas says, there are something wrong with your codes.

Since the WebJobsShutdownWatcher class will continue watch the webjobs status and you use wait method to wait the WebJobsShutdownWatcher class to get the cancel token, the host.RunAndBlock method is never hit.

You could remove the wait method, the codes will work well.

Here I write a test demo, it works well.

    static void Main()
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }

        var watcher = new WebJobsShutdownWatcher();



        Task.Run(() =>
        {
            bool isCancelled = false;
            while (!isCancelled)
            {
                if (watcher.Token.IsCancellationRequested)
                {
                    Console.WriteLine("WebJob cancellation Token Requested!");
                    isCancelled = true;
                }
            }
        }, watcher.Token);


        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }

enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • 1
    This is used only to notify us that the Webjobs are getting ShutDown ? can we pause the Shutdown of webjob so that the Current work can be finish after that the webjob can shutdown. @Brando Zhang – SaTyA Sep 14 '17 at 06:28