Azure Web 作业的自动重启故障排除

Troubleshooting automatic restart of Azure Web Jobs

我有一个 Azure 网站,用户可以在该网站上上传大量 XML 文件。这些文件需要处理并填入数据库。

对于这个处理,我使用了一个连续的网络作业。

出于非相关原因,所有上传的文件都需要按用户进行处理。 所以我有一个包含所有文件和用户 ID 的 table。我有一个 table 和 运行 宁工作。我有多个网络作业执行相同的过程。如果有任何文件需要处理,每个 webjob 都会查看文件 table。在开始之前,它会检查 运行ning 作业 table 是否有另一个作业尚未处理用户的文件。

这很好用,可以 运行 几个月没有任何问题。 但有时连续的网络作业正在重新启动。主要是在晚上(我的时间)让我错过了宝贵的处理时间。 我是唯一访问 Azure 的人。在重新启动之前我没有部署任何新的东西。作业在重新启动时大部分时间都在处理。所以内存问题可能是一个问题。但我正在 运行 宁 S3 和最大 cpu 内存不超过 40%。 日志记录也不是很有用:

[01/25/2018 05:03:20 > 5657e1: INFO] Starting job: 28158.
[01/25/2018 09:49:24 > 5657e1: SYS INFO] WebJob is still running
[01/25/2018 20:23:06 > 5657e1: SYS INFO] Status changed to Starting
[01/25/2018 20:23:06 > 5657e1: SYS INFO] WebJob singleton setting is False

因为 Web 作业没有很好地完成,所以 运行ning 作业 table 没有更新。重新启动时,该作业仍然认为用户的文件由另一个 Web 作业处理,使所有作业相互等待,没有任何反应。

如何查看作业重新启动的原因?当我知道原因时,我可能会修复它。 非常感谢任何帮助。

更新 我更改了入口点并在 main 方法的顶部添加了以下行:

    // Get the shutdown file path from the environment
    _shutdownFile = Environment.GetEnvironmentVariable("WEBJOBS_SHUTDOWN_FILE");
    _log.Info("Watching " + _shutdownFile);
    // Setup a file system watcher on that file's directory to know when the file is created:
    var filename = Path.GetFileName(_shutdownFile);
    if (filename != null)
    {
        var fileSystemWatcher = new FileSystemWatcher(filename);
        fileSystemWatcher.Created += OnAzureRestart;
        fileSystemWatcher.Changed += OnAzureRestart;
        fileSystemWatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.FileName | NotifyFilters.LastWrite;
        fileSystemWatcher.IncludeSubdirectories = false;
        fileSystemWatcher.EnableRaisingEvents = true;
        _log.Info("FileSystemWatcher is set-up");
    }

但是在将它发布到 Azure 之后,webjob 不会启动但会抛出错误:

[02/08/2018 15:23:56 > a93630: ERR ] Unhandled Exception: System.ArgumentException: The directory name gugfn3vx.0gk is invalid.
[02/08/2018 15:23:56 > a93630: ERR ]    at System.IO.FileSystemWatcher..ctor(String path, String filter)
[02/08/2018 15:23:56 > a93630: ERR ]    at System.IO.FileSystemWatcher..ctor(String path)
[02/08/2018 15:23:56 > a93630: ERR ]    at TaskRunner.Program.Main(String[] args)

我认为问题出在这一行 Path.GetFileName(_shutdownFile) 因为当 webjob 仍然 运行ning 时文件不存在。 还有更多建议吗?

更新 2 不知何故,我更改了错误的代码。这是工作代码:

    // Get the shutdown file path from the environment
    _shutdownFile = Environment.GetEnvironmentVariable("WEBJOBS_SHUTDOWN_FILE");
    _log.Info("Watching " + _shutdownFile);
    // Setup a file system watcher on that file's directory to know when the file is created:
    var folder = Path.GetDirectoryName(_shutdownFile);
    if (folder != null)
    {
        var fileSystemWatcher = new FileSystemWatcher(folder);
        fileSystemWatcher.Created += OnAzureRestart;
        fileSystemWatcher.Changed += OnAzureRestart;
        fileSystemWatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.FileName | NotifyFilters.LastWrite;
        fileSystemWatcher.IncludeSubdirectories = false;
        fileSystemWatcher.EnableRaisingEvents = true;
        _log.Info("FileSystemWatcher is set-up");
    }

变化在行var folder = Path.GetDirectoryName(_shutdownFile);

我们在评论中调查时概述了几个关键发现:

  • 为了获得最佳关闭行为,您的 WebJob 需要实现 graceful shutdown pattern,它主要是侦听名为 %WEBJOBS_SHUTDOWN_FILE% 的文件的出现(注意:使用时不需要这样做WebJobs SDK,因为它会自动执行此操作)。
  • 随着平台的升级,PaaS 环境中预计会进行一些重启。这一切都是为了在不中断的情况下处理它。