UrlFetchApp.fetch (url) intermittent "Unexpected error"

To check the status of the HTTP service, I wrote a script that goes through the spreadsheet, and to check the list of URLs, if they are up or down, the script is time-controlled every five minutes.

I have rare intermittent "Unexpected Errors" errors from UrlFetchApp.fetch (url) and from time to time there are DNS and timeout errors that will disappear if I repeat the request after a few seconds.

Some real questions if someone can help: I ​​used Utilities.sleep (5000) to pause for 5 seconds, is this normal or are there better ways to wait and try picking up again in a few seconds?

Why do I get "Unexpected errors" even if I repeat the request after 5 seconds, when the script starts again after five minutes, there is no "Unexpected error" !?

How can I improve the code below?

Actual script:

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

}

+4
source share
1 answer

You can use the muteHttpExceptions parameter to detect failures. The extraction does not throw an exception if the response code indicates a failure, and instead returns an HTTPResponse.

Example

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

And you can use cURL or an analogue for sheldue.

Example

curl. Set this at your system level.

 curl -L URL_OF_YOUR_SCRIPT 

Add to 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); } 

You must deploy the script as a web application for this.

If you use the second sample, you no longer need Utilities.sleep (5000).

Best.

+1
source

Source: https://habr.com/ru/post/1483015/


All Articles