This commit is contained in:
Romain Neutron
2013-01-21 23:19:50 +01:00
parent c144041980
commit b4dfcc2f2f
8 changed files with 52 additions and 201 deletions

View File

@@ -11,20 +11,12 @@
namespace Alchemy\Phrasea\Core\Service\TaskManager; namespace Alchemy\Phrasea\Core\Service\TaskManager;
use Alchemy\Phrasea\Core;
use Alchemy\Phrasea\Core\Service;
use Alchemy\Phrasea\Core\Service\ServiceAbstract; use Alchemy\Phrasea\Core\Service\ServiceAbstract;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Bridge\Monolog\Logger; use Symfony\Bridge\Monolog\Logger;
use Monolog\Handler\SyslogHandler; use Monolog\Handler\SyslogHandler;
use Monolog\Handler\NativeMailerHandler; use Monolog\Handler\NativeMailerHandler;
use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Exception\RuntimeException;
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class TaskManager extends ServiceAbstract class TaskManager extends ServiceAbstract
{ {
/** ` /** `
@@ -48,33 +40,18 @@ class TaskManager extends ServiceAbstract
$options = $this->getOptions(); $options = $this->getOptions();
$registry = $this->app['phraseanet.registry']; $registry = $this->app['phraseanet.registry'];
// send log to syslog ? if (null !== $syslogLevel = constant($options['syslog_level'])) {
if (null !== ($syslogLevel = constant($options['syslog_level']))) { $handler = new SyslogHandler("Phraseanet-Task", "user", $syslogLevel);
$handler = new SyslogHandler(
"Phraseanet-Task", // string added to each message
"user", // facility (type of program logging)
$syslogLevel, // level
true // bubble
);
$logger->pushHandler($handler); $logger->pushHandler($handler);
} }
// send log by mail ? if (null !== $maillogLevel = constant($options['maillog_level'])) {
if (null !== ($maillogLevel = constant($options['maillog_level']))) { if ('' === $adminMail = trim($registry->get('GV_adminMail'))) {
if (($adminMail = $registry->get('GV_adminMail')) == '') { throw new RuntimeException("Admininstrator mail must be set to get log by mail.");
throw(new RuntimeException(sprintf(
"Admininstrator mail must be set to get log by mail."))
);
} }
$senderMail = $registry->get('GV_defaultmailsenderaddr'); $senderMail = $registry->get('GV_defaultmailsenderaddr');
$handler = new NativeMailerHandler( $handler = new NativeMailerHandler($adminMail, "Phraseanet-Task", $senderMail, $maillogLevel);
$adminMail,
"Phraseanet-Task",
$senderMail,
$maillogLevel, // level
true
);
$logger->pushHandler($handler); $logger->pushHandler($handler);
} }
@@ -82,7 +59,7 @@ class TaskManager extends ServiceAbstract
} }
/** /**
* Set and return a new \task_manager instance * Set and return a new task_manager instance
* *
* @return \task_manager * @return \task_manager
*/ */

View File

@@ -10,13 +10,12 @@
*/ */
use Alchemy\Phrasea\Command\Command; use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Monolog\Handler; use Monolog\Handler;
use Monolog\Logger;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\RotatingFileHandler;
@@ -38,22 +37,23 @@ class module_console_taskrun extends Command
$this->task = NULL; $this->task = NULL;
$this->shedulerPID = NULL; $this->shedulerPID = NULL;
$this->addArgument('task_id', InputArgument::REQUIRED, 'The task_id to run'); $this
$this->addOption( ->addArgument('task_id', InputArgument::REQUIRED, 'The task_id to run')
'runner' ->addOption(
, 'r' 'runner',
, InputOption::VALUE_REQUIRED 'r',
, 'The name of the runner (manual, scheduler...)' InputOption::VALUE_REQUIRED,
, task_abstract::RUNNER_MANUAL 'The name of the runner (manual, scheduler...)',
); task_abstract::RUNNER_MANUAL
$this->addOption( )
'ttyloglevel' ->addOption(
, 't' 'ttyloglevel',
, InputOption::VALUE_REQUIRED 't',
, 'threshold : (DEBUG|INFO|WARNING|ERROR|CRITICAL|ALERT)' InputOption::VALUE_REQUIRED,
, '' 'threshold : (DEBUG|INFO|WARNING|ERROR|CRITICAL|ALERT)',
); ''
$this->setDescription('Run task'); )
->setDescription('Run task');
return $this; return $this;
} }
@@ -97,42 +97,31 @@ class module_console_taskrun extends Command
if ($input->getOption('verbose')) { if ($input->getOption('verbose')) {
$handler = new StreamHandler(fopen('php://stdout', 'a')); $this->container['monolog']->pushHandler(new StreamHandler('php://stdout'));
$this->container['monolog']->pushHandler($handler);
} }
$logfile = __DIR__ . '/../../../../logs/task_' . $task_id . '.log'; $logfile = __DIR__ . '/../../../../logs/task_' . $task_id . '.log';
$handler = new RotatingFileHandler($logfile, 10); $this->container['monolog']->pushHandler(new RotatingFileHandler($logfile, 10));
$this->container['monolog']->pushHandler($handler);
$this->task = $task_manager->getTask($task_id, $this->container['monolog']); $this->task = $task_manager->getTask($task_id, $this->container['monolog']);
$lib2v = array( $lib2v = array(
'DEBUG' => task_abstract::LOG_DEBUG, 'DEBUG' => \task_abstract::LOG_DEBUG,
'INFO' => task_abstract::LOG_INFO, 'INFO' => \task_abstract::LOG_INFO,
'WARNING' => task_abstract::LOG_WARNING, 'WARNING' => \task_abstract::LOG_WARNING,
'ERROR' => task_abstract::LOG_ERROR, 'ERROR' => \task_abstract::LOG_ERROR,
'CRITICAL' => task_abstract::LOG_CRITICAL, 'CRITICAL' => \task_abstract::LOG_CRITICAL,
'ALERT' => task_abstract::LOG_ALERT 'ALERT' => \task_abstract::LOG_ALERT
); );
$tmpTask = $task_manager->getTask($task_id, null); $tmpTask = $task_manager->getTask($task_id, null);
$taskname = $tmpTask->getName(); $taskname = $tmpTask->getName();
unset($tmpTask); unset($tmpTask);
// log to tty ?
if (($ttyloglevel = strtoupper($input->getOption('ttyloglevel'))) != '') { if (($ttyloglevel = strtoupper($input->getOption('ttyloglevel'))) != '') {
if (!array_key_exists($ttyloglevel, $lib2v)) { if (!array_key_exists($ttyloglevel, $lib2v)) {
throw(new Alchemy\Phrasea\Exception\RuntimeException(sprintf( throw new RuntimeException("Bad value '%s' for option loglevel\nuse DEBUG|INFO|WARNING|ERROR|CRITICAL|ALERT", $ttyloglevel);
"Bad value '%s' for option loglevel\nuse DEBUG|INFO|WARNING|ERROR|CRITICAL|ALERT", $ttyloglevel))
);
} }
$handler = new Handler\StreamHandler( $handler = new StreamHandler("php://stdout", $lib2v[$ttyloglevel]);
"php://stdout",
$lib2v[$ttyloglevel],
true
);
$logger->pushHandler($handler); $logger->pushHandler($handler);
} }
@@ -148,7 +137,6 @@ class module_console_taskrun extends Command
if (function_exists('pcntl_signal')) { if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, array($this, 'sig_handler')); pcntl_signal(SIGTERM, array($this, 'sig_handler'));
pcntl_signal(SIGINT, array($this, 'sig_handler')); pcntl_signal(SIGINT, array($this, 'sig_handler'));
// pcntl_signal(SIGKILL, array($this, 'sig_handler'));
} }
try { try {

View File

@@ -638,7 +638,7 @@ abstract class task_abstract
// if something went wrong, report // if something went wrong, report
if ($exception) { if ($exception) {
throw($exception); throw $exception;
} }
} }

