changes asked on last pull requaest

This commit is contained in:
jygaulier
2012-05-22 18:50:45 +02:00
parent b6be659242
commit 1d58f89c36
15 changed files with 429 additions and 251 deletions

2
.gitignore vendored
View File

@@ -3,7 +3,5 @@
.DS_Store .DS_Store
!.gitignore !.gitignore
/vendor/ /vendor/
/datas
composer.lock composer.lock
composer.phar composer.phar
/libevent-0.0.5/.libs/

View File

@@ -11,10 +11,10 @@
/** /**
* *
* @package APIv1
* @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\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Silex\Application; use Silex\Application;

View File

@@ -61,14 +61,20 @@ class module_console_schedulerStart extends Command
return 1; return 1;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php';
try { try {
$scheduler = new task_Scheduler(); $scheduler = new task_Scheduler();
$scheduler->run($input, $output); $scheduler->run($input, $output);
} catch (\Exception $e) { } catch (\Exception $e) {
switch($e->getCode())
return $e->getCode(); {
case task_Scheduler::ERR_ALREADY_RUNNING: // 114 : aka EALREADY (Operation already in progress)
$exitCode = ERR_ALREADY_RUNNING;
break;
default:
$exitCode = 1; // default exit code (error)
break;
}
return $exitCode;
} }
} }
} }

View File

@@ -72,8 +72,6 @@ class module_console_taskState extends Command
return self::EXITCODE_BAD_ARGUMENT; return self::EXITCODE_BAD_ARGUMENT;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php';
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = appbox::get_instance(\bootstrap::getCore());
$task_manager = new task_manager($appbox); $task_manager = new task_manager($appbox);

View File

@@ -12,7 +12,6 @@
/** /**
* @todo write tests * @todo write tests
* *
* @package KonsoleKomander
* @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
*/ */
@@ -69,6 +68,7 @@ class module_console_taskrun extends Command
{ {
if ( ! setup::is_installed()) { if ( ! setup::is_installed()) {
$output->writeln('Phraseanet is not set up'); $output->writeln('Phraseanet is not set up');
return 1; return 1;
} }
@@ -106,6 +106,7 @@ class module_console_taskrun extends Command
$this->task->run($runner, $input, $output); $this->task->run($runner, $input, $output);
} 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()));
return($e->getCode()); return($e->getCode());
} }

View File

