2011-09-23 4 views
1

필자는 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; 
     } 
    } 

이 유형의 시나리오에서 작업을 다시 시도하기위한 설정 패턴이 있습니까? 나는 재귀 함수로 이것을 재 작성하려고 시도했다. 오류가 여기에 끼어들 수있는 많은 중복 코드가 있기 때문에, 그러나 더 나은 해결책이 먼저 있는지 어떻게 알았는지 확실하지 않았다.

답변

3

푸시를 수행하려고 시도하는 재귀 메서드는 가장 좋은 방법 일 수 있습니다. 여전히 try..catch를 사용하여 재귀 메서드를 다시 시도 할 수 있습니다 (아마도 "수면"및 기타 특정 오류 처리). 반복적으로 호출해야하는 횟수를 정의하는 RETRY_COUNT 상수를 도입하여 다른 오류가 발생하지 않고 연속 루프가되지 않도록하고 "attemptCount"변수를 증가시켜 실행 시도 횟수를 추적하십시오 따라서 RETRY_COUNT에 도달 한 후 루프를 빠져 나올 수 있습니다.

관련 문제