Exemple #1
0
 private function msg_type_call($message)
 {
     // 		printf("msg_type_call: %s\n", print_r($message, true));
     if (!is_array($message) || count($message) !== 2) {
         GWF_Log::logCritical("Dog_WorkerThread::msg_type_call() - The message from queue is not array(func, args)");
     } elseif (!GWF_Callback::isCallback($message[0])) {
         GWF_Log::logCritical("Dog_WorkerThread::msg_type_call() - message[0] is not a valid callback");
     } elseif (!is_array($message[1])) {
         GWF_Log::logCritical("Dog_WorkerThread::msg_type_call() - message[1] is not an args array");
     } else {
         // 			printf("msg_type_call: %s\n%s", print_r($message[0]), print_r($message[1]));
         return call_user_func_array($message[0], $message[1]);
     }
 }
Exemple #2
0
 protected static function proc_rw_with_callbacks($command, array $callbacks = array(), $input = null)
 {
     $descriptors = array(0 => array('pipe', 'r'), 1 => array('pipe', 'rw'), 2 => array('pipe', 'rw'));
     if (!is_array($callbacks)) {
         $callbacks = array();
     } else {
         $callbacks = array_values($callbacks);
     }
     for ($i = count($callbacks); $i < 3; $i++) {
         $callbacks[] = null;
     }
     # Remove invalids
     for ($i = 0; $i < 3; $i++) {
         if (!GWF_Callback::isCallback($callbacks[$i])) {
             GWF_Log::logCritical("GWF_Worker::proc_with_callback() - Invalid callback {$i}: " . GWF_Callback::printCallback($callbacks[$i]));
             $callbacks[$i] = null;
         }
     }
     # Default handlers
     if ($callbacks[1] === null) {
         $callbacks[1] = array(__CLASS__, '_proc_out');
     }
     if ($callbacks[2] === null) {
         $callbacks[2] = array(__CLASS__, '_proc_err');
     }
     $pipes = null;
     if (false === ($proc = proc_open($command, $descriptors, $pipes, null, $_ENV))) {
         return self::err('ERR_GENERAL', array(__FILE__, __LINE__));
     }
     fclose($pipes[0]);
     $read_output = $read_error = false;
     $buffer_len = $prev_buffer_len = 0;
     $ms = 10;
     // 		$output      = '';
     $read_output = true;
     // 		$error       = '';
     $read_error = true;
     stream_set_blocking($pipes[1], 0);
     stream_set_blocking($pipes[2], 0);
     // dual reading of STDOUT and STDERR stops one full pipe blocking the other, because the external script is waiting
     while ($read_error != false || $read_output != false) {
         if ($read_output != false) {
             if (feof($pipes[1])) {
                 fclose($pipes[1]);
                 $read_output = false;
             } else {
                 $str = fread($pipes[1], 1024);
                 $len = strlen($str);
                 if ($len) {
                     call_user_func($callbacks[1], $str);
                     $buffer_len += $len;
                 }
             }
         }
         if ($read_error != false) {
             if (feof($pipes[2])) {
                 fclose($pipes[2]);
                 $read_error = false;
             } else {
                 $str = fread($pipes[2], 1024);
                 $len = strlen($str);
                 if ($len) {
                     call_user_func($callbacks[2], $str);
                     $buffer_len += $len;
                 }
             }
         }
         if ($buffer_len > $prev_buffer_len) {
             $prev_buffer_len = $buffer_len;
             $ms = 10;
         } else {
             usleep($ms * 1000);
             // sleep for $ms milliseconds
             if ($ms < 160) {
                 $ms = $ms * 2;
             }
         }
     }
     // 		fclose($pipes[1]);
     // 		fclose($pipes[2]);
     proc_close($proc);
 }