1

I am trying to implement graceful shutdown for a continuous Azure web job, provided the required job settings:

{
  "stopping_wait_time": 3600,
  "is_singleton":  true
}

I have registered the callback:

    static void Main(string[] args)
    {

        var cancellationToken = new WebJobsShutdownWatcher().Token;
        cancellationToken.Register(() =>
        {

            Console.WriteLine("Shutdown Started...");

            Thread.Sleep(5000);
            Console.WriteLine("Waited 5 seconds!" + DateTime.Now.ToString());
            Thread.Sleep(5000);
            Console.WriteLine("Waited 10 seconds!" + DateTime.Now.ToString());
            Thread.Sleep(5000);
            Console.WriteLine("Waited 15 seconds!" + DateTime.Now.ToString());
            Thread.Sleep(5000);
            Console.WriteLine("Waited 20 seconds!" + DateTime.Now.ToString());
            Thread.Sleep(5000);
            Console.WriteLine("Waited 25 seconds!" + DateTime.Now.ToString());
            Thread.Sleep(5000);
            Console.WriteLine("Waited 30 seconds!" + DateTime.Now.ToString());

        });


        JobHost host = new JobHost();

        MethodInfo methodInfo =   typeof(Functions).GetMethod("GracefulShutdown");
        host.CallAsync(methodInfo);     //a long running method      
        host.RunAndBlock();
    }

But on stopping the web job, I can never get the last line of the callback executed:

Console.WriteLine("Waited 30 seconds!" + DateTime.Now.ToString()

Any thoughts?

4

1 回答 1

1

对于触发的 WebJobs,没有关闭通知,但有一个宽限期(默认为 30 秒),WebJob 不会立即强制关闭,宽限期是可配置的。

可以为任何 WebJob 更新宽限期,方法是创建一个名为 settings.job 的文件,其内容如下: { "stopping_wait_time": 60 }

我可以看到您的宽限期仍然是 30 秒,这与默认值相同。请检查您是否将 settings.job 放在正确的位置。您将它放在您的 webjob 可执行文件所在的同一目录中(我假设您通过门户上传了一个 zip 文件?如果是这样,只需将设置文件放在您正在使用的 .exe 或 .cmd 旁边的 zip 中)。

有关详细信息,请参阅http://blog.amitapple.com/post/2014/05/webjobs-graceful-shutdown/#.Vw3HdUcl_dV 。

于 2016-04-13T04:44:32.347 回答