diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-01-03 02:24:53 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-01-12 05:02:19 +0300 |
commit | 1ab2d4fcc422f09df8fb5c3ae31d92e39be09650 (patch) | |
tree | 80679bfd21d430bc2bbdd0b3c2f891e2a10cc673 /core/TaskScheduler.php | |
parent | a3dbbf62fe52a604725399c26b01d551d6dc5a25 (diff) |
Moved Piwik\TaskScheduler to Piwik\Scheduler\Scheduler
Diffstat (limited to 'core/TaskScheduler.php')
-rw-r--r-- | core/TaskScheduler.php | 123 |
1 files changed, 15 insertions, 108 deletions
diff --git a/core/TaskScheduler.php b/core/TaskScheduler.php index 6e660a5162..a957c4774d 100644 --- a/core/TaskScheduler.php +++ b/core/TaskScheduler.php @@ -9,9 +9,9 @@ namespace Piwik; -use Exception; -use Piwik\Plugin\Manager as PluginManager; -use Piwik\Scheduler\Timetable; +use Piwik\Container\StaticContainer; +use Piwik\Scheduler\Scheduler; +use Piwik\Scheduler\Task; // When set to true, all scheduled tasks will be triggered in all requests (careful!) //define('DEBUG_FORCE_SCHEDULED_TASKS', true); @@ -50,21 +50,11 @@ use Piwik\Scheduler\Timetable; * * echo "Executed task '$task1Name'. Task output:\n$task1Output"; * - * @method static \Piwik\TaskScheduler getInstance() + * @deprecated Use Piwik\Scheduler\Scheduler instead + * @see \Piwik\Scheduler\Scheduler */ -class TaskScheduler extends Singleton +class TaskScheduler { - const GET_TASKS_EVENT = 'TaskScheduler.getScheduledTasks'; - - private $isRunning = false; - - private $timetable = null; - - public function __construct() - { - $this->timetable = new Timetable(); - } - /** * Executes tasks that are scheduled to run, then reschedules them. * @@ -80,79 +70,7 @@ class TaskScheduler extends Singleton */ public static function runTasks() { - return self::getInstance()->doRunTasks(); - } - - // for backwards compatibility - private function collectTasksRegisteredViaEvent() - { - $tasks = array(); - - /** - * @ignore - * @deprecated - */ - Piwik::postEvent(self::GET_TASKS_EVENT, array(&$tasks)); - - return $tasks; - } - - private function getScheduledTasks() - { - /** @var \Piwik\ScheduledTask[] $tasks */ - $tasks = $this->collectTasksRegisteredViaEvent(); - - /** @var \Piwik\Plugin\Tasks[] $pluginTasks */ - $pluginTasks = PluginManager::getInstance()->findComponents('Tasks', 'Piwik\\Plugin\\Tasks'); - foreach ($pluginTasks as $pluginTask) { - - $pluginTask->schedule(); - - foreach ($pluginTask->getScheduledTasks() as $task) { - $tasks[] = $task; - } - } - - return $tasks; - } - - private function doRunTasks() - { - $tasks = $this->getScheduledTasks(); - - // remove from timetable tasks that are not active anymore - $this->timetable->removeInactiveTasks($tasks); - - // for every priority level, starting with the highest and concluding with the lowest - $executionResults = array(); - for ($priority = ScheduledTask::HIGHEST_PRIORITY; - $priority <= ScheduledTask::LOWEST_PRIORITY; - ++$priority) { - // loop through each task - foreach ($tasks as $task) { - // if the task does not have the current priority level, don't execute it yet - if ($task->getPriority() != $priority) { - continue; - } - - $taskName = $task->getName(); - $shouldExecuteTask = $this->timetable->shouldExecuteTask($taskName); - - if ($this->timetable->taskShouldBeRescheduled($taskName)) { - $this->timetable->rescheduleTask($task); - } - - if ($shouldExecuteTask) { - $this->isRunning = true; - $message = self::executeTask($task); - $this->isRunning = false; - - $executionResults[] = array('task' => $taskName, 'output' => $message); - } - } - } - - return $executionResults; + return self::getInstance()->run(); } /** @@ -161,12 +79,12 @@ class TaskScheduler extends Singleton * Call this method if your task's scheduled time has changed due to, for example, an option that * was changed. * - * @param ScheduledTask $task Describes the scheduled task being rescheduled. + * @param Task $task Describes the scheduled task being rescheduled. * @api */ - public static function rescheduleTask(ScheduledTask $task) + public static function rescheduleTask(Task $task) { - self::getInstance()->timetable->rescheduleTask($task); + self::getInstance()->rescheduleTask($task); } /** @@ -176,7 +94,7 @@ class TaskScheduler extends Singleton */ public static function isTaskBeingExecuted() { - return self::getInstance()->isRunning; + return self::getInstance()->isRunning(); } /** @@ -190,25 +108,14 @@ class TaskScheduler extends Singleton */ public static function getScheduledTimeForMethod($className, $methodName, $methodParameter = null) { - return self::getInstance()->timetable->getScheduledTimeForMethod($className, $methodName, $methodParameter); + return self::getInstance()->getScheduledTimeForMethod($className, $methodName, $methodParameter); } /** - * Executes the given taks - * - * @param ScheduledTask $task - * @return string + * @return Scheduler */ - private static function executeTask($task) + private static function getInstance() { - try { - $timer = new Timer(); - call_user_func(array($task->getObjectInstance(), $task->getMethodName()), $task->getMethodParameter()); - $message = $timer->__toString(); - } catch (Exception $e) { - $message = 'ERROR: ' . $e->getMessage(); - } - - return $message; + return StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler'); } } |