View File

@@ -88,11 +88,10 @@ abstract class task_databoxAbstract extends task_abstract
// get the records to process // get the records to process
$databox = $this->dependencyContainer['phraseanet.appbox']->get_databox((int) $row['sbas_id']); $databox = $this->dependencyContainer['phraseanet.appbox']->get_databox((int) $row['sbas_id']);
} catch (Exception $e) { } catch (Exception $e) {
$this->log(sprintf( $this->log(sprintf('can\'t connect to sbas(%s) because "%s"',
'can\'t connect to sbas(%s) because "%s"' $row['sbas_id'],
, $row['sbas_id'] $e->getMessage()),
, $e->getMessage()) self::LOG_WARNING
, self::LOG_WARNING
); );
continue; continue;
} }
@@ -100,10 +99,9 @@ abstract class task_databoxAbstract extends task_abstract
try { try {
$this->loadSettings(simplexml_load_string($row['settings'])); $this->loadSettings(simplexml_load_string($row['settings']));
} catch (Exception $e) { } catch (Exception $e) {
$this->log(sprintf( $this->log(sprintf('can\'t get get settings of task because "%s"',
'can\'t get get settings of task because "%s"' $e->getMessage()),
, $e->getMessage()) self::LOG_WARNING
, self::LOG_WARNING
); );
continue; continue;
} }

