Update task manager commands

This commit is contained in:
Romain Neutron
2013-10-01 18:34:42 +02:00
parent 58c402ac2b
commit afd9d33b9a
21 changed files with 558 additions and 555 deletions

View File

@@ -1,65 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @package KonsoleKomander
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Alchemy\Phrasea\Command\Command;
use Monolog\Handler;
use Monolog\Logger;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class module_console_schedulerStart extends Command
{
public function __construct($name = null)
{
parent::__construct($name);
$this->setDescription('Starts Phraseanet scheduler');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$logger = new Logger('Task logger');
$streamHandler = new Handler\StreamHandler('php://stdout', $input->getOption('verbose') ? Logger::DEBUG : Logger::WARNING);
$logger->pushHandler($streamHandler);
$logfile = __DIR__ . '/../../../../logs/scheduler.log';
$rotateHandler = new Handler\RotatingFileHandler($logfile, 10);
$logger->pushHandler($rotateHandler);
try {
$scheduler = new task_Scheduler($this->container, $logger);
$scheduler->run();
} catch (\Exception $e) {
switch ($e->getCode()) {
// 114 : aka EALREADY (Operation already in progress)
case task_Scheduler::ERR_ALREADY_RUNNING:
$exitCode = task_Scheduler::ERR_ALREADY_RUNNING;
break;
default:
$exitCode = 1; // default exit code (error)
break;
}
return $exitCode;
}
}
}

View File

@@ -1,85 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @package KonsoleKomander
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class module_console_schedulerState extends Command
{
const EXITCODE_SETUP_ERROR = 1;
const EXITCODE_STATE_UNKNOWN = 21;
private $stateToExitCode = array(
\task_manager::STATE_TOSTOP => 13,
\task_manager::STATE_STARTED => 10,
\task_manager::STATE_STOPPING => 12,
\task_manager::STATE_STOPPED => 11,
);
public function __construct($name = null)
{
parent::__construct($name);
$this->setDescription('Returns Phraseanet scheduler status');
$this->addOption(
'short'
, NULL
, InputOption::VALUE_NONE
, 'print short result, ie: <info>stopped()</info> | <info>started(12345)</info> | <info>tostop(12345)</info> | <info>stopping(12345)</info>'
, NULL
);
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
if (!$this->container['phraseanet.configuration-tester']->isInstalled()) {
return self::EXITCODE_SETUP_ERROR;
}
$task_manager = $this->container['task-manager'];
$exitCode = 0;
$state = $task_manager->getSchedulerState();
if ($input->getOption('short')) {
$output->writeln(sprintf('%s(%s)', $state['status'], $state['pid']));
} else {
if ($state['pid'] != NULL) {
$output->writeln(sprintf(
'Scheduler is %s on pid %d'
, $state['status']
, $state['pid']
));
} else {
$output->writeln(sprintf('Scheduler is %s', $state['status']));
}
}
if (array_key_exists($state['status'], $this->stateToExitCode)) {
$exitCode = $this->stateToExitCode[$state['status']];
} else {
$exitCode = self::EXITCODE_STATE_UNKNOWN;
}
return $exitCode;
}
}

View File

@@ -1,47 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @package KonsoleKomander
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class module_console_schedulerStop extends Command
{
public function __construct($name = null)
{
parent::__construct($name);
$this->setDescription('Stops Phraseanet scheduler');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
try {
$task_manager = $this->container['task-manager'];
$task_manager->setSchedulerState(task_manager::STATE_TOSTOP);
return 0;
} catch (\Exception $e) {
return 1;
}
return 0;
}
}

View File

