Fix #1380 Add default settings for periodic tasks

This commit is contained in:
Nicolas Le Goff
2013-08-02 15:09:12 +02:00
parent 94596926ac
commit d413394027
17 changed files with 252 additions and 82 deletions

View File

@@ -496,7 +496,7 @@ class Collection implements ControllerProviderInterface
$msg = _('Collection empty successful');
} else {
$settings = '<?xml version="1.0" encoding="UTF-8"?><tasksettings><bas_id>' . $collection->get_base_id() . '</bas_id></tasksettings>';
\task_abstract::create($app, 'task_period_emptyColl', $settings);
\task_period_emptyColl::create($app, $settings);
$msg = _('A task has been creted, please run it to complete empty collection');
}

View File

@@ -882,7 +882,7 @@ class Databox implements ControllerProviderInterface
$msg = _('Base empty successful');
} else {
$settings = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tasksettings><base_id>" . $collection->get_base_id() . "</base_id></tasksettings>";
\task_abstract::create($app, 'task_period_emptyColl', $settings);
\task_period_emptyColl::create($app, $settings);
$msg = _('A task has been creted, please run it to complete empty collection');
}
}

View File

@@ -49,7 +49,11 @@ class TaskManager implements ControllerProviderInterface
})->bind('admin_tasks_list');
$controllers->post('/tasks/create/', function(Application $app, Request $request) {
$task = \task_abstract::create($app, $request->request->get('tcl'));
if (!class_exists($className = $request->request->get('tcl'))) {
$app->abort(400, sprintf('Unknown task %s', $className));
}
$task = $className::create($app);
$tid = $task->getId();
return $app->redirectPath('admin_tasks_task_show', array('id' => $tid));

View File

@@ -94,30 +94,13 @@ class Installer
);
foreach (array('cindexer', 'subdef', 'writemeta') as $task) {
$class_name = sprintf('task_period_%s', $task);
if ($task === 'cindexer' && is_executable($this->phraseaIndexer)) {
$credentials = $databox->get_appbox()->get_connection()->get_credentials();
$className = sprintf('task_period_%s', $task);
$host = $credentials['hostname'];
$port = $credentials['port'];
$user_ab = $credentials['user'];
$password = $credentials['password'];
$dbname = $credentials['dbname'];
$settings = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tasksettings>\n<binpath>"
. str_replace('/phraseanet_indexer', '', $this->phraseaIndexer)
. "</binpath><host>" . $host . "</host><port>"
. $port . "</port><base>"
. $dbname . "</base><user>"
. $user_ab . "</user><password>"
. $password . "</password><socket>25200</socket>"
. "<use_sbas>1</use_sbas><nolog>0</nolog><clng></clng>"
. "<winsvc_run>0</winsvc_run><charset>utf8</charset></tasksettings>";
} else {
$settings = null;
if (!class_exists($className)) {
throw new \InvalidArgumentException('Unknown task class "' . $className.'"');
}
\task_abstract::create($this->app, $class_name, $settings);
$className::create($this->app);
}
}

View File

@@ -1,6 +1,7 @@
<?php
use Monolog\Logger;
use Alchemy\Phrasea\Core\Configuration\Configuration;
abstract class task_abstract
{
@@ -833,12 +834,8 @@ abstract class task_abstract
* @param string $settings
* @return task_abstract
*/
public static function create(\Pimple $dependencyContainer, $class_name, $settings = null)
public static function create(\Pimple $dependencyContainer, $settings = null)
{
if ( ! class_exists($class_name)) {
throw new Exception('Unknown task class');
}
$sql = 'INSERT INTO task2
(task_id, usr_id_owner, status, crashed, active,
name, last_exec_time, class, settings)
@@ -848,24 +845,25 @@ abstract class task_abstract
$domdoc = new DOMDocument();
if ($settings && ! $domdoc->loadXML($settings)) {
throw new Exception('settings invalide');
throw new Exception('Invalid settings');
} elseif (! $settings) {
$settings = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tasksettings>\n</tasksettings>";
$settings = static::getDefaultSettings($dependencyContainer['phraseanet.configuration']);
}
$params = array(
':active' => 1
, ':name' => ''
, ':class' => $class_name
, ':class' => get_called_class()
, ':settings' => $settings
);
$stmt = $dependencyContainer['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
$tid = $dependencyContainer['phraseanet.appbox']->get_connection()->lastInsertId();
$task = new $class_name($tid, $dependencyContainer, $dependencyContainer['task-manager.logger']);
$task = new static($tid, $dependencyContainer, $dependencyContainer['task-manager.logger']);
$task->setTitle($task->getName());
return $task;
@@ -917,4 +915,13 @@ abstract class task_abstract
return $this;
}
/**
*
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tasksettings>\n</tasksettings>";
}
}

View File

@@ -8,14 +8,10 @@
* file that was distributed with this source code.
*/
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Alchemy\Phrasea\Core\Configuration\Configuration;
class task_period_RecordMover extends task_appboxAbstract
{
/**
*
* @return string
@@ -788,25 +784,97 @@ class task_period_RecordMover extends task_appboxAbstract
switch ($parm2['ACT']) {
case 'CALCTEST':
$sxml = simplexml_load_string($parm2['xml']);
if (isset($sxml->tasks->task)) {
foreach ($sxml->tasks->task as $sxtask) {
$ret['tasks'][] = $this->calcSQL($sxtask, false);
}
}
break;
case 'PLAYTEST':
$sxml = simplexml_load_string($parm2['xml']);
if (isset($sxml->tasks->task)) {
foreach ($sxml->tasks->task as $sxtask) {
$ret['tasks'][] = $this->calcSQL($sxtask, true);
}
}
break;
case 'CALCSQL':
$xml = $this->graphic2xml('<?xml version="1.0" encoding="UTF-8"?><tasksettings/>');
$sxml = simplexml_load_string($xml);
if (isset($sxml->tasks->task)) {
foreach ($sxml->tasks->task as $sxtask) {
$ret['tasks'][] = $this->calcSQL($sxtask, false);
}
}
break;
}
return json_encode($ret);
}
/**
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
$period = isset($params['period']) ? $params['period'] : self::MINPERIOD;
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<tasksettings>
<period>". min(max($period, self::MINPERIOD), self::MAXPERIOD) ."</period>
<logsql>0</logsql>
<!--
<tasks>
//Maintain offline (sb4 = 1) all docs under copyright
<task active=\"1\" name=\"confidentiel\" action=\"update\" sbas_id=\"1\">
<from>
<date direction=\"before\" field=\"FIN_COPYRIGHT\"/>
</from>
<to>
<status mask=\"x1xxxx\"/>
</to>
</task>
//Put online (sb4 = 0) all docs from 'public' collection and between the copyright date and the date of filing
<task active=\"1\" name=\"visible\" action=\"update\" sbas_id=\"1\">
<from>
<coll compare=\"=\" id=\"5\"/>
<date direction=\"after\" field=\"FIN_COPYRIGHT\"/>
<date direction=\"before\" field=\"ARCHIVAGE\"/>
</from>
<to>
<status mask=\"x0xxxx\"/>
</to>
</task>
// Warn 10 days before archiving (raise sb5)
<task active=\"1\" name=\"bientôt la fin\" action=\"update\" sbas_id=\"1\">
<from>
<coll compare=\"=\" id=\"5\"/>
<date direction=\"after\" field=\"ARCHIVAGE\" delta=\"-10\"/>
</from>
<to>
<status mask=\"1xxxxx\"/>
</to>
</task>
//Move to 'archive' collection
<task active=\"1\" name=\"archivage\" action=\"update\" sbas_id=\"1\">
<from>
<coll compare=\"=\" id=\"5\"/>
<date direction=\"after\" field=\"ARCHIVAGE\" />
</from>
<to>
<status mask=\"00xxxx\"/> on nettoie les status pour la forme
<coll id=\"666\" />
</to>
</task>
//Purge the archived documents from one year that are in the 'archive' collection
<task active=\"1\" name=\"archivage\" action=\"delete\" sbas_id=\"1\">
<from>
<coll compare=\"=\" id=\"666\"/>
<date direction=\"after\" field=\"ARCHIVAGE\" delta=\"+365\" />
</from>
</task>
</tasks>
-->
</tasksettings>";
}
}

View File

@@ -9,11 +9,6 @@
* file that was distributed with this source code.
*/
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class task_period_apibridge extends task_appboxAbstract
{

View File

@@ -9,6 +9,7 @@
*/
use Alchemy\Phrasea\Border\File;
use Alchemy\Phrasea\Core\Configuration\Configuration;
use Alchemy\Phrasea\Metadata\Tag as PhraseaTag;
use Alchemy\Phrasea\Border\Attribute as BorderAttribute;
use Alchemy\Phrasea\Border\MetadataBag;
@@ -17,11 +18,6 @@ use PHPExiftool\Driver\Metadata\MetadataBag as ExiftoolMetadataBag;
use PHPExiftool\Driver\Metadata\Metadata;
use Symfony\Component\Filesystem\Exception\IOException;
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class task_period_archive extends task_abstract
{
const MINCOLD = 5;
@@ -2206,4 +2202,24 @@ class CListFolder
{
return array_shift($this->list);
}
/**
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
$period = isset($params['period']) ? $params['period'] : self::MINPERIOD;
return sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<tasksettings>
<base_id></base_id>
<hotfolder></hotfolder>
<period>%s</period>
<move_archived>0</move_archived>
<move_error>0</move_error>
<delfolder>0</delfolder>
<copy_spe>0</copy_spe>
<cold></cold>
</tasksettings>", min(max($period, self::MINPERIOD), self::MAXPERIOD));
}
}

View File

@@ -8,13 +8,9 @@
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Core\Configuration\Configuration;
use Symfony\Component\Process\ExecutableFinder;
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class task_period_cindexer extends task_abstract
{
// how to execute indexer (choose in 'run2' method)
@@ -656,4 +652,23 @@ class task_period_cindexer extends task_abstract
pcntl_exec($cmd, $args);
sleep(2);
}
/**
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
$binaries = $config['binaries'];
$database = $config['main']['database'];
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tasksettings>\n<binpath>"
. (array_key_exists('phraseanet_indexer', $binaries) ? str_replace('/phraseanet_indexer', '', $binaries['phraseanet_indexer']) : '')
. "</binpath><host>" . $database['host'] . "</host><port>"
. $database['port'] . "</port><base>"
. $database['dbname'] . "</base><user>"
. $database['user'] . "</user><password>"
. $database['password'] . "</password><socket>25200</socket>"
. "<use_sbas>1</use_sbas><nolog>0</nolog><clng></clng>"
. "<winsvc_run>0</winsvc_run><charset>utf8</charset></tasksettings>";
}
}

View File

@@ -9,11 +9,6 @@
* file that was distributed with this source code.
*/
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class task_period_emptyColl extends task_appboxAbstract
{
protected $base_id;
@@ -35,6 +30,15 @@ class task_period_emptyColl extends task_appboxAbstract
return("Vide une collection");
}
/**
*
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tasksettings>\n</tasksettings>";
}
protected function loadSettings(SimpleXMLElement $sx_task_settings)
{
$this->base_id = (int) $sx_task_settings->base_id;

View File

@@ -8,10 +8,12 @@
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Core\Configuration\Configuration;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Notification\Mail\MailSuccessFTPSender;
use Alchemy\Phrasea\Notification\Receiver;
class task_period_ftp extends task_appboxAbstract
{
protected $proxy;
@@ -704,4 +706,21 @@ class task_period_ftp extends task_appboxAbstract
return $this;
}
/**
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
$period = isset($params['period']) ? $params['period'] : self::MINPERIOD;
return sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<tasksettings>
<proxy></proxy>
<proxyport></proxyport>
<period>%s</period>
<syslog></syslog>
</tasksettings>", min(max($period, self::MINPERIOD), self::MAXPERIOD));
}
}

View File

@@ -8,11 +8,8 @@
* file that was distributed with this source code.
*/
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
use Alchemy\Phrasea\Core\Configuration\Configuration;
class task_period_ftpPull extends task_appboxAbstract
{
protected $proxy;
@@ -453,4 +450,27 @@ class task_period_ftpPull extends task_appboxAbstract
{
}
/**
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
$period = isset($params['period']) ? $params['period'] : self::MINPERIOD;
return sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<tasksettings>
<proxy></proxy>
<proxyport></proxyport>
<period>%s</period>
<passive>0</passive>
<ssl>0</ssl>
<password></password>
<user></user>
<ftppath></ftppath>
<localpath></localpath>
<port>21</port>
<host></host>
</tasksettings>", min(max($period, self::MINPERIOD), self::MAXPERIOD));
}
}

View File

@@ -8,6 +8,8 @@
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Core\Configuration\Configuration;
class task_period_subdef extends task_databoxAbstract
{
const MINMEGS = 20;
@@ -328,4 +330,28 @@ class task_period_subdef extends task_databoxAbstract
return $this;
}
/**
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
$period = isset($params['period']) ? $params['period'] : self::MINPERIOD;
$flush = isset($params['flush']) ? $params['flush'] : self::MINFLUSH;
$maxrecs = isset($params['maxrecs']) ? $params['maxrecs'] : self::MINRECS;
$maxmegs = isset($params['maxmegs']) ? $params['maxmegs'] : self::MINMEGS;
return sprintf('<?xml version="1.0" encoding="UTF-8"?>
<tasksettings>
<period>%s</period>
<flush>%s</flush>
<maxrecs>%s</maxrecs>
<maxmegs>%s</maxmegs>
</tasksettings>',
min(max($period, self::MINPERIOD), self::MAXPERIOD),
min(max($flush, self::MINFLUSH), self::MAXFLUSH),
min(max($maxrecs, self::MINRECS), self::MAXRECS),
min(max($maxmegs, self::MINMEGS), self::MAXMEGS)
);
}
}

View File

@@ -9,11 +9,6 @@
* file that was distributed with this source code.
*/
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class task_period_test extends task_appboxAbstract
{

View File

@@ -8,16 +8,12 @@
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Core\Configuration\Configuration;
use PHPExiftool\Driver\Metadata;
use PHPExiftool\Driver\Value;
use PHPExiftool\Driver\Tag;
use PHPExiftool\Writer;
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class task_period_writemeta extends task_databoxAbstract
{
protected $clear_doc;
@@ -340,4 +336,26 @@ class task_period_writemeta extends task_databoxAbstract
return $this;
}
/**
* @param array $params
*/
public static function getDefaultSettings(Configuration $config, array $params = array())
{
$period = isset($params['period']) ? $params['period'] : self::MINPERIOD;
$maxrecs = isset($params['maxrecs']) ? $params['maxrecs'] : self::MINRECS;
$maxmegs = isset($params['maxmegs']) ? $params['maxmegs'] : self::MINMEGS;
return sprintf('<?xml version="1.0" encoding="UTF-8"?>
<tasksettings>
<period>%s</period>
<maxrecs>%s</maxrecs>
<maxmegs>%s</maxmegs>
<cleardoc>0</cleardoc>
</tasksettings>',
min(max($period, self::MINPERIOD), self::MAXPERIOD),
min(max($maxrecs, self::MINRECS), self::MAXRECS),
min(max($maxmegs, self::MINMEGS), self::MAXMEGS)
);
}
}

View File

@@ -16,7 +16,7 @@ class task_period_archiveTest extends \PhraseanetPHPUnitAbstract
$app = new Application('test');
$task = \task_period_archive::create($app, 'task_period_archive');
$task = \task_period_archive::create($app);
$logger = new \Monolog\Logger('test');
$logger->pushHandler(new \Monolog\Handler\NullHandler());

View File

@@ -15,7 +15,7 @@ class task_abstractTest extends PhraseanetPHPUnitAbstract
parent::setUpBeforeClass();
$app = new Application('test');
self::$task = task_abstract::create($app, 'task_period_test');
self::$task = task_period_test::create($app);
self::$tid = self::$task->getID();
}