get_parms(
'binpath', 'host', 'port', 'base', 'user', 'password', 'socket', 'use_sbas', 'nolog', 'clng', 'winsvc_run', 'charset', 'debugmask'
);
$dom = new DOMDocument();
$dom->formatOutput = true;
if ($dom->loadXML($oldxml)) {
$xmlchanged = false;
foreach (array("str:binpath", "str:host", "str:port", "str:base", "str:user", "str:password", "str:socket", "boo:use_sbas", "boo:nolog", "str:clng", "boo:winsvc_run", "str:charset", 'str:debugmask') as $pname) {
$ptype = substr($pname, 0, 3);
$pname = substr($pname, 4);
$pvalue = $parm2[$pname];
if ($ns = $dom->getElementsByTagName($pname)->item(0)) {
// le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu)
while (($n = $ns->firstChild))
$ns->removeChild($n);
} else {
// le champ n'existait pas dans le xml, on le cr�e
$ns = $dom->documentElement->appendChild($dom->createElement($pname));
}
// on fixe sa valeur
switch ($ptype) {
case "str":
$ns->appendChild($dom->createTextNode($pvalue));
break;
case "boo":
$ns->appendChild($dom->createTextNode($pvalue ? '1' : '0'));
break;
}
$xmlchanged = true;
}
}
return($dom->saveXML());
}
/**
*
* @param string $xml
* @param string $form
* @return string
*/
public function xml2graphic($xml, $form)
{
if (($sxml = simplexml_load_string($xml))) { // in fact XML IS always valid here...
?>
system == 'WINDOWS')
$appname .= '.exe';
?>
system == 'WINDOWS')
$appname .= '.exe';
ob_start();
?>
cmd
host = trim($sx_task_settings->host);
$this->port = trim($sx_task_settings->port);
$this->base = trim($sx_task_settings->base);
$this->user = trim($sx_task_settings->user);
$this->password = trim($sx_task_settings->password);
$this->socket = trim($sx_task_settings->socket);
$this->use_sbas = p4field::isyes(trim($sx_task_settings->use_sbas));
$this->charset = trim($sx_task_settings->charset);
$this->debugmask = (int) (trim($sx_task_settings->debugmask));
$this->nolog = p4field::isyes(trim($sx_task_settings->nolog));
$this->winsvc_run = p4field::isyes(trim($sx_task_settings->winsvc_run));
$this->binpath = p4string::addEndSlash(trim($sx_task_settings->binpath));
parent::load_settings($sx_task_settings);
return $this;
}
/**
*
* @return void
*/
protected function run2()
{
$cmd = $this->binpath . 'phraseanet_indexer';
switch ($this->system) {
case "WINDOWS":
$cmd .= '.exe';
$nullfile = 'NUL';
$this->method = self::METHOD_PROC_OPEN;
break;
default:
case "DARWIN":
case "LINUX":
$nullfile = '/dev/null';
// $this->method = self::METHOD_FORK;
// $this->method = self::METHOD_EXEC;
$this->method = self::METHOD_PROC_OPEN;
break;
}
if ( ! file_exists($cmd) || ! is_executable($cmd)) {
$this->set_status(self::STATUS_STOPPED);
$this->log(sprintf(_('task::cindexer:file \'%s\' does not exists'), $cmd));
throw new Exception('cindexer executable not found', self::ERR_EXECUTABLE_NOT_FOUND);
return;
}
$args = array();
if ($this->host)
$args[] = '-h=' . $this->host;
if ($this->port)
$args[] = '-P=' . $this->port;
if ($this->base)
$args[] = '-b=' . $this->base;
if ($this->user)
$args[] = '-u=' . $this->user;
if ($this->password)
$args[] = '-p=' . $this->password;
if ($this->socket)
$args[] = '--socket=' . $this->socket;
if ($this->use_sbas)
$args[] = '-o';
if ($this->charset)
$args[] = '--default-character-set=' . $this->charset;
if ($this->debugmask > 0)
$args[] = '-d=' . $this->debugmask;
if ($this->nolog)
$args[] = '-n';
if ($this->winsvc_run)
$args[] = '--run';
$registry = registry::get_instance();
$logdir = p4string::addEndSlash($registry->get('GV_RootPath') . 'logs');
$this->new_status = NULL; // new status to set at the end
$this->exception = NULL; // exception to throw at the end
$this->log(sprintf("running cindexer with method %s", $this->method));
switch ($this->method) {
case self::METHOD_PROC_OPEN:
$this->run_with_proc_open($cmd, $args);
break;
case self::METHOD_FORK:
$this->run_with_fork($cmd, $args);
break;
case self::METHOD_EXEC:
$this->run_with_exec($cmd, $args);
break;
}
if ($this->new_status !== NULL)
$this->set_status($this->new_status);
if ($this->exception)
throw $this->exception;
}
private function run_with_proc_open($cmd, $args)
{
$nullfile = $this->system == 'WINDOWS' ? 'NUL' : '/dev/null';
$descriptors = array();
// $descriptors[1] = array("file", $logdir . "/phraseanet_indexer_" . $this->get_task_id() . ".log", "a+");
// $descriptors[2] = array("file", $logdir . "/phraseanet_indexer_" . $this->get_task_id() . ".error.log", "a+");
$descriptors[1] = array("file", $nullfile, "a+");
$descriptors[2] = array("file", $nullfile, "a+");
$pipes = array();
$this->log(sprintf('cmd=\'%s %s\'', $cmd, implode(' ', $args)));
$process = proc_open($cmd . ' ' . implode(' ', $args), $descriptors, $pipes, $this->binpath, null, array('bypass_shell' => true));
$pid = NULL;
if (is_resource($process)) {
$proc_status = proc_get_status($process);
if ($proc_status['running'])
$pid = $proc_status['pid'];
}
$qsent = '';
$timetokill = NULL;
$sock = NULL;
$this->running = true;
while ($this->running) {
if ($this->get_status() == self::STATUS_TOSTOP && $this->socket > 0) {
// must quit task, so send 'Q' to port 127.0.0.1:XXXX to cindexer
if ( ! $qsent && (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) !== false)) {
if (socket_connect($sock, '127.0.0.1', $this->socket) === true) {
socket_write($sock, 'Q', 1);
socket_write($sock, "\r\n", strlen("\r\n"));
for ($i = 0; $this->running && $i < 5; $i ++ )
sleep(1);
$qsent = 'Q';
$timetokill = time() + 10;
} else {
socket_close($sock);
$sock = NULL;
}
}
}
$proc_status = proc_get_status($process);
if ( ! $proc_status['running']) {
// the cindexer died
if ($qsent == 'Q') {
$this->log(_('task::cindexer:the cindexer clean-quit'));
$this->new_status = self::STATUS_STOPPED;
} elseif ($qsent == 'K') {
$this->log(_('task::cindexer:the cindexer has been killed'));
$this->new_status = self::STATUS_STOPPED;
} else {
$this->log(_('task::cindexer:the cindexer crashed'));
$this->exception = new Exception('cindexer crashed', self::ERR_CRASHED);
// do not change the status so scheduler may restart it
}
$this->running = false;
} else {
// the cindexer is still alive
if ($qsent == 'Q') {
if (time() > $timetokill) {
// must kill cindexer
$this->log(_('task::cindexer:killing the cindexer'));
$qsent = 'K';
proc_terminate($process); // sigint
}
}
}
for ($i = 0; $this->running && $i < 5; $i ++ )
sleep(1);
}
if ($sock) {
socket_close($sock);
$sock = NULL;
}
foreach (array_keys($pipes) as $offset) {
if (is_resource($pipes[$offset]))
fclose($pipes[$offset]);
}
proc_terminate($process); // sigint
proc_close($process);
}
private function run_with_fork($cmd, $args)
{
$nullfile = $this->system == 'WINDOWS' ? 'NUL' : '/dev/null';
$pid = pcntl_fork();
if ($pid == -1) {
$this->exception = new Exception('cindexer can\'t fork', self::ERR_CANT_FORK);
} elseif ($pid == 0) {
// child
umask(0);
// openlog('MyLog', LOG_PID | LOG_PERROR, LOG_LOCAL0);
if (($err = posix_setsid()) < 0) {
$this->exception = new Exception('cindexer can\'t detach from terminal', $err);
} else {
chdir(dirname(__FILE__));
pcntl_exec($cmd, $args);
sleep(2);
}
} else {
// parent
$this->running = true;
$sigsent = NULL;
while ($this->running) {
// is the cindexer alive ?
if ( ! posix_kill($pid, 0)) {
// dead...
if ($sigsent === NULL) {
// but it's not my fault
$this->log(_('task::cindexer:the cindexer crashed'));
$this->exception = new Exception('cindexer crashed', self::ERR_CRASHED);
// do not change the status so scheduler may restart it
break;
}
}
if ($this->get_status() == self::STATUS_TOSTOP) {
posix_kill($pid, ($sigsent = SIGINT));
sleep(2);
}
$status = NULL;
if (pcntl_wait($status, WNOHANG) == $pid) {
// child (indexer) has exited
if ($sigsent == SIGINT) {
$this->log(_('task::cindexer:the cindexer clean-quit'));
$this->new_status = self::STATUS_STOPPED;
} elseif ($sigsent == SIGKILL) {
$this->log(_('task::cindexer:the cindexer has been killed'));
$this->new_status = self::STATUS_STOPPED;
} else {
$this->log(_('task::cindexer:the cindexer crashed'));
$this->exception = new Exception('cindexer crashed', self::ERR_CRASHED);
// do not change the status so scheduler may restart it
}
$this->running = false;
} else {
if ($sigsent == SIGINT && time() > $timetokill) {
// must kill cindexer
$this->log(_('task::cindexer:killing the cindexer'));
$qsent = 'K';
posix_kill($pid, ($sigsent = SIGKILL));
}
sleep(2);
}
} // while running
}
}
private function run_with_exec($cmd, $args)
{
$x = pcntl_exec($cmd, $args);
sleep(2);
}
}
// class