@@ -22,6 +22,7 @@ class task_Scheduler
// 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)
private $method; private $method;
@@ -52,7 +53,11 @@ class task_Scheduler
return appbox::get_instance(\bootstrap::getCore())->get_connection(); return appbox::get_instance(\bootstrap::getCore())->get_connection();
} }
public function run($input = null, OutputInterface $output = null) /*
* @throws Exception if scheduler is already running
* @todo doc all possible exception
*/
public function run(InputInterface $input = null, OutputInterface $output = null)
{ {
require_once dirname(__FILE__) . '/../../bootstrap.php'; require_once dirname(__FILE__) . '/../../bootstrap.php';
$this->input = $input; $this->input = $input;
@@ -87,6 +92,7 @@ class task_Scheduler
fclose($schedlock); fclose($schedlock);
throw new Exception('scheduler already running.', self::ERR_ALREADY_RUNNING); throw new Exception('scheduler already running.', self::ERR_ALREADY_RUNNING);
return; return;
} else { } else {
sleep(2); sleep(2);

View File

@@ -113,21 +113,35 @@ abstract class task_abstract
return $row['status']; return $row['status'];
} }
/*
* to be overwritten by tasks : echo text to be included in <head> in task interface
*/
public function printInterfaceHEAD() public function printInterfaceHEAD()
{ {
return false; return false;
} }
/*
* to be overwritten by tasks : echo javascript to be included in <head> in task interface
*/
public function printInterfaceJS() public function printInterfaceJS()
{ {
return false; return false;
} }
/**
*
* @return boolean
*/
public function printInterfaceHTML() public function printInterfaceHTML()
{ {
return false; return false;
} }
/**
*
* @return boolean
*/
public function getGraphicForm() public function getGraphicForm()
{ {
return false; return false;
@@ -289,8 +303,7 @@ abstract class task_abstract
$this->log($e->getMessage()); $this->log($e->getMessage());
$this->log(("Warning : abox connection lost, restarting in 10 min.")); $this->log(("Warning : abox connection lost, restarting in 10 min."));
for ($t = 60 * 10; $this->running && $t > 0; $t -- ) // DON'T do sleep(600) because it prevents ticks ! $this->sleep(60 * 10);
sleep(1);
$this->running = false; $this->running = false;
@@ -429,6 +442,16 @@ abstract class task_abstract
} }
} }
protected function sleep($nsec)
{
$nsec = (int) $nsec;
if ($nsec < 0)
throw new \InvalidArgumentException(sprintf("(%s) is not > 0"));
while ($this->running && $nsec -- > 0) {
sleep(1);
}
}
private function getLockfilePath() private function getLockfilePath()
{ {
$registry = registry::get_instance(); $registry = registry::get_instance();
@@ -515,6 +538,94 @@ abstract class task_abstract
abstract protected function run2(); abstract protected function run2();
protected function processLoop(&$box, &$rs)
{
$ret = self::STATE_OK;
$rowstodo = count($rs);
$rowsdone = 0;
if ($rowstodo > 0) {
$this->setProgress(0, $rowstodo);
}
foreach ($rs as $row) {
try {
// process one record
$this->processOneContent($box, $row);
} catch (Exception $e) {
$this->log("Exception : " . $e->getMessage() . " " . basename($e->getFile()) . " " . $e->getLine());
}
$this->records_done ++;
$this->setProgress($rowsdone, $rowstodo);
// post-process
$this->postProcessOneContent($box, $row);
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (actual is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
if ( ! $this->running) {
break;
}
}
//
// if nothing was done, at least check the status
if (count($rs) == 0 && $this->running) {
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (current is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
}
if ($rowstodo > 0) {
$this->setProgress(0, 0);
}
return $ret;
}
protected function loadSettings(SimpleXMLElement $sx_task_settings) protected function loadSettings(SimpleXMLElement $sx_task_settings)
{ {
$this->period = (int) $sx_task_settings->period; $this->period = (int) $sx_task_settings->period;

View File

@@ -118,98 +118,110 @@ abstract class task_appboxAbstract extends task_abstract
*/ */
protected function process(appbox $appbox) protected function process(appbox $appbox)
{ {
$ret = self::STATE_OK; $ret = self::STATE_OK;
try { try {
// get the records to process // get the records to process
$rs = $this->retrieveContent($appbox); $rs = $this->retrieveContent($appbox);
$ret = $this->processLoop($appbox, $rs);
} catch (Exception $e) { } catch (Exception $e) {
$this->log('Error : ' . $e->getMessage()); $this->log('Error : ' . $e->getMessage());
$rs = array();
} }
$rowstodo = count($rs);
$rowsdone = 0;
if ($rowstodo > 0) {
$this->setProgress(0, $rowstodo);
}
foreach ($rs as $row) {
try {
// process one record
$this->processOneContent($appbox, $row);
} catch (Exception $e) {
$this->log("Exception : " . $e->getMessage() . " " . basename($e->getFile()) . " " . $e->getLine());
}
$this->records_done ++;
$this->setProgress($rowsdone, $rowstodo);
// post-process
$this->postProcessOneContent($appbox, $row);
// $this->check_memory_usage();
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (actual is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
// $this->check_task_status();
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
if ( ! $this->running) {
break;
}
}
//
// if nothing was done, at least check the status
if (count($rs) == 0 && $this->running) {
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (current is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
}
if ($rowstodo > 0) {
$this->setProgress(0, 0);
}
return $ret; return $ret;
/*
$ret = self::STATE_OK;
try {
// get the records to process
$rs = $this->retrieveContent($appbox);
} catch (Exception $e) {
$this->log('Error : ' . $e->getMessage());
$rs = array();
}
$rowstodo = count($rs);
$rowsdone = 0;
if ($rowstodo > 0) {
$this->setProgress(0, $rowstodo);
}
foreach ($rs as $row) {
try {
// process one record
$this->processOneContent($appbox, $row);
} catch (Exception $e) {
$this->log("Exception : " . $e->getMessage() . " " . basename($e->getFile()) . " " . $e->getLine());
}
$this->records_done ++;
$this->setProgress($rowsdone, $rowstodo);
// post-process
$this->postProcessOneContent($appbox, $row);
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (actual is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
if ( ! $this->running) {
break;
}
}
//
// if nothing was done, at least check the status
if (count($rs) == 0 && $this->running) {
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (current is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
}
if ($rowstodo > 0) {
$this->setProgress(0, 0);
}
return $ret;
*/
} }
} }

View File

@@ -86,6 +86,14 @@ abstract class task_databoxAbstract extends task_abstract
$this->sbas_id = (int) $row['sbas_id']; $this->sbas_id = (int) $row['sbas_id'];
$this->log('This task works now on ' . phrasea::sbas_names($this->sbas_id)); $this->log('This task works now on ' . phrasea::sbas_names($this->sbas_id));
try {
// get the records to process
$databox = databox::get_instance((int)$row['sbas_id']);
} catch (Exception $e) {
$this->log(sprintf('Warning : can\' connect to sbas(%s)', $row['sbas_id']));
continue;
}
try { try {
$this->loadSettings(simplexml_load_string($row['settings'])); $this->loadSettings(simplexml_load_string($row['settings']));
} catch (Exception $e) { } catch (Exception $e) {
@@ -93,7 +101,15 @@ abstract class task_databoxAbstract extends task_abstract
continue; continue;
} }
$process_ret = $this->processSbas(); $process_ret = $this->processSbas($databox);
// close the cnx to the dbox
$connbas = $databox->get_connection();
if ($connbas instanceof PDO) {
$connbas->close();
unset($connbas);
}
switch ($process_ret) { switch ($process_ret) {
case self::STATE_MAXMEGSREACHED: case self::STATE_MAXMEGSREACHED:
@@ -137,109 +153,113 @@ abstract class task_databoxAbstract extends task_abstract
* *
* @return <type> * @return <type>
*/ */
protected function processSbas() protected function processSbas(databox $databox)
{ {
$ret = self::STATE_OK; $ret = self::STATE_OK;
$connbas = false;
try { try {
// get the records to process // get the records to process
$databox = databox::get_instance($this->sbas_id);
$connbas = $databox->get_connection();
$rs = $this->retrieveSbasContent($databox); $rs = $this->retrieveSbasContent($databox);
$ret = $this->processLoop($databox, $rs);
} catch (Exception $e) { } catch (Exception $e) {
$this->log('Error : ' . $e->getMessage()); $this->log('Error : ' . $e->getMessage());
$rs = array();
}
$rowstodo = count($rs);
$rowsdone = 0;
if ($rowstodo > 0) {
$this->setProgress(0, $rowstodo);
}
foreach ($rs as $row) {
try {
// process one record
$this->processOneContent($databox, $row);
} catch (Exception $e) {
$this->log("Exception : " . $e->getMessage() . " " . basename($e->getFile()) . " " . $e->getLine());
}
$this->records_done ++;
$this->setProgress($rowsdone, $rowstodo);
// post-process
$this->postProcessOneContent($databox, $row);
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (actual is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
if ( ! $this->running) {
break;
}
}
//
// if nothing was done, at least check the status
if (count($rs) == 0 && $this->running) {
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (current is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
}
// close the cnx to the dbox
if ($connbas instanceof PDO) {
$connbas->close();
unset($connbas);
}
if ($rowstodo > 0) {
$this->setProgress(0, 0);
} }
return $ret; return $ret;
/*
$ret = self::STATE_OK;
try {
// get the records to process
$rs = $this->retrieveSbasContent($databox);
} catch (Exception $e) {
$this->log('Error : ' . $e->getMessage());
$rs = array();
}
$rowstodo = count($rs);
$rowsdone = 0;
if ($rowstodo > 0) {
$this->setProgress(0, $rowstodo);
}
foreach ($rs as $row) {
try {
// process one record
$this->processOneContent($databox, $row);
} catch (Exception $e) {
$this->log("Exception : " . $e->getMessage() . " " . basename($e->getFile()) . " " . $e->getLine());
}
$this->records_done ++;
$this->setProgress($rowsdone, $rowstodo);
// post-process
$this->postProcessOneContent($databox, $row);
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (actual is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
if ( ! $this->running) {
break;
}
}
//
// if nothing was done, at least check the status
if (count($rs) == 0 && $this->running) {
$current_memory = memory_get_usage();
if ($current_memory >> 20 >= $this->maxmegs) {
$this->log(sprintf("Max memory (%s M) reached (current is %s M)", $this->maxmegs, $current_memory));
$this->running = FALSE;
$ret = self::STATE_MAXMEGSREACHED;
}
if ($this->records_done >= (int) ($this->maxrecs)) {
$this->log(sprintf("Max records done (%s) reached (actual is %s)", $this->maxrecs, $this->records_done));
$this->running = FALSE;
$ret = self::STATE_MAXRECSDONE;
}
try {
$status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE;
$ret = self::STATE_TOSTOP;
}
} catch (Exception $e) {
$this->running = FALSE;
}
}
if ($rowstodo > 0) {
$this->setProgress(0, 0);
}
return $ret;
*/
} }
} }

View File

@@ -382,6 +382,7 @@ class task_period_archive extends task_abstract
// runner = manual : can't restart so simply quit // runner = manual : can't restart so simply quit
} }
$this->running = FALSE; $this->running = FALSE;
return; return;
} }
@@ -399,19 +400,16 @@ class task_period_archive extends task_abstract
$this->setState(self::STATE_STOPPED); $this->setState(self::STATE_STOPPED);
} }
$this->running = FALSE; $this->running = FALSE;
return; return;
} }
$this->setLastExecTime(); $this->setLastExecTime();
$row = NULL;
try { try {
$sql = "SELECT * FROM task2 WHERE task_id = :task_id"; if (!($this->sxTaskSettings = @simplexml_load_string($this->getSettings()))) {
$stmt = $conn->prepare($sql); throw new Exception(sprintf('Error fetching or reading settings of the task \'%d\'', $this->getID()));
$stmt->execute(array(':task_id' => $this->getID())); } else {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
if ($row && $this->sxTaskSettings = @simplexml_load_string($row['settings'])) {
// copy settings to task, so it's easier to get later // copy settings to task, so it's easier to get later
$this->move_archived = p4field::isyes($this->sxTaskSettings->move_archived); $this->move_archived = p4field::isyes($this->sxTaskSettings->move_archived);
$this->move_error = p4field::isyes($this->sxTaskSettings->move_error); $this->move_error = p4field::isyes($this->sxTaskSettings->move_error);
@@ -424,15 +422,13 @@ class task_period_archive extends task_abstract
if ($cold <= 0 || $cold >= 60 * 60) { if ($cold <= 0 || $cold >= 60 * 60) {
$cold = 60; $cold = 60;
} }
} else {
throw new Exception(sprintf('Error fetching or reading settings of the task \'%d\'', $this->getID()));
} }
} catch (Exception $e) { } catch (Exception $e) {
if ($this->getRunner() == self::RUNNER_SCHEDULER) { if ($this->getRunner() == self::RUNNER_SCHEDULER) {
$this->log(sprintf(('Warning : error fetching or reading settings of the task \'%d\', restarting in 10 min.'), $this->getID())); $this->log(sprintf(('Warning : error fetching or reading settings of the task \'%d\', restarting in 10 min.'), $this->getID()));
for ($t = 60 * 10; $this->running && $t; $t -- ) // DON'T do sleep(600) because it prevents ticks ! $this->sleep(60 * 10);
sleep(1);
$this->setState(self::STATE_TORESTART); $this->setState(self::STATE_TORESTART);
} else { } else {
$this->log(sprintf(('Error : error fetching task \'%d\', stopping.'), $this->getID())); $this->log(sprintf(('Error : error fetching task \'%d\', stopping.'), $this->getID()));
@@ -440,11 +436,15 @@ class task_period_archive extends task_abstract
$this->setState(self::STATE_STOPPED); $this->setState(self::STATE_STOPPED);
} }
$this->running = FALSE; $this->running = FALSE;
return; return;
} }
if ($row['status'] == self::STATE_TOSTOP) { $status = $this->getState();
if ($status == self::STATE_TOSTOP) {
$this->running = FALSE; $this->running = FALSE;
return; return;
} }
@@ -485,13 +485,13 @@ class task_period_archive extends task_abstract
case 'MAXRECSDONE': case 'MAXRECSDONE':
case 'MAXMEMORY': case 'MAXMEMORY':
case 'MAXLOOP': case 'MAXLOOP':
if ($row['status'] == self::STATE_STARTED && $this->getRunner() !== self::RUNNER_MANUAL) { if ($status == self::STATE_STARTED && $this->getRunner() !== self::RUNNER_MANUAL) {
$this->setState(self::STATE_TORESTART); $this->setState(self::STATE_TORESTART);
$this->running = FALSE; $this->running = FALSE;
} }
break; break;
default: default:
if ($row['status'] == self::STATE_STARTED) { if ($status == self::STATE_STARTED) {
$this->setState(self::STATE_STOPPED); $this->setState(self::STATE_STOPPED);
$this->running = FALSE; $this->running = FALSE;
} }

