1
我正在研究一個腳本,它不斷監視一組文件以確保它們不超過四個小時。在複製這些文件的過程中,腳本可能會將它們視爲丟失,所以我在try ... catch ...塊內手動重試一次。例如:是否有任何推薦的重試操作模式?
try
{
report.age = getFileAgeInMilliSeconds(report.filePath);
// If the file is over age threshhold.
if (report.age > REPORT_AGE_THRESHOLD)
{
// Generate an alert and push it onto the stack.
downtimeReportAlerts.push(generateOldFileAlert(report));
}
}
// If we have trouble...
catch (e)
{
// Find out what the error was and handle it as well as possible.
switch (e.number)
{
case FILE_NOT_FOUND_ERROR_CODE:
// Try once more.
WScript.Sleep(FILE_STAT_RETRY_INTERVAL);
try
{
report.age = getFileAgeInMilliSeconds(report.filePath);
// If the file is over age threshhold.
if (report.age > REPORT_AGE_THRESHOLD)
{
// Generate an alert and push it onto the stack.
downtimeReportAlerts.push(generateOldFileAlert(report));
}
}
// If we have trouble this time...
catch (e)
{
switch (e.number)
{
case FILE_NOT_FOUND_ERROR_CODE:
// Add an alert to the stack.
downtimeReportAlerts.push(generateFileUnavailableAlert(report));
break;
default:
// No idea what the error is. Let it bubble up.
throw(e);
break;
}
}
break;
default:
// No idea what the error is. Let it bubble up.
throw(e);
break;
}
}
在這種類型的場景中是否存在重試操作的任何已建立模式?我正在考慮嘗試將其重寫爲遞歸函數,因爲有很多重複的代碼,其中的錯誤可能會在這裏蔓延,但我不知道如何以爲我會先檢查是否有更好的解決方案。