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

标签 c# azure azure-webjobs

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

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”文件被忽略?

最佳答案

问题是您的 settings.job 文件有两组 UTF-8 BOM,这是非常不寻常的并且会导致解析器崩溃。

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

您使用的是什么编辑器导致了这个问题?

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

关于c# - 为什么我的 Azure WebJobs "settings.job"文件被忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36695607/

相关文章:

azure - 配置 Azure Web 服务器日志以匿名保存或根本不保存 IP

azure-webjobs - 使用静态内容文件部署 WebJob

c# - 如何在 View 模型中包装模型并在数据网格中使用它?

c# - 在 Visual Studio 2017 中为 C# 禁用 'Show live semantic errors'

azure - 交换槽中的名称在 azure 中显示阶段和生产的相同名称

azure - 如何通过 VS Team Services 自动部署 azure webjob - 发布

azure - azure 队列触发器中的单例未按预期工作

c# - 了解 LINQ 查询和 ViewModel

c# - C# (.NET) 中的图表控件使用大量 CPU

azure - 将Azure函数应用程序升级到3.0.0-beta8并在之后中断一些DI代码