Skip to content
Extraits de code Groupes Projets
Valider 756d1e0d rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

refs #5301 added scheduled task generator, some code tweaks and more docs

parent 3e03b0a7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -342,11 +342,10 @@ class Manager extends Singleton
*/
public function findComponents($componentName, $expectedSubclass)
{
$pluginNames = $this->getLoadedPlugins();
$plugins = $this->getLoadedPlugins();
$components = array();
foreach ($pluginNames as $plugin) {
foreach ($plugins as $plugin) {
$component = $plugin->findComponent($componentName, $expectedSubclass);
if (!empty($component)) {
......
......@@ -24,8 +24,13 @@ class Tasks
const HIGH_PRIORITY = ScheduledTask::HIGH_PRIORITY;
const HIGHEST_PRIORITY = ScheduledTask::HIGHEST_PRIORITY;
/**
* This method is called to collect all schedule tasks. Register all your tasks here that should be executed
* regularily such as daily or monthly.
*/
public function schedule()
{
// eg $this->daily('myMethodName')
}
/**
......@@ -121,12 +126,18 @@ class Tasks
$objectOrClassName = get_class($objectOrClassName);
}
$this->addScheduledTask(new ScheduledTask($objectOrClassName, $methodName, $methodParameter, $time, $priority));
$this->scheduleTask(new ScheduledTask($objectOrClassName, $methodName, $methodParameter, $time, $priority));
return $time;
}
protected function addScheduledTask(ScheduledTask $task)
/**
* In case you need very high flexibility and none of the other convenient methods such as {@link hourly()} or
* {@link custom()} suit you, you can use this method to add a custom scheduled task.
*
* @param ScheduledTask $task
*/
protected function scheduleTask(ScheduledTask $task)
{
$this->tasks[] = $task;
}
......
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CoreConsole\Commands;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
*/
class GenerateScheduledTask extends GeneratePluginBase
{
protected function configure()
{
$this->setName('generate:scheduledtask')
->setDescription('Adds a tasks class to an existing plugin which allows you to specify scheduled tasks')
->addOption('pluginname', null, InputOption::VALUE_REQUIRED, 'The name of an existing plugin which does not have any tasks defined yet');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$pluginName = $this->getPluginName($input, $output);
$exampleFolder = PIWIK_INCLUDE_PATH . '/plugins/ExamplePlugin';
$replace = array('ExamplePlugin' => $pluginName);
$whitelistFiles = array('/Tasks.php');
$this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);
$this->writeSuccessMessage($output, array(
sprintf('Tasks.php for %s generated.', $pluginName),
'You can now start specifying your scheduled tasks',
'Enjoy!'
));
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @throws \RunTimeException
*/
protected function getPluginName(InputInterface $input, OutputInterface $output)
{
$pluginNames = $this->getPluginNamesHavingNotSpecificFile('Tasks.php');
$invalidName = 'You have to enter the name of an existing plugin which does not already have any tasks defined';
return $this->askPluginNameAndValidate($input, $output, $pluginNames, $invalidName);
}
}
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\ExamplePlugin;
class Tasks extends \Piwik\Plugin\Tasks
{
public function schedule()
{
$this->hourly('myTask'); // method will be executed once every hour
$this->daily('myTask'); // method will be executed every daily
$this->weekly('myTask'); // method will be executed once every weekly
$this->monthly('myTask'); // method will be executed once every monthly
// pass a parameter to the task
$this->weekly('myTaskWithParam', 'anystring');
// specify a different priority
$this->monthly('myTask', null, self::LOWEST_PRIORITY);
$this->monthly('myTaskWithParam', 'anystring', self::HIGH_PRIORITY);
}
public function myTask()
{
// do something
}
public function myTaskWithParam($param)
{
// do something
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ namespace Piwik\Plugins\PrivacyManager;
class Tasks extends \Piwik\Plugin\Tasks
{
public function schedule()
{
$this->daily('deleteReportData', null, self::LOW_PRIORITY);
......
......@@ -14,7 +14,7 @@ class Tasks extends \Piwik\Plugin\Tasks
{
// add the auto updater task if GeoIP admin is enabled
if (UserCountry::isGeoLocationAdminEnabled()) {
$this->addScheduledTask(new GeoIPAutoUpdater());
$this->scheduleTask(new GeoIPAutoUpdater());
}
}
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter