2014-11-10 5 views
0

로컬 서버에 저장하기 위해 라이브 서버의 데이터를 가져 오는 클라이언트의 웹 사이트에 이미 기능이 있습니다. 그러나 내가 예상하지 못했던 부분은 때때로 이러한 로컬 서버 좋은 서비스를 제공하는 지역에 있지 않으므로 연결에 실패했기 때문에 스크립트가 일정 시간이 지나면 죽습니다.다른 기능이 시간 프레임 내에서 완료되지 않는 경우 기능 실행

이러한 유형의 외부 호출을 사용하지 않도록 이미 시스템이 구현되어 있지만 서비스가 좋지 않아 서버가 시도하기 때문에 클라이언트가이 "오프라인 모드"를 설정하는 옵션을 시작할 수 없습니다 라이브 서버에 도달합니다.

그래서 내가 무엇을해야하는지는 SyncTable 기능이 8 초 만에 완료하는 데 실패 할 경우 자동으로 "오프라인 모드"를 설정하는 다른 함수를 호출 set_time_limit(8) 같은 함수 내 SyncTable 기능을 포장입니다.

이렇게 할 수 있나요? 그렇다면, 어떻게하면 이러한 고객을 거친 서비스가있는 지역에 저장할 수 있는지 알고 싶습니다.

+0

당신이 뭔가를 의미 http://php.net/manual/en/function.sleep.php : 여기

내가 이렇게하고 시간 제한을 적용하는 데 사용하는 기능입니다)? –

답변

0

proc_open, proc_get_statusproc_terminate을 사용하여 SyncTable 작업을 프로세스로 시작하고 모니터링하고 필요한 경우 종료 할 수 있습니다. 주 : SyncTable 기능을 독립형 프로세스로 시작할 수 있도록 간단한 랩퍼 스크립트를 작성해야 할 수도 있습니다. 같은 Googlable, [수면] (

/// Executes a command and returns the output 
/// If the timeout value (in seconds) is reached, it terminates the process 
/// and returns FALSE 
function exec_timeout($cmd, $timeout=30) 
    { 
    $descriptors = array(
     0 => array('pipe', 'r'), // stdin 
     1 => array('pipe', 'w'), // stdout 
     2 => array('pipe', 'w') // stderr 
    ); 
    $pipes = Array(); 
    $process = proc_open($cmd, $descriptors, $pipes); 

    $result = ''; 
    $end_time = time() + $timeout; 

    if (is_resource($process)) 
     { 
     // set the streams to non-blocking 
     stream_set_blocking($pipes[0], 0); 
     stream_set_blocking($pipes[1], 0); 
     stream_set_blocking($pipes[2], 0); 

     $timeleft = $end_time - time(); 
     while ($timeleft > 0) 
      { 
      $status = proc_get_status($process); 
      $result .= stream_get_contents($pipes[1]); 

      // leave the loop if the process has already finished 
      if (!$status['running']) 
       break; 

      $timeleft = $end_time - time(); 
      } 

     if ($timeleft <= 0) 
      { 
      proc_terminate($process); 
      $result = FALSE; 
      } 
     } 

    // check for errors 
    $errors = stream_get_contents($pipes[2]); 
    if (!empty($errors)) 
     fwrite(STDERR, "$errors\n"); 

    // close streams 
    fclose($pipes[0]); 
    fclose($pipes[1]); 
    fclose($pipes[2]); 

    proc_close($process); 

    return $result; 
    } 
관련 문제