【发布时间】:2016-04-18 13:35:51
【问题描述】:
当 Azure WebJobs 因任何原因停止或重新启动时,在主机控制台进程终止之前有 5 秒的默认宽限期。显然,当 WebJob 发布到 Azure 时,可以通过在 WebJob 中包含一个“settings.job”文件来增加这个时间段,如explained here。不幸的是,我无法让它在我的网站上运行,并且关闭时间为始终 5 秒,尽管这是发布到设置为“始终开启”的基本应用服务的连续 WebJob。
根据链接中的示例代码,我的 Program.cs 如下所示:
class Program
{
static void Main()
{
var host = new JobHost();
host.RunAndBlock();
}
}
我的 Functions.cs 看起来像这样:
public class Functions
{
public static async Task ProcessQueueMessageAsync(
[QueueTrigger("testq")] string message, TextWriter log,
CancellationToken cancellationToken)
{
Console.WriteLine("Function started.");
try
{
await Task.Delay(TimeSpan.FromMinutes(10), cancellationToken);
}
catch (TaskCanceledException)
{
Shutdown().Wait();
}
Console.WriteLine("Function completed succesfully.");
}
private static async Task Shutdown()
{
Console.WriteLine("Function has been cancelled. Performing cleanup ...");
await Task.Delay(TimeSpan.FromSeconds(30));
Console.WriteLine("Function was cancelled and has terminated gracefully.");
}
}
我已将 settings.job 文件添加到项目中,并在 Visual Studio 的属性中将其设置为“始终复制”。文件内容如下:
{ "stopping_wait_time": 60 }
WebJob 随网站一起发布。通过使用 Kudu 工具并转到调试控制台,我可以验证“settings.job”文件是否被复制到与 WebJob 主机可执行文件相同的位置:
但是,如果我查看链接 https://xxx.scm.azurewebsites.net/api/continuouswebjobs,返回的 JSON 根本不包含任何设置:
{
"status":"Stopped",
"detailed_status":"f9e13c - Stopped\r\n",
"log_url":"https://...",
"name":"WebJobTest",
"run_command":"WebJobTest.exe",
"url":"https://...",
"extra_info_url":"https://...",
"type":"continuous",
"error":null,
"using_sdk":true,
"settings":{
}
}
因此,当我从 Azure 门户停止 WebJob 时,我最终会在日志中看到类似的内容:
[04/18/2016 12:53:12 > f9e13c: SYS INFO] Run script 'WebJobTest.exe' with script host - 'WindowsScriptHost'
[04/18/2016 12:53:12 > f9e13c: SYS INFO] Status changed to Running
[04/18/2016 12:53:13 > f9e13c: INFO] Found the following functions:
[04/18/2016 12:53:13 > f9e13c: INFO] WebJobTest.Functions.ProcessQueueMessageAsync
[04/18/2016 12:53:13 > f9e13c: INFO] Job host started
[04/18/2016 13:01:58 > f9e13c: INFO] Executing: 'Functions.ProcessQueueMessageAsync' - Reason: 'New queue message detected on 'testq'.'
[04/18/2016 13:01:58 > f9e13c: INFO] Function started.
[04/18/2016 13:02:47 > f9e13c: SYS INFO] Status changed to Disabling
[04/18/2016 13:02:53 > f9e13c: SYS INFO] Detected WebJob file/s were updated, refreshing WebJob
[04/18/2016 13:02:53 > f9e13c: SYS INFO] Status changed to Stopping
[04/18/2016 13:02:53 > f9e13c: INFO] Function has been cancelled. Performing cleanup ...
[04/18/2016 13:02:58 > f9e13c: ERR ] Thread was being aborted.
[04/18/2016 13:02:58 > f9e13c: SYS INFO] WebJob process was aborted
[04/18/2016 13:02:58 > f9e13c: SYS INFO] Status changed to Stopped
请注意“状态更改为停止”和“线程正在中止”之间的标准 5 秒间隔。
为什么“settings.job”文件被忽略了?
【问题讨论】:
-
对于连续工作,我认为它的工作方式不同。你需要寻找WEBJOBS_SHUTDOWN_FILE blog.amitapple.com/post/2014/05/webjobs-graceful-shutdown/…
-
验证
settings.job文件是否在webjob目录的根目录下,你可以浏览到https://{sitename}.scm.azurewebsites.net/debugconsole然后到目录:@987654332 @ -
在 settings.job 中应该是 { "stopping_wait_time": 60, "is_singleton": true }
-
@DavidEbbo 类似的是,但是在另一个问题上,Azure 基础架构正在检测“stopping_wait_time”值,而我的值似乎被忽略了。
标签: c# azure azure-webjobs