@@ -1,117 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @package KonsoleKomander
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Alchemy\Phrasea\Command\Command;
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\HttpKernel\Exception\NotFoundHttpException;
class module_console_taskState extends Command
{
const EXITCODE_SETUP_ERROR = 1;
const EXITCODE_BAD_ARGUMENT = 2;
const EXITCODE_FATAL_ERROR = 3;
const EXITCODE_TASK_UNKNOWN = 20;
const EXITCODE_STATE_UNKNOWN = 21;
private $stateToExitCode = array(
\task_abstract::STATE_TOSTOP => 13,
\task_abstract::STATE_STARTED => 10,
\task_abstract::STATE_TOSTART => 14,
\task_abstract::STATE_TORESTART => 15,
\task_abstract::STATE_STOPPED => 11,
\task_abstract::STATE_TODELETE => 16
);
public function __construct($name = null)
{
parent::__construct($name);
$this->addArgument('task_id', InputArgument::REQUIRED, 'The task_id to test');
$this->setDescription('Returns a Phraseanet task state given its id');
$this->addOption(
'short'
, NULL
, InputOption::VALUE_NONE
, 'print short result, ie: <info>stopped()</info> | <info>started(12345)</info> | <info>tostop(12345)</info> | <info>...</info>'
, NULL
);
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
if (!$this->container['phraseanet.configuration-tester']->isInstalled()) {
return self::EXITCODE_SETUP_ERROR;
}
$task_id = (int) $input->getArgument('task_id');
if ($task_id <= 0 || strlen($task_id) !== strlen($input->getArgument('task_id'))) {
$output->writeln($input->getOption('short') ? 'bad_id' : 'Argument must be an ID');
return self::EXITCODE_BAD_ARGUMENT;
}
$task_manager = $this->container['task-manager'];
$taskPID = $taskState = NULL;
$exitCode = 0;
$task = NULL;
try {
$task = $task_manager->getTask($task_id);
$taskPID = $task->getPID();
$taskState = $task->getState();
} catch (NotFoundHttpException $e) {
$output->writeln($input->getOption('short') ? 'unknown_id' : $e->getMessage());
return self::EXITCODE_TASK_UNKNOWN;
} catch (Exception $e) {
$output->writeln($input->getOption('short') ? 'fatal_error' : $e->getMessage());
return self::EXITCODE_FATAL_ERROR;
}
if ($input->getOption('short')) {
$output->writeln(sprintf('%s(%s)', $taskState, $taskPID));
} else {
if ($taskPID !== NULL) {
$output->writeln(sprintf(
'Task %d is %s on pid %d'
, $task_id
, $taskState
, $taskPID
));
} else {
$output->writeln(sprintf('Task %d is %s', $task_id, $taskState));
}
}
if (array_key_exists($taskState, $this->stateToExitCode)) {
$exitCode = $this->stateToExitCode[$taskState];
} else {
$exitCode = self::EXITCODE_STATE_UNKNOWN;
}
return $exitCode;
}
}

View File

@@ -1,66 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @todo write tests
*
* @package KonsoleKomander
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class module_console_tasklist extends Command
{
public function __construct($name = null)
{
parent::__construct($name);
$this->setDescription('Lists Phraseanet tasks');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
if (!$this->container['phraseanet.configuration-tester']->isInstalled()) {
return self::EXITCODE_SETUP_ERROR;
}
try {
$task_manager = $this->container['task-manager'];
$tasks = $task_manager->getTasks();
if (count($tasks) === 0) {
$output->writeln('No tasks on your install !');
}
foreach ($tasks as $task) {
$this->printTask($task, $output);
}
return 0;
} catch (\Exception $e) {
return 1;
}
}
protected function printTask(task_abstract $task, OutputInterface $output)
{
$message = $task->getID() . "\t" . ($task->getState() ) . "\t" . $task->getTitle();
$output->writeln($message);
return $this;
}
}

View File

