2013-05-27 481 views
4

为了检查HTTP服务状态,我编写了一个脚本,它通过电子表格和URL列表检查它们是Up还是Down,脚本每五分钟。UrlFetchApp.fetch(url)间歇性“意外错误”

我从UrlFetchApp.fetch(url)和 中偶尔会发生罕见的间歇性“意外错误”错误,如果我在几秒钟后重复请求,DNS和超时错误将会消失。

没有几个实际问题,如果有人能够帮助: 我用使用Utilities.sleep(5000)暂停5秒, 是确定或有几秒钟后,再次有更好的方法来等待和 尝试获取?

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

我该如何改进下面的代码?

实际脚本:

/* 
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; 
} 

}

+0

我写了一个Google Apps脚本用于相同的目的,并且遇到同样的问题。如果您发现此解决方案,请在此处发布。我会继续寻找其他地方... –

+0

似乎其他人也看到了这一点。 https://code.google.com/p/google-apps-script-issues/issues/detail?id=2758 https://code.google.com/p/gmail-delay-send/issues/detail?id = 60 –

+0

我认为出现问题[2758](https://code.google.com/p/google-apps-script-issues/issues/detail?id=2758)(谢谢,Rafael!)是我们的最佳选择现在让Google关注这个问题。 – Vimes

回答

1

你可以使用一个muteHttpExceptions parameter为赶上故障。如果响应代码指示失败,则提取不会引发异常,而是返回HTTPResponse。

样品

var params = {muteHttpExceptions:true}; 
response = UrlFetchApp.fetch(url, params); 

,你可能use a cURL或用于类比一sheldue。

样品

卷曲。将其设置为您的操作系统。

curl -L URL_OF_YOUR_SCRIPT 

添加到脚本

function doGet(e) { 
    var result = {'status': 'ok'}; 
    try{ 
     main(); 
    }catch(err){ 
     result.['status'] = err; 
    } 
    return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON); 
} 

您应该deploy your script as a Web App这一点。

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

最好。