Remove platform detection

This commit is contained in:
Romain Neutron
2012-05-24 14:52:20 +02:00
parent a3c87fdf10
commit 2b7c196d13
13 changed files with 106 additions and 185 deletions

View File

@@ -52,8 +52,8 @@ class module_console_schedulerStart extends Command
$logger->pushHandler($handler); $logger->pushHandler($handler);
try { try {
$scheduler = new task_Scheduler(); $scheduler = new task_Scheduler($logger);
$scheduler->run($logger); $scheduler->run();
} catch (\Exception $e) { } catch (\Exception $e) {
switch ($e->getCode()) { switch ($e->getCode()) {
case task_Scheduler::ERR_ALREADY_RUNNING: // 114 : aka EALREADY (Operation already in progress) case task_Scheduler::ERR_ALREADY_RUNNING: // 114 : aka EALREADY (Operation already in progress)

View File

@@ -81,7 +81,6 @@ class module_console_taskrun extends Command
$appbox = \appbox::get_instance(\bootstrap::getCore()); $appbox = \appbox::get_instance(\bootstrap::getCore());
$task_manager = new task_manager($appbox); $task_manager = new task_manager($appbox);
$this->task = $task_manager->getTask($task_id);
if ($input->getOption('runner') === task_abstract::RUNNER_MANUAL) { if ($input->getOption('runner') === task_abstract::RUNNER_MANUAL) {
$schedStatus = $task_manager->getSchedulerState(); $schedStatus = $task_manager->getSchedulerState();
@@ -109,6 +108,8 @@ class module_console_taskrun extends Command
$handler = new Handler\RotatingFileHandler($logfile, 10, $level = Logger::WARNING); $handler = new Handler\RotatingFileHandler($logfile, 10, $level = Logger::WARNING);
$logger->pushHandler($handler); $logger->pushHandler($handler);
$this->task = $task_manager->getTask($task_id, $logger);
register_tick_function(array($this, 'tick_handler'), true); register_tick_function(array($this, 'tick_handler'), true);
declare(ticks = 1); declare(ticks = 1);
@@ -117,7 +118,7 @@ class module_console_taskrun extends Command
} }
try { try {
$this->task->run($runner, $logger); $this->task->run($runner);
} catch (Exception $e) { } catch (Exception $e) {
$this->task->log(sprintf("taskrun : exception from 'run()', %s \n", $e->getMessage())); $this->task->log(sprintf("taskrun : exception from 'run()', %s \n", $e->getMessage()));

View File

@@ -237,42 +237,56 @@ class setup
public static function discover_binaries() public static function discover_binaries()
{ {
if (system_server::get_platform() == 'WINDOWS') { $finder = new \Symfony\Component\Process\ExecutableFinder();
$indexer = dirname(dirname(__DIR__)) . '/bin/phraseanet_indexer.exe';
} else {
$indexer = null;
}
return array( return array(
'php' => array('name' => 'PHP CLI', 'binary' => self::discover_binary('php')), 'php' => array(
'phraseanet_indexer' => array('name' => 'Indexeur Phrasea', 'binary' => self::discover_binary('phraseanet_indexer', array($indexer))), 'name' => 'PHP CLI',
'convert' => array('name' => 'ImageMagick (convert)', 'binary' => self::discover_binary('convert')), 'binary' => $finder->find('php')
'composite' => array('name' => 'ImageMagick (composite)', 'binary' => self::discover_binary('composite')), ),
'pdf2swf' => array('name' => 'PDF 2 SWF', 'binary' => self::discover_binary('pdf2swf')), 'phraseanet_indexer' => array(
'unoconv' => array('name' => 'Unoconv', 'binary' => self::discover_binary('unoconv')), 'name' => 'Indexeur Phrasea',
'swfextract' => array('name' => 'SWFextract', 'binary' => self::discover_binary('swfextract')), 'binary' => $finder->find('phraseanet_indexer')
'swfrender' => array('name' => 'SWFrender', 'binary' => self::discover_binary('swfrender')), ),
'MP4Box' => array('name' => 'MP4Box', 'binary' => self::discover_binary('MP4Box')), 'convert' => array(
'xpdf' => array('name' => 'XPDF', 'binary' => self::discover_binary('xpdf')), 'name' => 'ImageMagick (convert)',
'ffmpeg' => array('name' => 'FFmpeg', 'binary' => self::discover_binary('ffmpeg')), 'binary' => $finder->find('convert')
),
'composite' => array(
'name' => 'ImageMagick (composite)',
'binary' => $finder->find('composite')
),
'pdf2swf' => array(
'name' => 'PDF 2 SWF',
'binary' => $finder->find('pdf2swf')
),
'unoconv' => array(
'name' => 'Unoconv',
'binary' => $finder->find('unoconv')
),
'swfextract' => array(
'name' => 'SWFextract',
'binary' => $finder->find('swfextract')
),
'swfrender' => array(
'name' => 'SWFrender',
'binary' => $finder->find('swfrender')
),
'MP4Box' => array(
'name' => 'MP4Box',
'binary' => $finder->find('MP4Box')
),
'xpdf' => array(
'name' => 'XPDF',
'binary' => $finder->find('xpdf')
),
'ffmpeg' => array(
'name' => 'FFmpeg',
'binary' => $finder->find('ffmpeg')
),
); );
} }
protected static function discover_binary($binary, array $look_here = array())
{
if (system_server::get_platform() == 'WINDOWS') {
return null;
}
foreach ($look_here as $place) {
if (is_executable($place)) {
return $place;
}
}
return exec(sprintf('which %s', $binary));
}
public function check_mod_auth_token() public function check_mod_auth_token()
{ {
$registry = registry::get_instance(); $registry = registry::get_instance();
@@ -473,7 +487,6 @@ class setup
</form> </form>
<?php <?php
return; return;
} }

View File

@@ -76,24 +76,4 @@ class system_server
return false; return false;
} }
/**
* Return server platform name
*
* @staticvar string $_system
* @return string
*/
public static function get_platform()
{
static $_system = NULL;
if ($_system === NULL) {
$_system = strtoupper(php_uname('s'));
if ($_system == 'WINDOWS NT') {
$_system = 'WINDOWS';
}
}
return($_system);
}
} }

View File

@@ -9,21 +9,20 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Monolog\Logger;
use Symfony\Component\Console\Output\OutputInterface;
/** /**
* *
* @license http://opensource.org/licenses/gpl-3.0 GPLv3 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com * @link www.phraseanet.com
*/ */
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class task_Scheduler class task_Scheduler
{ {
const TASKDELAYTOQUIT = 60; const TASKDELAYTOQUIT = 60;
// how to schedule tasks (choose in 'run' method) // how to schedule tasks (choose in 'run' method)
const METHOD_FORK = 'METHOD_FORK'; const METHOD_FORK = 'METHOD_FORK';
const METHOD_PROC_OPEN = 'METHOD_PROC_OPEN'; const METHOD_PROC_OPEN = 'METHOD_PROC_OPEN';
const ERR_ALREADY_RUNNING = 114; // aka EALREADY (Operation already in progress) const ERR_ALREADY_RUNNING = 114; // aka EALREADY (Operation already in progress)
/** /**
@@ -32,8 +31,11 @@ class task_Scheduler
*/ */
private $logger; private $logger;
private $method; private $method;
private $input;
protected $output; public function __construct(Logger $logger)
{
$this->logger = $logger;
}
protected function log($message) protected function log($message)
{ {
@@ -47,30 +49,25 @@ class task_Scheduler
return appbox::get_instance(\bootstrap::getCore())->get_connection(); return appbox::get_instance(\bootstrap::getCore())->get_connection();
} }
/* /**
* @throws Exception if scheduler is already running * @throws Exception if scheduler is already running
* @todo doc all possible exception * @todo doc all possible exception
*/ */
public function run(\Monolog\Logger $logger) public function run()
{ {
$this->logger = $logger;
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = appbox::get_instance(\bootstrap::getCore());
$registry = $appbox->get_registry(); $registry = $appbox->get_registry();
$nullfile = ''; $this->method = self::METHOD_PROC_OPEN;
$system = system_server::get_platform();
switch ($system) { $nullfile = '/dev/null';
case "WINDOWS":
$nullfile = 'NUL'; if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->method = self::METHOD_PROC_OPEN; $nullfile = 'NUL';
break; }
default:
case "DARWIN": if (function_exists('pcntl_fork')) {
case "LINUX": $this->method = self::METHOD_FORK;
$nullfile = '/dev/null';
$this->method = self::METHOD_FORK;
break;
} }
$lockdir = $registry->get('GV_RootPath') . 'tmp/locks/'; $lockdir = $registry->get('GV_RootPath') . 'tmp/locks/';
@@ -320,10 +317,11 @@ class task_Scheduler
if ($this->method == self::METHOD_PROC_OPEN) { if ($this->method == self::METHOD_PROC_OPEN) {
if ( ! $taskPoll[$tkey]["process"]) { if ( ! $taskPoll[$tkey]["process"]) {
$tmpFile = tempnam(sys_get_temp_dir(), 'task');
$descriptors[1] = array('file', $tmpFile, 'a+'); $nullfile = defined('PHP_WINDOWS_VERSION_BUILD') ? 'NUL' : '/dev/null';
$descriptors[2] = array('file', $tmpFile, 'a+');
$descriptors[1] = array('file', $nullfile, 'a+');
$descriptors[2] = array('file', $nullfile, 'a+');
$taskPoll[$tkey]["process"] = proc_open( $taskPoll[$tkey]["process"] = proc_open(
$taskPoll[$tkey]["cmd"] . ' ' . implode(' ', $taskPoll[$tkey]["args"]) $taskPoll[$tkey]["cmd"] . ' ' . implode(' ', $taskPoll[$tkey]["args"])

View File

@@ -283,26 +283,15 @@ abstract class task_abstract
abstract public function help(); abstract public function help();
public function __construct($taskid) public function __construct($taskid, Logger $logger)
{ {
$this->logger = $logger;
$this->taskid = $taskid; $this->taskid = $taskid;
phrasea::use_i18n(Session_Handler::get_locale()); phrasea::use_i18n(Session_Handler::get_locale());
$this->system = system_server::get_platform();
$this->launched_by = array_key_exists("REQUEST_URI", $_SERVER) ? self::LAUCHED_BY_BROWSER : self::LAUCHED_BY_COMMANDLINE; $this->launched_by = array_key_exists("REQUEST_URI", $_SERVER) ? self::LAUCHED_BY_BROWSER : self::LAUCHED_BY_COMMANDLINE;
if ($this->system != "DARWIN" && $this->system != "WINDOWS" && $this->system != "LINUX") {
if ($this->launched_by == self::LAUCHED_BY_COMMANDLINE) {
// printf("Desole, ce programme ne fonctionne pas sous '" . $this->system . "'.\n");
flush();
}
exit(-1);
} else {
if ($this->launched_by == self::LAUCHED_BY_COMMANDLINE) {
flush();
}
}
try { try {
$conn = connection::getPDOConnection(); $conn = connection::getPDOConnection();
@@ -496,10 +485,8 @@ abstract class task_abstract
return $lockFD; return $lockFD;
} }
final public function run($runner, $logger) final public function run($runner)
{ {
$this->logger = $logger;
$lockFD = $this->lockTask(); $lockFD = $this->lockTask();
$this->setRunner($runner); $this->setRunner($runner);
@@ -680,7 +667,9 @@ abstract class task_abstract
public function log($message) public function log($message)
{ {
$this->logger->addInfo($message); if ($this->logger) {
$this->logger->addInfo($message);
}
return $this; return $this;
} }

View File

@@ -16,10 +16,6 @@
*/ */
abstract class task_databoxAbstract extends task_abstract abstract class task_databoxAbstract extends task_abstract
{ {
// abstract public function help();
//
// abstract public function getName();
protected $mono_sbas_id; protected $mono_sbas_id;
abstract protected function retrieveSbasContent(databox $databox); abstract protected function retrieveSbasContent(databox $databox);

View File

@@ -37,6 +37,8 @@ class task_manager
return $this->tasks; return $this->tasks;
} }
$core = \bootstrap::getCore();
$sql = "SELECT task2.* FROM task2 ORDER BY task_id ASC"; $sql = "SELECT task2.* FROM task2 ORDER BY task_id ASC";
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->appbox->get_connection()->prepare($sql);
$stmt->execute(); $stmt->execute();
@@ -45,9 +47,6 @@ class task_manager
$tasks = array(); $tasks = array();
$appbox = \appbox::get_instance(\bootstrap::getCore());
$lockdir = $appbox->get_registry()->get('GV_RootPath') . 'tmp/locks/';
foreach ($rs as $row) { foreach ($rs as $row) {
$row['pid'] = NULL; $row['pid'] = NULL;
@@ -56,21 +55,7 @@ class task_manager
continue; continue;
} }
try { try {
// if( ($lock = fopen( $lockdir . 'task.'.$row['task_id'].'.lock', 'a+')) ) $tasks[$row['task_id']] = new $classname($row['task_id'], $core['monolog']);
// {
// if (flock($lock, LOCK_SH | LOCK_NB) === FALSE)
// {
// // already locked : running !
// $row['pid'] = fgets($lock, 512);
// }
// else
// {
// // can lock : not running
// flock($lock, LOCK_UN);
// }
// fclose($lock);
// }
$tasks[$row['task_id']] = new $classname($row['task_id']);
} catch (Exception $e) { } catch (Exception $e) {
} }

View File

@@ -202,7 +202,7 @@ class task_period_cindexer extends task_abstract
public function printInterfaceJS() public function printInterfaceJS()
{ {
$appname = 'phraseanet_indexer'; $appname = 'phraseanet_indexer';
if ($this->system == 'WINDOWS') { if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$appname .= '.exe'; $appname .= '.exe';
} }
?> ?>
@@ -277,17 +277,17 @@ class task_period_cindexer extends task_abstract
public function printInterfaceHTML() public function printInterfaceHTML()
{ {
$appname = 'phraseanet_indexer'; $appname = 'phraseanet_indexer';
if ($this->system == 'WINDOWS') { if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$appname .= '.exe'; $appname .= '.exe';
} }
ob_start(); ob_start();
?> ?>
<form name="graphicForm" onsubmit="return(false);" method="post"> <form name="graphicForm" onsubmit="return(false);" method="post">
<br/> <br/>
<?php echo _('task::cindexer:executable') ?>&nbsp;:&nbsp; <?php echo _('task::cindexer:executable') ?>&nbsp;:&nbsp;
<input type="text" name="binpath" style="width:300px;" onchange="chgxmltxt(this, 'binpath');" value="">&nbsp;/&nbsp;<?php echo $appname ?> <input type="text" name="binpath" style="width:300px;" onchange="chgxmltxt(this, 'binpath');" value="">&nbsp;/&nbsp;<?php echo $appname ?>
<br/> <br/>
<?php echo _('task::cindexer:host') ?>&nbsp;:&nbsp;<input type="text" name="host" style="width:100px;" onchange="chgxmltxt(this, 'host');" value=""> <?php echo _('task::cindexer:host') ?>&nbsp;:&nbsp;<input type="text" name="host" style="width:100px;" onchange="chgxmltxt(this, 'host');" value="">
<br/> <br/>
<?php echo _('task::cindexer:port') ?>&nbsp;:&nbsp;<input type="text" name="port" style="width:100px;" onchange="chgxmltxt(this, 'port');" value=""> <?php echo _('task::cindexer:port') ?>&nbsp;:&nbsp;<input type="text" name="port" style="width:100px;" onchange="chgxmltxt(this, 'port');" value="">
<br/> <br/>
@@ -299,7 +299,7 @@ class task_period_cindexer extends task_abstract
<br/> <br/>
<br/> <br/>
<?php echo _('task::cindexer:control socket') ?>&nbsp;:&nbsp;<input type="text" name="socket" style="width:50px;" onchange="chgxmltxt(this, 'socket');" value=""> <?php echo _('task::cindexer:control socket') ?>&nbsp;:&nbsp;<input type="text" name="socket" style="width:50px;" onchange="chgxmltxt(this, 'socket');" value="">
<br/> <br/>
<?php echo _('task::cindexer:Debug mask') ?>&nbsp;:&nbsp;<input type="text" name="debugmask" style="width:50px;" onchange="chgxmltxt(this, 'debugmask');" value=""> <?php echo _('task::cindexer:Debug mask') ?>&nbsp;:&nbsp;<input type="text" name="debugmask" style="width:50px;" onchange="chgxmltxt(this, 'debugmask');" value="">
<br/> <br/>
@@ -310,20 +310,20 @@ class task_period_cindexer extends task_abstract
<br/> <br/>
</div> </div>
<?php echo _('task::cindexer:MySQL charset') ?>&nbsp;:&nbsp;<input type="text" name="charset" style="width:100px;" onchange="chgxmltxt(this, 'charset');" value=""> <?php echo _('task::cindexer:MySQL charset') ?>&nbsp;:&nbsp;<input type="text" name="charset" style="width:100px;" onchange="chgxmltxt(this, 'charset');" value="">
<br/> <br/>
<input type="checkbox" name="nolog" onclick="chgxmlck(this, 'nolog');">&nbsp;<?php echo _('task::cindexer:do not (sys)log, but out to console)') ?> <input type="checkbox" name="nolog" onclick="chgxmlck(this, 'nolog');">&nbsp;<?php echo _('task::cindexer:do not (sys)log, but out to console)') ?>
<br/> <br/>
<?php echo _('task::cindexer:default language for new candidates') ?>&nbsp;:&nbsp;<input type="text" name="clng" style="width:50px;" onchange="chgxmltxt(this, 'clng');" value=""> <?php echo _('task::cindexer:default language for new candidates') ?>&nbsp;:&nbsp;<input type="text" name="clng" style="width:50px;" onchange="chgxmltxt(this, 'clng');" value="">
<br/> <br/>
<br/> <br/>
<hr/> <hr/>
<br/> <br/>
<?php echo _('task::cindexer:windows specific') ?>&nbsp;:<br/> <?php echo _('task::cindexer:windows specific') ?>&nbsp;:<br/>
<input type="checkbox" name="winsvc_run" onclick="chgxmlck(this, 'run');">&nbsp;<?php echo _('task::cindexer:run as application, not as service') ?> <input type="checkbox" name="winsvc_run" onclick="chgxmlck(this, 'run');">&nbsp;<?php echo _('task::cindexer:run as application, not as service') ?>
<br/> <br/>
@@ -369,20 +369,12 @@ class task_period_cindexer extends task_abstract
{ {
$cmd = $this->binpath . 'phraseanet_indexer'; $cmd = $this->binpath . 'phraseanet_indexer';
switch ($this->system) { $nullfile = '/dev/null';
case "WINDOWS": $this->method = self::METHOD_PROC_OPEN;
$cmd .= '.exe';
$nullfile = 'NUL'; if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->method = self::METHOD_PROC_OPEN; $nullfile = '/dev/null';
break; $cmd .= '.exe';
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)) { if ( ! file_exists($cmd) || ! is_executable($cmd)) {
@@ -470,7 +462,7 @@ class task_period_cindexer extends task_abstract
private function run_with_proc_open($cmd, $args, $args_nopwd) private function run_with_proc_open($cmd, $args, $args_nopwd)
{ {
$nullfile = $this->system == 'WINDOWS' ? 'NUL' : '/dev/null'; $nullfile = defined('PHP_WINDOWS_VERSION_BUILD') ? 'NUL' : '/dev/null';
$descriptors = array(); $descriptors = array();
$descriptors[1] = array("file", $nullfile, "a+"); $descriptors[1] = array("file", $nullfile, "a+");

View File

@@ -41,9 +41,4 @@ class system_serverTest extends PhraseanetPHPUnitAbstract
$this->assertFalse($this->objects['lighttpd']->is_apache()); $this->assertFalse($this->objects['lighttpd']->is_apache());
} }
public function testGet_platform()
{
$platform = system_server::get_platform();
$this->assertRegExp('/[A-Z]+/', $platform);
}
} }

View File

@@ -2,10 +2,6 @@
require_once __DIR__ . '/../../PhraseanetPHPUnitAbstract.class.inc'; require_once __DIR__ . '/../../PhraseanetPHPUnitAbstract.class.inc';
/**
* Test class for task_period_archive.
* Generated by PHPUnit on 2012-05-22 at 15:54:27.
*/
class task_period_archiveTest extends \PhraseanetPHPUnitAbstract class task_period_archiveTest extends \PhraseanetPHPUnitAbstract
{ {
/** /**
@@ -19,7 +15,7 @@ class task_period_archiveTest extends \PhraseanetPHPUnitAbstract
$appbox = \appbox::get_instance(\bootstrap::getCore('test')); $appbox = \appbox::get_instance(\bootstrap::getCore('test'));
$task = \task_period_archive::create($appbox, 'task_period_archive'); $task = \task_period_archive::create($appbox, 'task_period_archive');
self::$object = new archiveTester($task->getID()); self::$object = new archiveTester($task->getID(), self::$core['monolog']);
} }
public static function tearDownAfterClass() public static function tearDownAfterClass()

View File

@@ -39,43 +39,19 @@ set_time_limit(0);
session_write_close(); session_write_close();
ignore_user_abort(true); ignore_user_abort(true);
$nullfile = '/dev/null';
$system = system_server::get_platform(); if (defined('PHP_WINDOWS_VERSION_BUILD')) {
if ($system != "DARWIN" && $system != "WINDOWS" && $system != "LINUX") { $nullfile = 'NUL';
phrasea::headers(500);
} }
$logdir = p4string::addEndSlash($registry->get('GV_RootPath') . 'logs');
$phpcli = $registry->get('GV_cli'); $phpcli = $registry->get('GV_cli');
$nullfile = ''; $cmd = $phpcli . ' -f ' . $registry->get('GV_RootPath') . "bin/console scheduler:start";
switch ($system) {
case "DARWIN":
$cmd = $phpcli . ' -f ' . $registry->get('GV_RootPath') . "bin/console scheduler:start";
$nullfile = '/dev/null';
break;
case "LINUX":
$cmd = $phpcli . ' -f ' . $registry->get('GV_RootPath') . "bin/console scheduler:start";
$nullfile = '/dev/null';
break;
case "WINDOWS":
case "WINDOWS NT":
$cmd = $phpcli . ' -f ' . $registry->get('GV_RootPath') . "bin/console scheduler:start";
$nullfile = 'NUL';
break;
}
//if ($logdir)
//{
// $descriptors[1] = array("file", $logdir . "scheduler.log", "a+");
// $descriptors[2] = array("file", $logdir . "scheduler.error.log", "a+");
//}
//else
//{
$descriptors[1] = array("file", $nullfile, "a+"); $descriptors[1] = array("file", $nullfile, "a+");
$descriptors[2] = array("file", $nullfile, "a+"); $descriptors[2] = array("file", $nullfile, "a+");
//}
$pipes = null; $pipes = null;
$cwd = $registry->get('GV_RootPath') . "bin/"; $cwd = $registry->get('GV_RootPath') . "bin/";

View File

@@ -14,13 +14,13 @@
* @license http://opensource.org/licenses/gpl-3.0 GPLv3 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com * @link www.phraseanet.com
*/ */
require_once dirname(dirname(__DIR__)) . "/lib/bootstrap.php"; $Core = require_once dirname(dirname(__DIR__)) . "/lib/bootstrap.php";
$request = http_request::getInstance(); $request = http_request::getInstance();
$parm = $request->get_parms('cls', 'taskid'); $parm = $request->get_parms('cls', 'taskid');
$cls = 'task_period_' . $parm['cls']; $cls = 'task_period_' . $parm['cls'];
$ztask = new $cls($parm['taskid']); $ztask = new $cls($parm['taskid'], $Core['monolog']);
echo $ztask->facility(); echo $ztask->facility();