View File

@@ -391,38 +391,50 @@ class task_period_cindexer extends task_abstract
} }
$args = array(); $args = array();
$args_nopwd = array();
if ($this->host) { if ($this->host) {
$args[] = '-h=' . $this->host; $args[] = '-h=' . $this->host;
$args_nopwd[] = '-h=' . $this->host;
} }
if ($this->port) { if ($this->port) {
$args[] = '-P=' . $this->port; $args[] = '-P=' . $this->port;
$args_nopwd[] = '-P=' . $this->port;
} }
if ($this->base) { if ($this->base) {
$args[] = '-b=' . $this->base; $args[] = '-b=' . $this->base;
$args_nopwd[] = '-b=' . $this->base;
} }
if ($this->user) { if ($this->user) {
$args[] = '-u=' . $this->user; $args[] = '-u=' . $this->user;
$args_nopwd[] = '-u=' . $this->user;
} }
if ($this->password) { if ($this->password) {
$args[] = '-p=' . $this->password; $args[] = '-p=' . $this->password;
$args_nopwd[] = '-p=******';
} }
if ($this->socket) { if ($this->socket) {
$args[] = '--socket=' . $this->socket; $args[] = '--socket=' . $this->socket;
$args_nopwd[] = '--socket=' . $this->socket;
} }
if ($this->use_sbas) { if ($this->use_sbas) {
$args[] = '-o'; $args[] = '-o';
$args_nopwd[] = '-o';
} }
if ($this->charset) { if ($this->charset) {
$args[] = '--default-character-set=' . $this->charset; $args[] = '--default-character-set=' . $this->charset;
$args_nopwd[] = '--default-character-set=' . $this->charset;
} }
if ($this->debugmask > 0) { if ($this->debugmask > 0) {
$args[] = '-d=' . $this->debugmask; $args[] = '-d=' . $this->debugmask;
$args_nopwd[] = '-d=' . $this->debugmask;
} }
if ($this->nolog) { if ($this->nolog) {
$args[] = '-n'; $args[] = '-n';
$args_nopwd[] = '-n';
} }
if ($this->winsvc_run) { if ($this->winsvc_run) {
$args[] = '--run'; $args[] = '--run';
$args_nopwd[] = '--run';
} }
$registry = registry::get_instance(); $registry = registry::get_instance();
@@ -434,13 +446,13 @@ class task_period_cindexer extends task_abstract
$this->log(sprintf("running cindexer with method %s", $this->method)); $this->log(sprintf("running cindexer with method %s", $this->method));
switch ($this->method) { switch ($this->method) {
case self::METHOD_PROC_OPEN: case self::METHOD_PROC_OPEN:
$this->run_with_proc_open($cmd, $args); $this->run_with_proc_open($cmd, $args, $args_nopwd);
break; break;
case self::METHOD_FORK: case self::METHOD_FORK:
$this->run_with_fork($cmd, $args); $this->run_with_fork($cmd, $args, $args_nopwd);
break; break;
case self::METHOD_EXEC: case self::METHOD_EXEC:
$this->run_with_exec($cmd, $args); $this->run_with_exec($cmd, $args, $args_nopwd);
break; break;
} }
@@ -453,7 +465,7 @@ class task_period_cindexer extends task_abstract
} }
} }
private function run_with_proc_open($cmd, $args) private function run_with_proc_open($cmd, $args, $args_nopwd)
{ {
$nullfile = $this->system == 'WINDOWS' ? 'NUL' : '/dev/null'; $nullfile = $this->system == 'WINDOWS' ? 'NUL' : '/dev/null';
@@ -463,7 +475,8 @@ class task_period_cindexer extends task_abstract
$pipes = array(); $pipes = array();
$this->log(sprintf('cmd=\'%s %s\'', $cmd, implode(' ', $args))); $this->log(sprintf('cmd=\'%s %s\'', $cmd, implode(' ', $args_nopwd)));
$process = proc_open($cmd . ' ' . implode(' ', $args), $descriptors, $pipes, $this->binpath, null, array('bypass_shell' => true)); $process = proc_open($cmd . ' ' . implode(' ', $args), $descriptors, $pipes, $this->binpath, null, array('bypass_shell' => true));
$pid = NULL; $pid = NULL;
@@ -544,7 +557,7 @@ class task_period_cindexer extends task_abstract
proc_close($process); proc_close($process);
} }
private function run_with_fork($cmd, $args) private function run_with_fork($cmd, $args, $args_nopwd)
{ {
$pid = pcntl_fork(); $pid = pcntl_fork();
if ($pid == -1) { if ($pid == -1) {
@@ -610,7 +623,7 @@ class task_period_cindexer extends task_abstract
} }
} }
private function run_with_exec($cmd, $args) private function run_with_exec($cmd, $args, $args_nopwd)
{ {
pcntl_exec($cmd, $args); pcntl_exec($cmd, $args);
sleep(2); sleep(2);

View File

@@ -54,8 +54,9 @@ class task_period_ftpPull extends task_appboxAbstract
$pvalue = $parm2[$pname]; $pvalue = $parm2[$pname];
if ($ns = $dom->getElementsByTagName($pname)->item(0)) { if ($ns = $dom->getElementsByTagName($pname)->item(0)) {
// le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu) // le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu)
while (($n = $ns->firstChild)) while (($n = $ns->firstChild)) {
$ns->removeChild($n); $ns->removeChild($n);
}
} else { } else {
// le champ n'existait pas dans le xml, on le cree // le champ n'existait pas dans le xml, on le cree
$dom->documentElement->appendChild($dom->createTextNode("\t")); $dom->documentElement->appendChild($dom->createTextNode("\t"));
@@ -200,8 +201,9 @@ class task_period_ftpPull extends task_appboxAbstract
if ($parm[$f] !== NULL) { if ($parm[$f] !== NULL) {
if (($ns = $domTaskSettings->getElementsByTagName($f)->item(0)) != NULL) { if (($ns = $domTaskSettings->getElementsByTagName($f)->item(0)) != NULL) {
// le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu) // le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu)
while (($n = $ns->firstChild)) while (($n = $ns->firstChild)) {
$ns->removeChild($n); $ns->removeChild($n);
}
} else { } else {
// le champ n'existait pas dans le xml, on le cree // le champ n'existait pas dans le xml, on le cree
$domTaskSettings->documentElement->appendChild($domTaskSettings->createTextNode("\t")); $domTaskSettings->documentElement->appendChild($domTaskSettings->createTextNode("\t"));
@@ -213,13 +215,15 @@ class task_period_ftpPull extends task_appboxAbstract
$xmlchanged = true; $xmlchanged = true;
} }
} }
if ($xmlchanged) if ($xmlchanged) {
$parm["xml"] = $domTaskSettings->saveXML(); $parm["xml"] = $domTaskSettings->saveXML();
}
} }
} }
// si on doit changer le xml, on verifie qu'il est valide // si on doit changer le xml, on verifie qu'il est valide
if ($parm["xml"] && ! DOMDocument::loadXML($parm["xml"])) { if ($parm["xml"] && ! DOMDocument::loadXML($parm["xml"])) {
return(false); return(false);
} }
@@ -247,9 +251,11 @@ class task_period_ftpPull extends task_appboxAbstract
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
return false; return false;
} }
} else { } else {
return true; return true;
} }
} }
@@ -286,8 +292,10 @@ class task_period_ftpPull extends task_appboxAbstract
$this->log('\'' . $this->localpath . '\' is not writeable'); $this->log('\'' . $this->localpath . '\' is not writeable');
$this->running = FALSE; $this->running = FALSE;
} }
if ( ! $this->running) { if ( ! $this->running) {
$this->set_status(self::STATE_STOPPED); $this->set_status(self::STATE_STOPPED);
return array(); return array();
} }
@@ -297,6 +305,7 @@ class task_period_ftpPull extends task_appboxAbstract
$ftp->chdir($this->ftppath); $ftp->chdir($this->ftppath);
$list_1 = $ftp->list_directory(true); $list_1 = $ftp->list_directory(true);
$done = 0;
$todo = count($list_1); $todo = count($list_1);
$this->setProgress($done, $todo); $this->setProgress($done, $todo);
@@ -304,18 +313,13 @@ class task_period_ftpPull extends task_appboxAbstract
echo "attente de 25sec pour avoir les fichiers froids...\n"; echo "attente de 25sec pour avoir les fichiers froids...\n";
} }
for ($t = 25; $this->running && $t > 0; $t -- ) { // DON'T do sleep($this->period - $when_started) because it prevents ticks ! $this->sleep(25);
$s = $this->getState(); if ( ! $this->running) {
if ($s == self::STATE_TOSTOP) { if (isset($ftp) && $ftp instanceof ftpclient) {
if (isset($ftp) && $ftp instanceof ftpclient) $ftp->close();
$ftp->close();
$this->set_status(self::STATE_STOPPED);
$this->running = FALSE;
return array();
}
else {
sleep(1);
} }
return array();
} }
$list_2 = $ftp->list_directory(true); $list_2 = $ftp->list_directory(true);
@@ -325,13 +329,15 @@ class task_period_ftpPull extends task_appboxAbstract
$this->setProgress($done, $todo); $this->setProgress($done, $todo);
if ( ! isset($list_2[$filepath])) { if ( ! isset($list_2[$filepath])) {
if ($this->debug) if ($this->debug) {
echo "le fichier $filepath a disparu...\n"; echo "le fichier $filepath a disparu...\n";
}
continue; continue;
} }
if ($list_2[$filepath] !== $timestamp) { if ($list_2[$filepath] !== $timestamp) {
if ($this->debug) if ($this->debug) {
echo "le fichier $filepath a ete modifie depuis le dernier passage...\n"; echo "le fichier $filepath a ete modifie depuis le dernier passage...\n";
}
continue; continue;
} }
@@ -339,16 +345,18 @@ class task_period_ftpPull extends task_appboxAbstract
echo "Ok pour rappatriement de $filepath vers $finalpath\n"; echo "Ok pour rappatriement de $filepath vers $finalpath\n";
try { try {
if (file_exists($finalpath)) if (file_exists($finalpath)) {
throw new Exception("Un fichier du meme nom ($finalpath) existe deja..."); throw new Exception("Un fichier du meme nom ($finalpath) existe deja...");
}
system_file::mkdir(dirname($finalpath)); system_file::mkdir(dirname($finalpath));
$ftp->get($finalpath, $filepath); $ftp->get($finalpath, $filepath);
$ftp->delete($filepath); $ftp->delete($filepath);
} catch (Exception $e) { } catch (Exception $e) {
if ($this->debug) if ($this->debug) {
echo "Erreur lors du rappatriement de $filepath : " . $e->getMessage() . "\n"; echo "Erreur lors du rappatriement de $filepath : " . $e->getMessage() . "\n";
}
} }
} }
@@ -356,9 +364,11 @@ class task_period_ftpPull extends task_appboxAbstract
$this->setProgress(0, 0); $this->setProgress(0, 0);
} catch (Exception $e) { } catch (Exception $e) {
if (isset($ftp) && $ftp instanceof ftpclient) if (isset($ftp) && $ftp instanceof ftpclient) {
$ftp->close(); $ftp->close();
}
echo $e->getMessage() . "\n"; echo $e->getMessage() . "\n";
return array(); return array();
} }
} }

