【问题标题】:Continuous WebJob stopping_wait_time ignored?连续 WebJob 停止等待时间被忽略?
【发布时间】:2015-12-31 07:53:16
【问题描述】:

在我昨天的问题 (Continuous WebJobs and CancellationToken) 之后,我想现在我已经了解了连续 WebJobs 和正常关闭的处理并进行了一些测试。

其他信息

https://myWebJob.scm.azurewebsites.net/api/continuouswebjobs 我得到:

{
    [some info ommited...]
    "type": "continuous",
    "error": null,
    "using_sdk": true,
    "settings": {
        "stopping_wait_time": 120
    }
}

在 2015 年 12 月 31 日星期四 07:00:23 GMT,我停止了服务器,仪表板中的以下条目是:

[12/31/2015 07:00:23 > d71f29: SYS INFO] 状态更改为禁用 [12/31/2015 07:00:28 > d71f29: SYS INFO] 检测到的 WebJob 文件/s 已更新,正在刷新 WebJob [12/31/2015 07:00:28 > d71f29: SYS INFO] 状态更改为停止 [12/31/2015 07:00:30 > d71f29: INFO] 作业主机已停止 [12/31/2015 07:00:32 > d71f29: SYS INFO] 状态更改为成功 [12/31/2015 07:00:32 > d71f29: SYS INFO] 状态更改为已停止

我的跑步工作立即结束,没有任何进一步的信息。 我的函数的最后两行代码是记录到仪表板并将条目写入存储表。这些条目都没有写...我已经重试了几次,每次都在 5 秒内停止作业主机。所以:

问题

对于连续的 WebJobs,是否会优雅地忽略“stopping_wait_time”值?

【问题讨论】:

    标签: .net azure azure-webjobs azure-webjobssdk


    【解决方案1】:

    stopping_wait_time 设置用于连续作业和触发作业。您可以看到 Continuous 作业运行器如何使用此设置 here in the source code

    我上传了一个工作示例,展示了正常关机here。在该示例中,我将关闭超时设置为 60 秒,并验证我的作业功能是否能够执行 30 秒的关闭活动而不会被杀死。

    请下载该示例并尝试一下。您还可以按照上面链接的示例自述文件中的详细说明在本地运行示例。我还在此处内联了示例函数,展示了如何使用 CancellationToken:

    public static class Functions
    {
        [NoAutomaticTrigger]
        public static async Task GracefulShutdown(CancellationToken cancellationToken)
        {
            // Simulate a long running function, passing in the CancellationToken to
            // all async operations we initiate. 
            // We can also monitor CancellationToken.IsCancellationRequested in our code
            // if our code is iterative.
            try
            {
                await Task.Delay(TimeSpan.FromMinutes(10), cancellationToken);
            }
            catch (TaskCanceledException)
            {
                Shutdown().Wait();
            }
    
            Console.WriteLine("Function completed succesfully.");        
        }
    
        private static async Task Shutdown()
        {
            // simulate some cleanup work to show that the Continuous job host
            // will wait for the time configured via stopping_wait_time before
            // killing the process. The default value for stopping_wait_time is 5 seconds,
            // so this code will demonstrate that our override in settings.job
            // is functional.
            Console.WriteLine("Function has been cancelled. Performing cleanup ...");
            await Task.Delay(TimeSpan.FromSeconds(30));
    
            Console.WriteLine("Function was cancelled and has terminated gracefully.");
        }
    

    该函数30秒的优雅关机逻辑运行成功,如控制台输出所示:

    • [12/31/2015 17:15:08 > 951460: SYS INFO] 状态更改为正在停止
    • [12/31/2015 17:15:09 > 951460: INFO] 功能已取消。执行清理...
    • [12/31/2015 17:15:39 > 951460: INFO] 函数已取消并正常终止。
    • [12/31/2015 17:15:39 > 951460: INFO] 功能成功完成。
    • [12/31/2015 17:15:39 > 951460: INFO] 作业主机已停止
    • [12/31/2015 17:15:39 > 951460: SYS INFO] 状态更改为成功
    • [12/31/2015 17:15:39 > 951460: SYS INFO] 状态更改为已停止

    【讨论】:

    • 抱歉劫持了另一个用户的问题,但我无法让它工作。即使在 VS 项目中将 settings.job 文件标记为“始终复制”,我总是会延迟 5 秒,并且我可以看到该文件已被复制到 WebJob 的根文件夹(与 . exe文件)。 Web 应用程序设置为“始终开启”,WebJob 是连续的,但查看xxx.scm.azurewebsites.net/api/continuouswebjobs 显示“设置”的属性为空。知道可能出了什么问题吗?
    【解决方案2】:

    2019 年 3 月 19 日编辑

    可以通过在settings.job 中设置stopping_wait_time 值来配置该设置。在幕后,JobSettings.GetStoppingWaitTime 方法将根据该设置检索TimeSpan如果设置中不存在 5 秒的默认值。

    public TimeSpan GetStoppingWaitTime(long defaultTime)
    {
        return TimeSpan.FromSeconds(GetSetting(JobSettingsKeys.StoppingWaitTime, defaultTime));
    }
    

    我的错误,我不应该剪切和粘贴。我想说的是(如果我的理解是正确的),你不能配置这个默认时间 - 如果你在这里阅读它https://github.com/projectkudu/kudu/wiki/Web-jobs 没有提到“可配置”,如果你得到,这就是 Kudu 服务代码中当前的内容它在 GitHub 上 Kudu.core 的 ContinuousJobRunner.cs 文件中。

    5 是默认的硬编码值

    public class ContinuousJobRunner : BaseJobRunner, IDisposable
    {
        public const string WebsiteSCMAlwaysOnEnabledKey = "WEBSITE_SCM_ALWAYS_ON_ENABLED";
        private const int DefaultContinuousJobStoppingWaitTimeInSeconds = 5;
    
        private static readonly TimeSpan WarmupTimeSpan = TimeSpan.FromMinutes(2);
    
     ....
    
                _continuousJobLogger.ReportStatus(ContinuousJobStatus.Stopping);
    
                NotifyShutdownJob();
    
                // By default give the continuous job 5 seconds before killing it (after notifying the continuous job)
                if (!_continuousJobThread.Join(JobSettings.GetStoppingWaitTime(DefaultContinuousJobStoppingWaitTimeInSeconds)))
                {
                    _continuousJobThread.Abort();
                }
    
                _continuousJobThread = null;
    

    希望这会有所帮助 新年快乐 斯蒂芬

    【讨论】:

      【解决方案3】:

      你完全正确。对于连续的WebJobs,参数stopping_wait_time"的值将被忽略。

      对于连续的 WebJobs,Azure 会在 WebJob 正在运行的进程即将停止时通知它,然后它会等待一段可配置的时间(默认为 5 秒),之后如果进程没有安静地退出,它会关闭它

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

      完整的解释在这里。 http://blog.amitapple.com/post/2014/05/webjobs-graceful-shutdown/#.VM9Su40tEic

      希望这会有所帮助 最好的祝福 斯蒂芬

      【讨论】:

      • «可配置的时间量»,那么问题仍然存在:如何?
      • 我的错误,我不应该剪切和粘贴 - 我的理解是你不能配置这个默认时间 - 如果你在这里阅读它github.com/projectkudu/kudu/wiki/Web-jobs 没有提到“可配置”,它是Kudu 服务代码中的当前内容
      猜你喜欢
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 2021-04-17
      相关资源
      最近更新 更多