在我昨天的问题(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格林尼治标准时间我向服务器发出了一个停靠点,Dash Board中的以下条目是:
[12/31/2015 07:00:23 > d71f29: SYS INFO] Status changed to Disabling [12/31/2015 07:00:28 > d71f29: SYS INFO] Detected WebJob file/s were updated, refreshing WebJob [12/31/2015 07:00:28 > d71f29: SYS INFO] Status changed to Stopping [12/31/2015 07:00:30 > d71f29: INFO] Job host stopped [12/31/2015 07:00:32 > d71f29: SYS INFO] Status changed to Success [12/31/2015 07:00:32 > d71f29: SYS INFO] Status changed to Stopped
我的正在运行的工作立即结束而没有任何进一步的信息我函数的最后两行代码是记录到仪表板并将条目写入存储表。这些条目都没有被写入......我已经多次重试,并且每次都在5秒内重置作业主机。所以:
题
连续WebJobs是否正常忽略“stopping_wait_time”值?
stopping_wait_time
设置由连续和触发作业使用。您可以看到Continuous job runner如何使用此设置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秒正常关闭逻辑成功运行,如控制台输出所示:
编辑3/19/2019
通过在stopping_wait_time
中设置settings.job
值,可以根据文档配置该设置。在幕后,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;
希望这有助于新年快乐Stéphane
你是完全正确的。对于连续的WebJobs,将忽略参数stops_wait_time“值。
对于连续的WebJobs,Azure将在WebJob运行进程即将停止时通知它,然后它将等待一段可配置的时间(默认情况下为5秒),之后如果进程没有安静地退出,它将关闭它
对于触发的WebJobs(而不是连续的WebJobs),没有关闭通知,但是有一个优雅的时间段(默认为30秒),WebJob不会立即强制关闭,优雅的时间段是可配置的。
完整的解释在这里。 http://blog.amitapple.com/post/2014/05/webjobs-graceful-shutdown/#.VM9Su40tEic
希望这有助于最好的问候Stéphane