@@ -1,169 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Exception\RuntimeException;
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 Monolog\Handler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
/**
* @todo write tests
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class module_console_taskrun extends Command
{
private $task;
private $shedulerPID;
public function __construct($name = null)
{
parent::__construct($name);
$this->task = NULL;
$this->shedulerPID = NULL;
$this
->addArgument('task_id', InputArgument::REQUIRED, 'The task_id to run')
->addOption(
'runner',
'r',
InputOption::VALUE_REQUIRED,
'The name of the runner (manual, scheduler...)',
task_abstract::RUNNER_MANUAL
)
->addOption(
'ttyloglevel',
't',
InputOption::VALUE_REQUIRED,
'threshold : (DEBUG|INFO|WARNING|ERROR|CRITICAL|ALERT)',
''
)
->setDescription('Runs a Phraseanet task given its id');
return $this;
}
public function sig_handler($signo)
{
if ($this->task) {
$this->task->log(sprintf("signal %s received", $signo));
$this->task->setRunning(false);
}
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
if (!$this->container['phraseanet.configuration-tester']->isInstalled()) {
return self::EXITCODE_SETUP_ERROR;
}
$task_id = (int) $input->getArgument('task_id');
if ($task_id <= 0 || strlen($task_id) !== strlen($input->getArgument('task_id'))) {
throw new \RuntimeException('Argument must be an Id.');
}
$task_manager = $this->container['task-manager'];
$logger = $task_manager->getLogger();
if ($input->getOption('runner') === task_abstract::RUNNER_MANUAL) {
$schedStatus = $task_manager->getSchedulerState();
if ($schedStatus && $schedStatus['status'] == task_abstract::STATE_STARTED && $schedStatus['pid']) {
$this->shedulerPID = $schedStatus['pid'];
}
$runner = task_abstract::RUNNER_MANUAL;
} else {
$runner = task_abstract::RUNNER_SCHEDULER;
$schedStatus = $task_manager->getSchedulerState();
if ($schedStatus && $schedStatus['status'] == task_abstract::STATE_STARTED && $schedStatus['pid']) {
$this->shedulerPID = $schedStatus['pid'];
}
}
$logfile = __DIR__ . '/../../../../logs/task_' . $task_id . '.log';
$this->container['task-manager.logger']->pushHandler(new RotatingFileHandler($logfile, 10));
$this->task = $task_manager->getTask($task_id, $this->container['task-manager.logger']);
$lib2v = array(
'DEBUG' => \task_abstract::LOG_DEBUG,
'INFO' => \task_abstract::LOG_INFO,
'WARNING' => \task_abstract::LOG_WARNING,
'ERROR' => \task_abstract::LOG_ERROR,
'CRITICAL' => \task_abstract::LOG_CRITICAL,
'ALERT' => \task_abstract::LOG_ALERT
);
$tmpTask = $task_manager->getTask($task_id, null);
unset($tmpTask);
if (($ttyloglevel = strtoupper($input->getOption('ttyloglevel'))) != '') {
if (!array_key_exists($ttyloglevel, $lib2v)) {
throw(new RuntimeException(sprintf(
"Bad value '%s' for option loglevel\nuse DEBUG|INFO|WARNING|ERROR|CRITICAL|ALERT", $ttyloglevel))
);
}
$handler = new StreamHandler("php://stdout", $lib2v[$ttyloglevel]);
$logger->pushHandler($handler);
}
$this->task = $task_manager->getTask($task_id, $logger);
register_tick_function(array($this, 'tick_handler'), true);
declare(ticks = 1);
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, array($this, 'sig_handler'));
pcntl_signal(SIGINT, array($this, 'sig_handler'));
}
try {
$this->task->run($runner);
} catch (Exception $e) {
$this->task->log(sprintf("taskrun : exception from 'run()', %s \n", $e->getMessage()));
return($e->getCode());
}
if ($input->getOption('runner') === task_abstract::RUNNER_MANUAL) {
$runner = task_abstract::RUNNER_MANUAL;
}
}
public function tick_handler()
{
static $start;
if ($start === null) {
$start = time();
}
if (time() - $start > 0) {
if ($this->shedulerPID) {
if (function_exists('posix_kill') && !posix_kill($this->shedulerPID, 0)) {
if (method_exists($this->task, 'signal')) {
$this->task->signal('SIGNAL_SCHEDULER_DIED');
} else {
$this->task->setState(task_abstract::STATE_TOSTOP);
}
}
$start = time();
}
}
}
}