run command with timeout

This commit is contained in:
Uwe Steinmann 2015-08-08 09:35:44 +02:00
parent d7223bc521
commit 1ad145ffad

View File

@ -50,6 +50,39 @@ class SeedDMS_Preview_Previewer {
$this->width = intval($width);
}
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;
}
} /* }}} */
/**
* Retrieve the physical filename of the preview image on disk
*
@ -113,7 +146,11 @@ class SeedDMS_Preview_Previewer {
break;
}
if($cmd) {
exec($cmd);
//exec($cmd);
try {
self::execWithTimeout($cmd);
} catch(Exception $e) {
}
}
return true;
}