View File

@@ -29,7 +29,7 @@ class task_period_test extends task_appboxAbstract
protected function retrieveContent(appbox $appbox) protected function retrieveContent(appbox $appbox)
{ {
$this->log('test class, retrive content'); $this->log('test class, retrieve content');
return array(array('hello'), array('world')); return array(array('hello'), array('world'));
} }

View File

@@ -3,11 +3,14 @@
require_once __DIR__ . '/../../PhraseanetWebTestCaseAuthenticatedAbstract.class.inc'; require_once __DIR__ . '/../../PhraseanetWebTestCaseAuthenticatedAbstract.class.inc';
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use \Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
class module_console_schedulerStateTest extends PHPUnit_Framework_TestCase class module_console_schedulerStateTest extends PHPUnit_Framework_TestCase
{ {
/**
* @covers module_console_schedulerState::execute
*/
public function testExecute() public function testExecute()
{ {
// mock the Kernel or create one depending on your needs // mock the Kernel or create one depending on your needs

View File

@@ -32,10 +32,10 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
public function testActive() public function testActive()
{ {
self::$task->setActive(true); self::$task->setActive(true);
self::assertTrue(self::$task->isActive()); $this->assertTrue(self::$task->isActive());
self::$task->setActive(false); self::$task->setActive(false);
self::assertFalse(self::$task->isActive()); $this->assertFalse(self::$task->isActive());
} }
/** /**
@@ -45,10 +45,10 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
public function testState() public function testState()
{ {
self::$task->setState(\task_abstract::STATE_STOPPED); self::$task->setState(\task_abstract::STATE_STOPPED);
self::assertEquals(\task_abstract::STATE_STOPPED, self::$task->getState()); $this->assertEquals(\task_abstract::STATE_STOPPED, self::$task->getState());
self::$task->setState(\task_abstract::STATE_TOSTOP); self::$task->setState(\task_abstract::STATE_TOSTOP);
self::assertEquals(\task_abstract::STATE_TOSTOP, self::$task->getState()); $this->assertEquals(\task_abstract::STATE_TOSTOP, self::$task->getState());
} }
/** /**
@@ -58,7 +58,7 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
public function testTitle() public function testTitle()
{ {
self::$task->setTitle('a_test_title'); self::$task->setTitle('a_test_title');
self::assertEquals('a_test_title', self::$task->getTitle()); $this->assertEquals('a_test_title', self::$task->getTitle());
} }
/** /**
@@ -70,13 +70,13 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
{ {
self::$task->resetCrashCounter(); self::$task->resetCrashCounter();
self::$task->incrementCrashCounter(); self::$task->incrementCrashCounter();
self::assertEquals(1, self::$task->getCrashCounter()); $this->assertEquals(1, self::$task->getCrashCounter());
self::$task->incrementCrashCounter(); self::$task->incrementCrashCounter();
self::assertEquals(2, self::$task->getCrashCounter()); $this->assertEquals(2, self::$task->getCrashCounter());
self::$task->resetCrashCounter(); self::$task->resetCrashCounter();
self::assertEquals(0, self::$task->getCrashCounter()); $this->assertEquals(0, self::$task->getCrashCounter());
} }
/** /**
@@ -92,8 +92,8 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
$settings = self::$task->getSettings(); $settings = self::$task->getSettings();
$sxSettings = @simplexml_load_string($settings); $sxSettings = @simplexml_load_string($settings);
self::assertTrue($sxSettings !== FALSE); $this->assertTrue($sxSettings !== FALSE);
self::assertEquals($sxGoodSettings->saveXML(), $sxSettings->saveXML()); $this->assertEquals($sxGoodSettings->saveXML(), $sxSettings->saveXML());
} }
/** /**
@@ -112,10 +112,10 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
public function testRunner() public function testRunner()
{ {
self::$task->setRunner(\task_abstract::RUNNER_MANUAL); self::$task->setRunner(\task_abstract::RUNNER_MANUAL);
self::assertTrue(\task_abstract::RUNNER_MANUAL === self::$task->getRunner()); $this->assertTrue(\task_abstract::RUNNER_MANUAL === self::$task->getRunner());
self::$task->setRunner(\task_abstract::RUNNER_SCHEDULER); self::$task->setRunner(\task_abstract::RUNNER_SCHEDULER);
self::assertTrue(\task_abstract::RUNNER_SCHEDULER === self::$task->getRunner()); $this->assertTrue(\task_abstract::RUNNER_SCHEDULER === self::$task->getRunner());
} }
/** /**
@@ -143,14 +143,14 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
try { try {
$fd = $methodL->invoke(self::$task); $fd = $methodL->invoke(self::$task);
} catch (Exception $e) { } catch (Exception $e) {
self::fail('file should not be locked'); $this->fail('file should not be locked');
} }
self::assertInternalType('resource', $fd); $this->assertInternalType('resource', $fd);
// now task should be locked // now task should be locked
try { try {
$fd = $methodL->invoke(self::$task); $fd = $methodL->invoke(self::$task);
self::fail('file should be locked'); $this->fail('file should be locked');
} catch (Exception $e) { } catch (Exception $e) {
} }
@@ -162,9 +162,9 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
try { try {
$fd = $methodL->invoke(self::$task); $fd = $methodL->invoke(self::$task);
} catch (Exception $e) { } catch (Exception $e) {
self::fail('file should not be locked'); $this->fail('file should not be locked');
} }
self::assertInternalType('resource', $fd); $this->assertInternalType('resource', $fd);
// leave the file unlocked // leave the file unlocked
$methodU->invokeArgs(self::$task, array($fd)); $methodU->invokeArgs(self::$task, array($fd));