필자는 4 시간을 넘지 않도록 파일 집합을 지속적으로 모니터하는 스크립트 작업을하고 있습니다. 이러한 파일이 복사되는 동안 스크립트에서 누락 된 파일로 표시 될 수 있으므로 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;
}
}
이 유형의 시나리오에서 작업을 다시 시도하기위한 설정 패턴이 있습니까? 나는 재귀 함수로 이것을 재 작성하려고 시도했다. 오류가 여기에 끼어들 수있는 많은 중복 코드가 있기 때문에, 그러나 더 나은 해결책이 먼저 있는지 어떻게 알았는지 확실하지 않았다.