gpt4 book ai didi

google-apps-script - UrlFetchApp.fetch(url) 间歇性 "Unexpected error"

转载 作者:行者123 更新时间:2023-12-03 19:11:08 26 4
gpt4 key购买 nike

为了检查 HTTP 服务状态,我编写了脚本,通过电子表格检查 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;
}

}

最佳答案

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

sample

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

你可以 use a cURL或类比 Sheldue。

sample

curl 。将此设置在您的操作系统中。
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)。

最好的事物。

关于google-apps-script - UrlFetchApp.fetch(url) 间歇性 "Unexpected error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16778964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com