【问题标题】:GraceFul ShutDown in Azure Webjob in C#C# 中 Azure Webjob 中的 GraceFul 关闭
【发布时间】:2017-09-11 13:36:09
【问题描述】:

我是 Azure Webjobs 的新手。我试图实现 GraceFul 关机。在使用 WebJobsShutdownWatcher 类时。

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");
    }
}

为了实现 GraceFul 关闭,我已停止 Webjob 并再次托管在 azure 上。 在 Azure 上托管后,队列没有触发。当我调试代码时,控件在 WebJobsShutdownWatcher 类处停止。 我做错了什么?

【问题讨论】:

标签: c# azure queue azure-webjobs


【解决方案1】:

正如 Thomas 所说,您的代码有问题。

由于 WebJobsShutdownWatcher 类将继续监视 webjobs 状态,并且您使用 wait 方法等待 WebJobsShutdownWatcher 类获取取消令牌,因此永远不会命中 host.RunAndBlock 方法。

您可以删除等待方法,代码将正常工作。

我这里写了一个测试demo,效果不错。

    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();
    }

【讨论】:

  • 这仅用于通知我们 Webjobs 正在关闭?我们可以暂停 webjob 的关闭,以便在 webjob 可以关闭之后完成当前工作。 @白兰度张
猜你喜欢
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多