diff --git a/lib/classes/module/console/schedulerStart.class.php b/lib/classes/module/console/schedulerStart.class.php index 6d667ea456..dc4a3180ed 100644 --- a/lib/classes/module/console/schedulerStart.class.php +++ b/lib/classes/module/console/schedulerStart.class.php @@ -15,6 +15,8 @@ * @license http://opensource.org/licenses/gpl-3.0 GPLv3 * @link www.phraseanet.com */ +use Monolog\Handler; +use Monolog\Logger; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -28,25 +30,6 @@ class module_console_schedulerStart extends Command parent::__construct($name); $this->setDescription('Start the scheduler'); - $this->addOption( - 'nolog' - , NULL - , 1 | InputOption::VALUE_NONE - , 'do not log (scheduler) to logfile' - , NULL - ); - $this->addOption( - 'notasklog' - , NULL - , 1 | InputOption::VALUE_NONE - , 'do not log (tasks) to logfiles' - , NULL - ); - $this->setHelp( - "You should use launch the command and finish it with `&`" - . " to return to the console\n\n" - . "\tie : bin/console scheduler:start &" - ); return $this; } @@ -59,9 +42,18 @@ class module_console_schedulerStart extends Command return 1; } + $logger = new Logger('Task logger'); + + $handler = new Handler\StreamHandler(fopen('php://stdout'), $input->getOption('verbose') ? Logger::DEBUG : Logger::WARNING); + $logger->pushHandler($handler); + + $logfile = __DIR__ . '/../../../../scheduler.log'; + $handler = new Handler\RotatingFileHandler($logfile, 10, $level = Logger::WARNING); + $logger->pushHandler($handler); + try { $scheduler = new task_Scheduler(); - $scheduler->run($input, $output); + $scheduler->run($logger); } catch (\Exception $e) { switch ($e->getCode()) { case task_Scheduler::ERR_ALREADY_RUNNING: // 114 : aka EALREADY (Operation already in progress) diff --git a/lib/classes/module/console/taskrun.class.php b/lib/classes/module/console/taskrun.class.php index 4034c2a5de..c537b8a007 100755 --- a/lib/classes/module/console/taskrun.class.php +++ b/lib/classes/module/console/taskrun.class.php @@ -9,17 +9,20 @@ * file that was distributed with this source code. */ +use Monolog\Handler; +use Monolog\Logger; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Command\Command; + /** * @todo write tests * * @license http://opensource.org/licenses/gpl-3.0 GPLv3 * @link www.phraseanet.com */ -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Command\Command; class module_console_taskrun extends Command { @@ -95,14 +98,26 @@ class module_console_taskrun extends Command } } + $logger = new Logger('Task logger'); + + if ($input->getOption('verbose')) { + $handler = new Handler\StreamHandler(fopen('php://stdout', 'a')); + $logger->pushHandler($handler); + } + + $logfile = __DIR__ . '/../../../../task_l_' . $task_id . '.log'; + $handler = new Handler\RotatingFileHandler($logfile, 10, $level = Logger::WARNING); + $logger->pushHandler($handler); + register_tick_function(array($this, 'tick_handler'), true); declare(ticks = 1); + if (function_exists('pcntl_signal')) { pcntl_signal(SIGTERM, array($this, 'sig_handler')); } try { - $this->task->run($runner, $input, $output); + $this->task->run($runner, $logger); } catch (Exception $e) { $this->task->log(sprintf("taskrun : exception from 'run()', %s \n", $e->getMessage())); diff --git a/lib/classes/record/Interface.class.php b/lib/classes/record/Interface.class.php index 5c554d380d..80e1735260 100644 --- a/lib/classes/record/Interface.class.php +++ b/lib/classes/record/Interface.class.php @@ -10,6 +10,7 @@ */ use MediaVorus\Media\Media; +use Monolog\Logger; /** * @@ -99,7 +100,7 @@ interface record_Interface public function delete(); - public function generate_subdefs(databox $databox, Array $wanted_subdefs = null); + public function generate_subdefs(databox $databox, Logger $logger = null, Array $wanted_subdefs = null); public function log_view($log_id, $referrer, $gv_sit); diff --git a/lib/classes/record/adapter.class.php b/lib/classes/record/adapter.class.php index fd8888ec7a..0a331daccc 100644 --- a/lib/classes/record/adapter.class.php +++ b/lib/classes/record/adapter.class.php @@ -9,6 +9,7 @@ * file that was distributed with this source code. */ +use Monolog\Logger; use MediaVorus\Media\Media; use Symfony\Component\HttpFoundation\File\File as SymfoFile; @@ -1647,15 +1648,23 @@ class record_adapter implements record_Interface, cache_cacheableInterface } /** + * Generates subdefs * - * @param databox $databox + * @param databox $databox The databox + * @param \Monolog\Logger $logger A logger for binary operation + * @param array $wanted_subdefs An array of subdef names + * @return \record_adapter */ - public function generate_subdefs(databox $databox, Array $wanted_subdefs = null) + public function generate_subdefs(databox $databox, Logger $logger = null, Array $wanted_subdefs = null) { $subdefs = $databox->get_subdef_structure()->getSubdefGroup($this->get_type()); $Core = bootstrap::getCore(); + if ( ! $logger) { + $logger = $Core['monolog']; + } + if ( ! $subdefs) { $Core['monolog']->addInfo(sprintf('Nothing to do for %s', $this->get_type())); @@ -1682,7 +1691,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface $pathdest = $this->generateSubdefPathname($subdef, $pathdest); - $this->generate_subdef($subdef, $pathdest); + $this->generate_subdef($subdef, $pathdest, $logger); if (file_exists($pathdest)) { $media = \MediaVorus\MediaVorus::guess(new \SplFileInfo($pathdest)); @@ -1712,12 +1721,14 @@ class record_adapter implements record_Interface, cache_cacheableInterface } /** + * Generate a subdef * - * @todo move to media_subdef class - * @param databox_subdef $subdef_class - * @param string $pathdest + * @param databox_subdef $subdef_class The related databox subdef + * @param type $pathdest The destination of the file + * @param Logger $logger A logger for binary operation + * @return \record_adapter */ - protected function generate_subdef(databox_subdef $subdef_class, $pathdest) + protected function generate_subdef(databox_subdef $subdef_class, $pathdest, Logger $logger) { $Core = \bootstrap::getCore(); @@ -1730,7 +1741,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface $Core['media-alchemyst']->turnInto($pathdest, $subdef_class->getSpecs()); $Core['media-alchemyst']->close(); } catch (\MediaAlchemyst\Exception\Exception $e) { - $Core['monolog']->addError(sprintf('Subdef generation failed with message %s', $e->getMessage())); + $logger->addError(sprintf('Subdef generation failed for record %d with message %s', $this->get_record_id(), $e->getMessage())); } return $this; diff --git a/lib/classes/task/Scheduler.class.php b/lib/classes/task/Scheduler.class.php index cb9c62336f..a4af0509b5 100755 --- a/lib/classes/task/Scheduler.class.php +++ b/lib/classes/task/Scheduler.class.php @@ -26,25 +26,18 @@ class task_Scheduler const ERR_ALREADY_RUNNING = 114; // aka EALREADY (Operation already in progress) + /** + * + * @var \Monolog\Logger + */ + private $logger; private $method; private $input; protected $output; protected function log($message) { - $registry = registry::get_instance(); - $logdir = $registry->get('GV_RootPath') . 'logs/'; - - logs::rotate($logdir . "scheduler_l.log"); - logs::rotate($logdir . "scheduler_o.log"); - logs::rotate($logdir . "scheduler_e.log"); - - $date_obj = new DateTime(); - $message = sprintf("%s\t%s", $date_obj->format(DATE_ATOM), $message); - - if ($this->input && ! ($this->input->getOption('nolog'))) { - file_put_contents($logdir . "scheduler_l.log", $message . "\n", FILE_APPEND); - } + $this->logger->addInfo($message); return $this; } @@ -58,11 +51,10 @@ class task_Scheduler * @throws Exception if scheduler is already running * @todo doc all possible exception */ - public function run(InputInterface $input = null, OutputInterface $output = null) + public function run(\Monolog\Logger $logger) { - require_once dirname(__FILE__) . '/../../bootstrap.php'; - $this->input = $input; - $this->output = $output; + $this->logger = $logger; + $appbox = appbox::get_instance(\bootstrap::getCore()); $registry = $appbox->get_registry(); @@ -119,8 +111,6 @@ class task_Scheduler pcntl_signal(SIGCHLD, SIG_IGN); } - $logdir = $registry->get('GV_RootPath') . 'logs/'; - $conn = appbox::get_instance(\bootstrap::getCore())->get_connection(); $taskPoll = array(); // the poll of tasks @@ -216,10 +206,6 @@ class task_Scheduler $this->log("schedstatus == 'stopping', waiting tasks to end"); } - logs::rotate($logdir . "scheduler_t.log"); - logs::rotate($logdir . "scheduler_o.log"); - logs::rotate($logdir . "scheduler_e.log"); - // initialy, all tasks are supposed to be removed from the poll foreach ($taskPoll as $tkey => $task) { $taskPoll[$tkey]["todel"] = true; @@ -229,10 +215,6 @@ class task_Scheduler $tkey = "t_" . $task->getID(); $status = $task->getState(); - logs::rotate($logdir . "task_t_" . $task->getID() . ".log"); - logs::rotate($logdir . "task_o_" . $task->getID() . ".log"); - logs::rotate($logdir . "task_e_" . $task->getID() . ".log"); - if ( ! isset($taskPoll[$tkey])) { // the task is not in the poll, add it $phpcli = $registry->get('GV_cli'); @@ -240,18 +222,12 @@ class task_Scheduler case "WINDOWS": $cmd = $phpcli; $args = array('-f', $registry->get('GV_RootPath') . 'bin/console', '--', '-q', 'task:run', $task->getID(), '--runner=scheduler'); - if ($this->input && ($this->input->getOption('notasklog'))) { - $args[] = 'notasklog'; - } break; default: case "DARWIN": case "LINUX": $cmd = $phpcli; $args = array('-f', $registry->get('GV_RootPath') . 'bin/console', '--', '-q', 'task:run', $task->getID(), '--runner=scheduler'); - if ($this->input && ($this->input->getOption('notasklog'))) { - $args[] = 'notasklog'; - } break; } @@ -344,8 +320,10 @@ class task_Scheduler if ($this->method == self::METHOD_PROC_OPEN) { if ( ! $taskPoll[$tkey]["process"]) { - $descriptors[1] = array('file', $logdir . "task_o_" . $taskPoll[$tkey]['task']->getID() . ".log", 'a+'); - $descriptors[2] = array('file', $logdir . "task_e_" . $taskPoll[$tkey]['task']->getID() . ".log", 'a+'); + $tmpFile = tempnam(sys_get_temp_dir(), 'task'); + + $descriptors[1] = array('file', $tmpFile, 'a+'); + $descriptors[2] = array('file', $tmpFile, 'a+'); $taskPoll[$tkey]["process"] = proc_open( $taskPoll[$tkey]["cmd"] . ' ' . implode(' ', $taskPoll[$tkey]["args"]) diff --git a/lib/classes/task/abstract.class.php b/lib/classes/task/abstract.class.php index 85a0cb3706..9b74ead79e 100755 --- a/lib/classes/task/abstract.class.php +++ b/lib/classes/task/abstract.class.php @@ -1,5 +1,7 @@ in task interface */ public function printInterfaceHEAD() @@ -121,7 +128,7 @@ abstract class task_abstract return false; } - /* + /** * to be overwritten by tasks : echo javascript to be included in in task interface */ public function printInterfaceJS() @@ -139,9 +146,9 @@ abstract class task_abstract } /** - * - * @return boolean - */ + * + * @return boolean + */ public function getGraphicForm() { return false; @@ -489,10 +496,9 @@ abstract class task_abstract return $lockFD; } - final public function run($runner, $input = null, $output = null) + final public function run($runner, $logger) { - $this->input = $input; - $this->output = $output; + $this->logger = $logger; $lockFD = $this->lockTask(); @@ -503,7 +509,7 @@ abstract class task_abstract $exception = NULL; try { $this->run2(); - } catch (Exception $exception) { + } catch (\Exception $exception) { } @@ -669,27 +675,12 @@ abstract class task_abstract $d = debug_backtrace(false); $lastt = $t; - echo "\n" . memory_get_usage() . " -- " . memory_get_usage(true) . "\n"; + $this->logger->addDebug(memory_get_usage() . " -- " . memory_get_usage(true)); } public function log($message) { - $registry = registry::get_instance(); - $logdir = $registry->get('GV_RootPath') . 'logs/'; - - logs::rotate($logdir . 'task_l_' . $this->taskid . '.log'); - logs::rotate($logdir . 'task_o_' . $this->taskid . '.log'); - logs::rotate($logdir . 'task_e_' . $this->taskid . '.log'); - - $date_obj = new DateTime(); - $message = sprintf("%s\t%s", $date_obj->format(DATE_ATOM), $message); - - if ($this->output) { - $this->output->writeln($message); - } - if ($this->input && ! ($this->input->getOption('nolog'))) { - file_put_contents($logdir . 'task_l_' . $this->taskid . '.log', $message . "\n", FILE_APPEND); - } + $this->logger->addInfo($message); return $this; } diff --git a/templates/web/prod/index.html.twig b/templates/web/prod/index.html.twig index fc8b8c08e2..68b7734542 100644 --- a/templates/web/prod/index.html.twig +++ b/templates/web/prod/index.html.twig @@ -169,6 +169,8 @@ + + @@ -1089,8 +1091,8 @@