You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure WebJob运行超时错误(SCM_COMMAND_IDLE_TIMEOUT等)解决咨询

解决Azure WebJob因Idle超时终止的问题

首先,你遇到的错误是因为Azure检测到WebJob进程长时间没有输出或CPU活动,自动终止了进程。你尝试在Web.config里添加配置没生效,是因为这些超时配置需要在Azure门户的App Service应用设置里配置,而不是Web.config(Web.config里的设置会被门户的应用设置覆盖,部分特定配置也不支持通过Web.config设置)。

下面是具体的解决步骤:

1. 在Azure门户正确配置超时参数

WebJobs的超时配置属于App Service的全局应用设置,并非单独的WebJob配置,操作步骤如下:

  • 登录Azure门户,找到你的目标App Service
  • 左侧导航栏选择配置 -> 应用程序设置
  • 点击新建应用程序设置,分别添加两个配置项:
    • 键:SCM_COMMAND_IDLE_TIMEOUT,值:3600(单位:秒,可根据需求调整,比如7200表示2小时)—— 适用于触发式WebJob(按需/定时运行的类型)
    • 键:WEBJOBS_IDLE_TIMEOUT,值:3600—— 适用于连续运行的WebJob
  • 保存设置,App Service会自动重启,配置生效

2. 在代码中添加定期输出,避免被判定为Idle

Azure判定进程Idle的核心依据是:长时间没有控制台输出,且无CPU活动。你的爬取代码是同步执行的,可能在下载大页面或处理大量数据时,长时间没有任何输出,导致触发超时。

修改代码,添加控制台输出(WebJobs会将控制台输出作为日志捕获,Azure会以此判断进程活跃):

爬取代码修改示例

public SiteProcessingResult ScrapeSite(string url, int siteId)
{
    SiteProcessingResult result = new SiteProcessingResult();
    int count = 0;
    Console.WriteLine($"Starting to scrape site: {url} (Site ID: {siteId})"); // 添加初始输出

    WebClient webRequest = new WebClient();
    webRequest.Encoding = Encoding.UTF8;
    Console.WriteLine("Downloading main site HTML..."); // 添加步骤输出
    string mainSiteHTML = webRequest.DownloadString(url);
    
    mainSiteHTML = ScrapeHelper.RemoveComments(mainSiteHTML);
    mainSiteHTML = mainSiteHTML.Substring(mainSiteHTML.IndexOf("<div class=\"wrap content\">"));
    mainSiteHTML = mainSiteHTML.Remove(mainSiteHTML.IndexOf("<footer"));
    
    string depReg = string.Format("{0}.*?{1}", "<div class=\"row\"", "@example.org</span>");
    MatchCollection matchList = Regex.Matches(mainSiteHTML, depReg, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Singleline);
    Console.WriteLine($"Found {matchList.Count} articles to process"); // 添加计数输出

    foreach (Match match in matchList)
    {
        count++;
        string articleUrl = GetArticleUrl(match.ToString(), url);
        string title = GetArticleTitle(match.ToString());
        // 每处理1条记录输出一次,确保Azure检测到活动
        Console.WriteLine($"Processing article {count}/{matchList.Count}: {title} ({articleUrl})");

        string organization = "";
        string date = "";
        string locationString = GetLocationFromArticle(match.ToString());
        string content = GetContentFromArticle(match.ToString());
        string state = GetState(locationString);
        
        SitePost record = SitePost.CreateSiteRecord(date, content, title, articleUrl, jobTitle, organization, locationString, state, null, CultureInfo.CreateSpecificCulture("en-US"));
        result.Records.Add(record);
    }

    Console.WriteLine($"Scraping completed. Total records added: {result.Records.Count}"); // 添加完成输出
    return result;
}

邮件发送代码修改示例

public void SendEmail(SiteTypeEnum siteType, string subject, string message, bool isHtml = false, string sendTo = null)
{
    string userName, password, to;
    GetEmailLoginDetails(siteType, out userName, out password, out to);
    if (sendTo == null)
    {
        sendTo = to;
    }

    Console.WriteLine($"Preparing to send email: Subject='{subject}', To='{sendTo}'"); // 添加输出
    LoggerHelper.WriteInfo("Send email, phase {0}, username {1}, password {2}, to {3}, subject {4}", siteType, userName, password, sendTo, subject);
    
    Logic.Helper.EmailGoogle.SendMessage(sendTo, string.Empty, string.Empty, subject, message, isHtml, null, userName, password, userName, userName);
    
    Console.WriteLine("Email sent successfully"); // 添加完成输出
    //SendEmailDev(phase2, subject, message, isHtml);
}

3. 可选:改用异步HTTP请求提升CPU活动检测

你的爬取代码使用WebClient.DownloadString同步方法,可能在等待响应时导致CPU处于闲置状态。建议改用HttpClient的异步方法,这样线程不会被阻塞,Azure更容易检测到CPU活动:

// 全局单例HttpClient,避免频繁创建实例
private static readonly HttpClient _httpClient = new HttpClient();

public async Task<SiteProcessingResult> ScrapeSiteAsync(string url, int siteId)
{
    SiteProcessingResult result = new SiteProcessingResult();
    int count = 0;
    Console.WriteLine($"Starting to scrape site: {url} (Site ID: {siteId})");

    Console.WriteLine("Downloading main site HTML...");
    string mainSiteHTML = await _httpClient.GetStringAsync(url);
    
    // 后续处理逻辑和之前一致,保持定期输出
    // ...
}

注意:如果你的WebJob是触发式,需要修改入口方法为异步兼容的形式(比如public static async Task Main())。


内容的提问来源于stack exchange,提问作者ISTech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:12:53