首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UrlFetchApp.fetch(url)间歇性“意外错误”

UrlFetchApp.fetch(url)间歇性“意外错误”
EN

Stack Overflow用户
提问于 2013-05-27 18:53:53
回答 1查看 3.5K关注 0票数 4

为了检查HTTP服务状态,我已经编写了通过电子表格的脚本,对于URL列表,如果它们是向上的还是向下的,脚本是每五分钟驱动一次的。

我有来自UrlFetchApp.fetch(url)的罕见的间歇性“意外错误”错误,如果我在几秒钟后重复请求,有时会有DNS和Timeout错误消失。

如果有人能帮忙的话,就不会有几个实际的问题:我已经用Utilities.sleep(5000)暂停了5秒,这样可以吗?还是有更好的方法来等待并尝试在几秒钟后再取?

为什么我会得到“意外错误”,即使我在5秒后重复请求,当脚本在5分钟后再次运行时,没有“意外错误”!?

如何改进下面的代码?

实际脚本:

代码语言:javascript
运行
复制
/* 
Periodically check status of web sites :-)
Google Apps for Busines UrlFetch daily limit is 100.000 requests,

Algorithm
  read site and old status from sheet
  check site and set new status  
  if status changed send email (+sms in future by using twilio)
  update status in spreadsheet

"Site, Status code, Time of last change, Last error description"
*/
function main() {
  var sheet = SpreadsheetApp.getActiveSheet() ;
  var currentRow, oldStatusCode, newStatusCode ;
  var url, response, err, subject, message ; 
  var today = new Date() ;

  currentRow = 2
  while ((url = sheet.getRange(currentRow, 1).getValue()) != "") {
    oldStatusCode = sheet.getRange(currentRow, 2).getValue() ;
    newStatusCode = "Ok"
    subject = "mCheck: " + url + " Up Status Change!" ;  
    message = url + " Up Status Change!" + "\n Time: " + today.toUTCString() ;

    var tries = 3 ; // Check at least three times that status changed and it is not a one time glitch
    do {
      try {
        response = UrlFetchApp.fetch(url) ;
      } catch (err) {
      newStatusCode = "Down"
      sheet.getRange(currentRow, 4).setValue(err.message) ;
      subject = "mCheck: " + url + " Down Status Change!" ;  
      message = url + " Down Status Change!" + "\n Error message: " + err.message + "\n Time: " + today.toUTCString() ;
      if (err.message.indexOf("Unexpected") > -1) { // If UrlFetch failed on Google side just ignore this iteration...
        newStatusCode = oldStatusCode ;
      }
    }
    if (oldStatusCode != newStatusCode) { // In case of status change wait 5 seconds before trying again
      Utilities.sleep(5000) ;
    }
    --tries ;
  } while ((oldStatusCode != newStatusCode) && tries >= 0)

  if (oldStatusCode != newStatusCode) {
    sheet.getRange(currentRow, 2).setValue(newStatusCode) ;
    sheet.getRange(currentRow, 3).setValue(today.toUTCString()) ;
    if (oldStatusCode != "") {
      MailApp.sendEmail(email_to, subject, message) ;
    }
  }
  ++currentRow;
 } 

}

EN

回答 1

Stack Overflow用户

发布于 2014-08-08 14:20:43

您可以使用muteHttpExceptions参数来捕获失败。如果响应代码指示失败,则fetch不会抛出异常,而是返回HTTPResponse。

样本

代码语言:javascript
运行
复制
var params = {muteHttpExceptions:true};
response = UrlFetchApp.fetch(url, params);

你可以用使用cURL或类似的方法来表示搁置。

样本

cURL。由于您的操作系统,请将此设置在搁置处。

代码语言:javascript
运行
复制
curl -L URL_OF_YOUR_SCRIPT

添加到脚本中

代码语言:javascript
运行
复制
function doGet(e) {
    var result = {'status': 'ok'};
    try{
        main();
    }catch(err){
        result.['status'] = err;
    }
  return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON);
 }

你应该为此而https://developers.google.com/apps-script/execution_web_apps

如果要使用第二个示例,则不需要更多的Utilities.sleep(5000)。

最好的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16778964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档