run external command with timeout

This commit is contained in:
Uwe Steinmann 2015-08-05 21:10:49 +02:00
parent b5f31fb617
commit 5a22a51943

View File

@ -23,11 +23,45 @@
* @version Release: @package_version@ * @version Release: @package_version@
*/ */
class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document { class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$pipes = array();
$timeout += time();
$process = proc_open($cmd, $descriptorspec, $pipes);
if (!is_resource($process)) {
throw new Exception("proc_open failed on: " . $cmd);
}
$output = '';
do {
$timeleft = $timeout - time();
$read = array($pipes[1]);
stream_select($read, $write = NULL, $exeptions = NULL, $timeleft, NULL);
if (!empty($read)) {
$output .= fread($pipes[1], 8192);
}
} while (!feof($pipes[1]) && $timeleft > 0);
if ($timeleft <= 0) {
proc_terminate($process);
throw new Exception("command timeout on: " . $cmd);
} else {
return $output;
}
} /* }}} */
/** /**
* 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) { public function __construct($dms, $document, $convcmd=null, $nocontent=false, $timeout=5) {
$_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/msword' => 'catdoc %s', 'application/msword' => 'catdoc %s',
@ -90,6 +124,8 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
$mimetype = $version->getMimeType(); $mimetype = $version->getMimeType();
if(isset($_convcmd[$mimetype])) { if(isset($_convcmd[$mimetype])) {
$cmd = sprintf($_convcmd[$mimetype], $path); $cmd = sprintf($_convcmd[$mimetype], $path);
$content = self::execWithTimeout($cmd);
/*
$fp = popen($cmd, 'r'); $fp = popen($cmd, 'r');
if($fp) { if($fp) {
$content = ''; $content = '';
@ -98,6 +134,7 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
} }
pclose($fp); pclose($fp);
} }
*/
if($content) { if($content) {
$this->addField(Zend_Search_Lucene_Field::UnStored('content', $content, 'utf-8')); $this->addField(Zend_Search_Lucene_Field::UnStored('content', $content, 'utf-8'));
} }