View File

@@ -44,10 +44,7 @@ class task_period_RecordMover extends task_appboxAbstract
{ {
$request = http_request::getInstance(); $request = http_request::getInstance();
$parm2 = $request->get_parms( $parm2 = $request->get_parms('period', 'logsql');
'period'
, 'logsql'
);
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->preserveWhiteSpace = false; $dom->preserveWhiteSpace = false;
$dom->formatOutput = true; $dom->formatOutput = true;
@@ -164,8 +161,6 @@ class task_period_RecordMover extends task_appboxAbstract
{ {
period.value = xml2.find("period").text(); period.value = xml2.find("period").text();
logsql.checked = Number(xml2.find("logsql").text()) > 0; logsql.checked = Number(xml2.find("logsql").text()) > 0;
// maxrecs.value = xml.find("maxrecs").text();
// maxmegs.value = xml.find("maxmegs").text();
} }
var data = {}; var data = {};

View File

@@ -2065,73 +2065,6 @@ class task_period_archive extends task_abstract
return $ret; return $ret;
} }
/**
* Map a bag of metadatas indexed by **FieldNames** to an array ready for
* \record_adapter metadatas submission
*
* @param \databox_descriptionStructure $metadatasStructure The databox structure related
* @param MetadataBag $metadatas The metadata bag
* @return array
*/
protected function bagToArray(\databox_descriptionStructure $metadatasStructure, MetadataBag $metadatas)
{
$metas = array();
$unicode = new \unicode();
foreach ($metadatasStructure as $databox_field) {
if ($metadatas->containsKey($databox_field->get_tag()->getTagname())) {
if ($databox_field->is_multi()) {
$values = $metadatas->get($databox_field->get_tag()->getTagname())->getValue()->asArray();
$tmp = array();
foreach ($values as $value) {
foreach (\caption_field::get_multi_values($value, $databox_field->get_separator()) as $v) {
$tmp[] = $v;
}
}
$values = array_unique($tmp);
foreach ($values as $value) {
$value = $unicode->substituteCtrlCharacters($value, ' ');
$value = $unicode->toUTF8($value);
if ($databox_field->get_type() == 'date') {
$value = $unicode->parseDate($value);
}
$metas[] = array(
'meta_struct_id' => $databox_field->get_id(),
'value' => $value,
'meta_id' => null
);
}
} else {
$value = $metadatas->get($databox_field->get_tag()->getTagname())->getValue()->asString();
$value = $unicode->substituteCtrlCharacters($value, ' ');
$value = $unicode->toUTF8($value);
if ($databox_field->get_type() == 'date') {
$value = $unicode->parseDate($value);
}
$metas[] = array(
'meta_struct_id' => $databox_field->get_id(),
'value' => $metadatas->get($databox_field->get_tag()->getTagname())->getValue()->asString(),
'meta_id' => null
);
}
}
}
unset($unicode);
return $metas;
}
protected function readXMLForDatabox(\databox_descriptionStructure $metadatasStructure, $pathfile) protected function readXMLForDatabox(\databox_descriptionStructure $metadatasStructure, $pathfile)
{ {
if (false === $this->dependencyContainer['filesystem']->exists($pathfile)) { if (false === $this->dependencyContainer['filesystem']->exists($pathfile)) {
@@ -2207,7 +2140,7 @@ class CListFolder
{ {
$this->list = array(); $this->list = array();
if ($hdir = opendir($path)) { if ($hdir = opendir($path)) {
while (false !== ($file = readdir($hdir))) { while (false !== $file = readdir($hdir)) {
$this->list[] = $file; $this->list[] = $file;
} }
closedir($hdir); closedir($hdir);

View File

@@ -45,23 +45,13 @@ class task_period_ftp extends task_appboxAbstract
{ {
$request = http_request::getInstance(); $request = http_request::getInstance();
$parm2 = $request->get_parms( $parm2 = $request->get_parms('proxy', 'proxyport', 'period', 'syslog');
'proxy'
, 'proxyport'
, 'period'
, 'syslog'
);
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->preserveWhiteSpace = false; $dom->preserveWhiteSpace = false;
$dom->formatOutput = true; $dom->formatOutput = true;
if ((@$dom->loadXML($oldxml)) != FALSE) { if ((@$dom->loadXML($oldxml)) != FALSE) {
$xmlchanged = false; $xmlchanged = false;
foreach (array( foreach (array('str:proxy', 'str:proxyport', 'str:period', 'pop:syslog') as $pname) {
'str:proxy'
, 'str:proxyport'
, 'str:period'
, 'pop:syslog'
) as $pname) {
$ptype = substr($pname, 0, 3); $ptype = substr($pname, 0, 3);
$pname = substr($pname, 4); $pname = substr($pname, 4);
$pvalue = $parm2[$pname]; $pvalue = $parm2[$pname];
@@ -354,17 +344,6 @@ class task_period_ftp extends task_appboxAbstract
$this->logger->addDebug($line); $this->logger->addDebug($line);
if (($ses_id = phrasea_create_session($usr_id)) == null) {
$this->logger->addDebug("Unable to create session");
return;
}
if (!($ph_session = phrasea_open_session($ses_id, $usr_id))) {
$this->logger->addDebug("Unable to open session");
phrasea_close_session($ses_id);
return;
}
try { try {
$ssl = ($ftp_export['ssl'] == '1'); $ssl = ($ftp_export['ssl'] == '1');
$ftp_client = $this->dependencyContainer['phraseanet.ftp.client']($ftp_server, 21, 300, $ssl, $this->proxy, $this->proxyport); $ftp_client = $this->dependencyContainer['phraseanet.ftp.client']($ftp_server, 21, 300, $ssl, $this->proxy, $this->proxyport);
@@ -558,7 +537,6 @@ class task_period_ftp extends task_appboxAbstract
unset($ftp_client); unset($ftp_client);
} }
$this->finalize($appbox, $id); $this->finalize($appbox, $id);
// phrasea_close_session($ses_id);
} }
protected function postProcessOneContent(appbox $appbox, Array $row) protected function postProcessOneContent(appbox $appbox, Array $row)

View File

@@ -8,14 +8,6 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Monolog\Logger;
class task_period_subdef extends task_databoxAbstract class task_period_subdef extends task_databoxAbstract
{ {
const MINMEGS = 20; const MINMEGS = 20;
@@ -70,24 +62,14 @@ class task_period_subdef extends task_databoxAbstract
{ {
$request = http_request::getInstance(); $request = http_request::getInstance();
$parm2 = $request->get_parms( $parm2 = $request->get_parms('period', 'flush', 'maxrecs', 'maxmegs');
'period'
, 'flush'
, 'maxrecs'
, 'maxmegs'
);
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->preserveWhiteSpace = false; $dom->preserveWhiteSpace = false;
$dom->formatOutput = true; $dom->formatOutput = true;
if (@$dom->loadXML($oldxml)) { if (@$dom->loadXML($oldxml)) {
$xmlchanged = false; $xmlchanged = false;
foreach (array( foreach (array('str:period', 'str:flush', 'str:maxrecs', 'str:maxmegs') as $pname) {
'str:period'
, 'str:flush'
, 'str:maxrecs'
, 'str:maxmegs'
) as $pname) {
$ptype = substr($pname, 0, 3); $ptype = substr($pname, 0, 3);
$pname = substr($pname, 4); $pname = substr($pname, 4);
$pvalue = $parm2[$pname]; $pvalue = $parm2[$pname];
@@ -327,7 +309,7 @@ class task_period_subdef extends task_databoxAbstract
$stmt = $connbas->prepare($sql); $stmt = $connbas->prepare($sql);
$stmt->execute(); $stmt->execute();
$stmt->closeCursor(); $stmt->closeCursor();
} catch (Exception $e) { } catch (\Exception $e) {
$this->log($e->getMessage(), self::LOG_CRITICAL); $this->log($e->getMessage(), self::LOG_CRITICAL);
} }
} }