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

时间:2016-04-18 13:35:51

标签: c# azure azure-webjobs

当Azure WebJobs因任何原因停止或重新启动时,在主机控制台进程终止之前,默认宽限期为5秒。显然,通过包含" settings.job"可以增加这个时间段。将WebJob发布到Azure时,将文件作为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主机可执行文件相同的位置:

Kudu screenshot

但是,如果我查看链接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"文件被忽略了?

1 个答案:

答案 0 :(得分:6)

问题是,您的settings.job文件有两个的UTF-8 BOM集,这非常不寻常,会抛弃解析器。

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

你使用的编辑器是什么造成的呢?

在任何情况下,一旦你解决了这个问题,它应该可以正常工作。