Azure Web Jobs的自动重启故障排除

时间:2018-01-26 11:38:45

标签: azure azure-webjobs

我有一个Azure网站,用户可以在其上传大量的XML文件。这些文件需要在数据库中处理和填写。

对于此处理,我使用了一个连续的webjob。

出于非相关原因,所有上传的文件都需要按用户进行处理。 所以我有一个包含所有文件和userId的表。我有一张跑步工作的桌子。我有多个webjobs做同样的过程。如果需要处理任何文件,每个webjob都会在files表中查找。在开始之前,如果另一个作业尚未处理用户的文件,则检查正在运行的作业表。

这很好用,可以运行几个月没有任何问题。 但有时连续的Web作业正在重新启动。大多数在晚上(我的时间)让我错过了宝贵的处理时间。 我是唯一访问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作业未完成,因此不会更新正在运行的作业表。重新启动时,作业仍然认为用户的文件由另一个Web作业处理,使所有作业等待彼此,并且没有任何事情发生。

如何查看作业重启的原因?当我知道我可能解决它的原因。 非常感谢任何帮助。

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

    // 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仍在运行时该文件不存在。 还有什么建议吗?

更新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);

1 个答案:

答案 0 :(得分:2)

我们在评论中调查了一些主要调查结果:

  • 为了获得最佳关机行为,您的WebJob需要实现graceful shutdown pattern,这基本上包括侦听名为%WEBJOBS_SHUTDOWN_FILE%的文件的外观(注意:使用WebJobs SDK时不需要这样做)自动)。
  • 随着平台升级,预计在PaaS环境中会有一些重启。这完全是为了在不中断的情况下处理它。
相关问题