[FEATURE] Refactoring of scheduler CLI 22/38022/6
authorMartin Tepper <martintepper@arcor.de>
Mon, 23 Mar 2015 08:05:36 +0000 (09:05 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 5 Apr 2015 16:56:34 +0000 (18:56 +0200)
Refactoring of scheduler CLI. Move the biggest part of the old
scheduler_cli_dispatch.php to an own controller. Add a new parameter "s"
to stop a running scheduler task.

Change-Id: I88fff1aa9c9d1f4322c70a3ab3627e92ee059b63
Resolves: #65918
Resolves: #63703
Releases: master
Reviewed-on: http://review.typo3.org/38022
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Documentation/Changelog/master/Feature-63703-AddOptionToStopTask.rst [new file with mode: 0644]
typo3/sysext/scheduler/Classes/Controller/SchedulerCliController.php [new file with mode: 0644]
typo3/sysext/scheduler/cli/scheduler_cli_dispatch.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-63703-AddOptionToStopTask.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-63703-AddOptionToStopTask.rst
new file mode 100644 (file)
index 0000000..9dd2c44
--- /dev/null
@@ -0,0 +1,9 @@
+====================================================================
+Feature: #63703 - Add option to stop a running task in the scheduler
+====================================================================
+
+Description
+===========
+
+The scheduler CLI has an option now to stop a running task.
+The new option can be used with "-s".
\ No newline at end of file
diff --git a/typo3/sysext/scheduler/Classes/Controller/SchedulerCliController.php b/typo3/sysext/scheduler/Classes/Controller/SchedulerCliController.php
new file mode 100644 (file)
index 0000000..45eec13
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+namespace TYPO3\CMS\Scheduler\Controller;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * CLI controller for the 'scheduler' extension.
+ *
+ * @author Markus Friedrich <markus.friedrich@dkd.de>
+ */
+class SchedulerCliController {
+
+       /**
+        * @var \TYPO3\CMS\Core\Controller\CommandLineController
+        */
+       protected $cli;
+
+       /**
+        * @var bool
+        */
+       protected $hasTask = TRUE;
+
+       /**
+        * @var \TYPO3\CMS\Scheduler\Scheduler
+        */
+       protected $scheduler;
+
+       /**
+        * Constructor
+        */
+       public function __construct() {
+               $this->cli = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Controller\CommandLineController::class);
+               $this->scheduler = GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Scheduler::class);
+       }
+
+       /**
+        * Execute scheduler tasks
+        */
+       public function run() {
+               if ($this->cli->cli_isArg('-i') && $this->cli->cli_isArg('-i') > 0) {
+                       /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
+                       $task = $this->getTask();
+                       if ($this->scheduler->isValidTaskObject($task)) {
+                               if ($this->cli->cli_isArg('-s')) {
+                                       $this->stopTask($task);
+                               } else {
+                                       $this->scheduler->executeTask($task);
+                               }
+
+                               // Record the run in the system registry
+                               $this->scheduler->recordLastRun('cli-by-id');
+                       }
+                       return;
+               }
+               $this->loopTasks();
+       }
+
+       /**
+        * Stop task
+        *
+        * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
+        */
+       protected function stopTask($task) {
+               if ($this->scheduler->isValidTaskObject($task)) {
+                       $result = $task->unmarkAllExecutions();
+               }
+       }
+
+       /**
+        * Return task
+        *
+        * @return \TYPO3\CMS\Scheduler\Task\AbstractTask
+        */
+       protected function getTask() {
+               $taskId = (int)$this->cli->cli_argValue('-i');
+
+               if ($this->cli->cli_isArg('-f') || $this->cli->cli_isArg('-s')) {
+                       $task = $this->scheduler->fetchTask($taskId);
+               } else {
+                       $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' . $GLOBALS['EXEC_TIME'];
+                       list($task) = $this->scheduler->fetchTasksWithCondition($whereClause);
+               }
+
+               return $task;
+       }
+
+       /**
+        * Execute tasks in loop
+        */
+       protected function loopTasks() {
+               do {
+                       // Try getting the next task and execute it
+                       // If there are no more tasks to execute, an exception is thrown by \TYPO3\CMS\Scheduler\Scheduler::fetchTask()
+                       try {
+                               /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
+                               $task = $this->scheduler->fetchTask();
+                               try {
+                                       $this->scheduler->executeTask($task);
+                               } catch (\Exception $e) {
+                                       // We ignore any exception that may have been thrown during execution,
+                                       // as this is a background process.
+                                       // The exception message has been recorded to the database anyway
+                                       continue;
+                               }
+                       } catch (\OutOfBoundsException $e) {
+                               $this->hasTask = FALSE;
+                       } catch (\UnexpectedValueException $e) {
+                               continue;
+                       }
+               } while ($this->hasTask);
+               // Record the run in the system registry
+               $this->scheduler->recordLastRun();
+       }
+
+}
index 6b2ebff..b56c706 100644 (file)
  *
  * @author Markus Friedrich <markus.friedrich@dkd.de>
  */
-if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI && basename(PATH_thisScript) === 'cli_dispatch.phpsh') {
-       $hasTask = TRUE;
-       // Create an instance of the scheduler object
-       /** @var $scheduler \TYPO3\CMS\Scheduler\Scheduler */
-       $scheduler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Scheduler::class);
-       /** @var \TYPO3\CMS\Core\Controller\CommandLineController $cli */
-       $cli = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Controller\CommandLineController::class);
-       // If a specific id is given in arguments, then run that task. Otherwise run scheduled tasks.
-       if ($cli->cli_isArg('-i')) {
-               $taskId = (int)$cli->cli_argValue('-i');
-               if ($taskId > 0) {
-                       // Force the execution of the task even if it is disabled or no execution scheduled
-                       if ($cli->cli_isArg('-f')) {
-                               $task = $scheduler->fetchTask($taskId);
-                       } else {
-                               $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' . $GLOBALS['EXEC_TIME'];
-                               list($task) = $scheduler->fetchTasksWithCondition($whereClause);
-                       }
-                       if ($scheduler->isValidTaskObject($task)) {
-                               try {
-                                       $scheduler->executeTask($task);
-                               } catch (\Exception $e) {
 
-                               }
-                               // Record the run in the system registry
-                               $scheduler->recordLastRun('cli-by-id');
-                       }
-               }
-       } else {
-               // Loop as long as there are tasks
-               do {
-                       // Try getting the next task and execute it
-                       // If there are no more tasks to execute, an exception is thrown by \TYPO3\CMS\Scheduler\Scheduler::fetchTask()
-                       try {
-                               /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
-                               $task = $scheduler->fetchTask();
-                               $hasTask = TRUE;
-                               try {
-                                       $scheduler->executeTask($task);
-                               } catch (\Exception $e) {
-                                       // We ignore any exception that may have been thrown during execution,
-                                       // as this is a background process.
-                                       // The exception message has been recorded to the database anyway
-                                       continue;
-                               }
-                       } catch (\OutOfBoundsException $e) {
-                               $hasTask = FALSE;
-                       } catch (\UnexpectedValueException $e) {
-                               continue;
-                       }
-               } while ($hasTask);
-               // Record the run in the system registry
-               $scheduler->recordLastRun();
-       }
+if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI && basename(PATH_thisScript) === 'cli_dispatch.phpsh') {
+       $schedulerCliController = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Controller\SchedulerCliController::class);
+       $schedulerCliController->run();
 } else {
        die('This script must be included by the "CLI module dispatcher"');
-}
+}
\ No newline at end of file