2013-05-27 480 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)。

最好。