Note that there are some explanatory texts on larger screens.

plurals
  1. POUrlFetchApp.fetch(url) intermittent "Unexpected error"
    primarykey
    data
    text
    <p>In order to check HTTP service status I've wrote script that goes through spreadsheet and for a list of URLs checks if they are Up or Down, script is time driven every five minutes.</p> <p>I have rare intermittent "Unexpected error" errors from UrlFetchApp.fetch(url) and from time to time there are DNS and Timeout errors that will go away if I repeat request after few seconds.</p> <p>No few actual questions if someone can help: I've used Utilities.sleep(5000) to pause for 5 seconds, is that OK or there are better ways to wait and try to fetch again after few seconds?</p> <p>Why do I get "Unexpected error" even if I repeat request after 5 seconds, when script is run again after five minutes there is no "Unexpected error" !?</p> <p>How could I improve code below?</p> <p>Actual script:</p> <pre><code>/* 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") &gt; -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) &amp;&amp; tries &gt;= 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; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload