execWithTimeout() returns stderr, which is saved in error msg

This commit is contained in:
Uwe Steinmann 2018-01-30 06:28:19 +01:00
parent a47300be26
commit 3d168e427a
2 changed files with 54 additions and 22 deletions

View File

@ -24,6 +24,8 @@
*/ */
class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document { class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
protected $errormsg;
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */ static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
$descriptorspec = array( $descriptorspec = array(
0 => array("pipe", "r"), 0 => array("pipe", "r"),
@ -37,17 +39,23 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
if (!is_resource($process)) { if (!is_resource($process)) {
throw new Exception("proc_open failed on: " . $cmd); throw new Exception("proc_open failed on: " . $cmd);
} }
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
$output = ''; $output = $error = '';
$timeleft = $timeout - time(); $timeleft = $timeout - time();
$read = array($pipes[1]); $read = array($pipes[1], $pipes[2]);
$write = NULL; $write = NULL;
$exeptions = NULL; $exeptions = NULL;
do { do {
stream_select($read, $write, $exeptions, $timeleft, 200000); $num_changed_streams = stream_select($read, $write, $exeptions, $timeleft, 200000);
if (!empty($read)) { if ($num_changed_streams === false) {
proc_terminate($process);
throw new Exception("stream select failed on: " . $cmd);
} elseif ($num_changed_streams > 0) {
$output .= fread($pipes[1], 8192); $output .= fread($pipes[1], 8192);
$error .= fread($pipes[2], 8192);
} }
$timeleft = $timeout - time(); $timeleft = $timeout - time();
} while (!feof($pipes[1]) && $timeleft > 0); } while (!feof($pipes[1]) && $timeleft > 0);
@ -56,7 +64,7 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
proc_terminate($process); proc_terminate($process);
throw new Exception("command timeout on: " . $cmd); throw new Exception("command timeout on: " . $cmd);
} else { } else {
return $output; return array('stdout'=>$output, 'stderr'=>$error);
} }
} /* }}} */ } /* }}} */
@ -64,7 +72,8 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
* Constructor. Creates our indexable document and adds all * Constructor. Creates our indexable document and adds all
* necessary fields to it using the passed in document * necessary fields to it using the passed in document
*/ */
public function __construct($dms, $document, $convcmd=null, $nocontent=false, $timeout=5) { public function __construct($dms, $document, $convcmd=null, $nocontent=false, $timeout=5) { /* {{{ */
$this->errormsg = '';
$_convcmd = array( $_convcmd = array(
'application/pdf' => 'pdftotext -enc UTF-8 -nopgbrk %s - |sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'', 'application/pdf' => 'pdftotext -enc UTF-8 -nopgbrk %s - |sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'',
'application/postscript' => 'ps2pdf14 %s - | pdftotext -enc UTF-8 -nopgbrk - - | sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'', 'application/postscript' => 'ps2pdf14 %s - | pdftotext -enc UTF-8 -nopgbrk - - | sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'',
@ -137,13 +146,20 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
if($cmd) { if($cmd) {
try { try {
$content = self::execWithTimeout($cmd, $timeout); $content = self::execWithTimeout($cmd, $timeout);
if($content) { if($content['stdout']) {
$this->addField(Zend_Search_Lucene_Field::UnStored('content', $content, 'utf-8')); $this->addField(Zend_Search_Lucene_Field::UnStored('content', $content['stdout'], 'utf-8'));
}
if($content['stderr']) {
$this->errormsg = $content['stderr'];
} }
} catch (Exception $e) { } catch (Exception $e) {
} }
} }
} }
} } /* }}} */
public function getErrorMsg() { /* {{{ */
return $this->errormsg;
} /* }}} */
} }
?> ?>

View File

@ -29,6 +29,8 @@ require_once('Document.php');
*/ */
class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document { class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
protected $errormsg;
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */ static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
$descriptorspec = array( $descriptorspec = array(
0 => array("pipe", "r"), 0 => array("pipe", "r"),
@ -36,23 +38,29 @@ class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
2 => array("pipe", "w") 2 => array("pipe", "w")
); );
$pipes = array(); $pipes = array();
$timeout += time(); $timeout += time();
$process = proc_open($cmd, $descriptorspec, $pipes); $process = proc_open($cmd, $descriptorspec, $pipes);
if (!is_resource($process)) { if (!is_resource($process)) {
throw new Exception("proc_open failed on: " . $cmd); throw new Exception("proc_open failed on: " . $cmd);
} }
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
$output = ''; $output = $error = '';
$timeleft = $timeout - time(); $timeleft = $timeout - time();
$read = array($pipes[1]); $read = array($pipes[1], $pipes[2]);
$write = NULL; $write = NULL;
$exeptions = NULL; $exeptions = NULL;
do { do {
stream_select($read, $write, $exeptions, $timeleft, 200000); $num_changed_streams = stream_select($read, $write, $exeptions, $timeleft, 200000);
if (!empty($read)) { if ($num_changed_streams === false) {
proc_terminate($process);
throw new Exception("stream select failed on: " . $cmd);
} elseif ($num_changed_streams > 0) {
$output .= fread($pipes[1], 8192); $output .= fread($pipes[1], 8192);
$error .= fread($pipes[2], 8192);
} }
$timeleft = $timeout - time(); $timeleft = $timeout - time();
} while (!feof($pipes[1]) && $timeleft > 0); } while (!feof($pipes[1]) && $timeleft > 0);
@ -61,7 +69,7 @@ class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
proc_terminate($process); proc_terminate($process);
throw new Exception("command timeout on: " . $cmd); throw new Exception("command timeout on: " . $cmd);
} else { } else {
return $output; return array('stdout'=>$output, 'stderr'=>$error);
} }
} /* }}} */ } /* }}} */
@ -69,7 +77,8 @@ class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
* Constructor. Creates our indexable document and adds all * Constructor. Creates our indexable document and adds all
* necessary fields to it using the passed in document * necessary fields to it using the passed in document
*/ */
public function __construct($dms, $document, $convcmd=null, $nocontent=false, $timeout=5) { public function __construct($dms, $document, $convcmd=null, $nocontent=false, $timeout=5) { /* {{{ */
$this->errormsg = '';
$_convcmd = array( $_convcmd = array(
'application/pdf' => 'pdftotext -enc UTF-8 -nopgbrk %s - |sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'', 'application/pdf' => 'pdftotext -enc UTF-8 -nopgbrk %s - |sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'',
'application/postscript' => 'ps2pdf14 %s - | pdftotext -enc UTF-8 -nopgbrk - - | sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'', 'application/postscript' => 'ps2pdf14 %s - | pdftotext -enc UTF-8 -nopgbrk - - | sed -e \'s/ [a-zA-Z0-9.]\{1\} / /g\' -e \'s/[0-9.]//g\'',
@ -142,13 +151,20 @@ class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
if($cmd) { if($cmd) {
try { try {
$content = self::execWithTimeout($cmd, $timeout); $content = self::execWithTimeout($cmd, $timeout);
if($content) { if($content['stdout']) {
$this->addField('content', $content, 'unstored'); $this->addField('content', $content['stdout'], 'unstored');
}
if($content['stderr']) {
$this->errormsg = $content['stderr'];
} }
} catch (Exception $e) { } catch (Exception $e) {
} }
} }
} }
} } /* }}} */
public function getErrorMsg() { /* {{{ */
return $this->errormsg;
} /* }}} */
} }
?> ?>