为什么我的Azure WebJobs“settings.job”文件被忽略?

当Azure WebJobs由于某种原因而停止或重新启动时,在主机控制台进程终止之前,默认宽限期为5秒。 显然,可以通过在WebJob发布到Azure时将“settings.job”文件包含在内来增加此时间段,如此处所述 。 不幸的是,我不能让这个工作用于我的网站,关机时间总是 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工具并转到调试控制台,我可以validation“settings.job”文件是否被复制到与WebJob主机可执行文件相同的位置:

Kudu截图

但是,如果我查看链接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”文件?

问题是你的settings.job文件有两组UTF-8 BOM,这是非常不寻常的,并抛出了解析器。

具体来说,它在开头有以下顺序(使用hex编辑器查看): EF BB BF EF BB BF 。 通常,UTF-8 BOM只是EF BB BF

您使用的是什么编辑器?

无论如何,一旦你解决了这个问题,它应该可以正常工作。