[FEATURE] Trigger execution of a specific task from CLI
authorPhilipp Gampe <philipp.gampe@typo3.org>
Sat, 30 Jun 2012 16:03:10 +0000 (18:03 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Sat, 18 Aug 2012 18:21:02 +0000 (20:21 +0200)
Make it possible run a specific task by providing an additional
parameter -i and the uid of the task on the command line.

./typo3/cli_dispatch.phpsh scheduler -i <uid>

Use the parameter -f to force the execution even if the task is
disabled or no execution is scheduled.

Change-Id: I6226ea41fbb391a56f9eee3d3de919cc116157bc
Resolves: #31073
Relates: #38506
Releases: 6.0
Reviewed-on: http://review.typo3.org/12481
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Oliver Hader
Tested-by: Oliver Hader
typo3/sysext/scheduler/class.tx_scheduler.php
typo3/sysext/scheduler/cli/scheduler_cli_dispatch.php

index ad091fe..c9de35b 100755 (executable)
@@ -207,7 +207,7 @@ class tx_scheduler implements t3lib_Singleton {
         */
        public function recordLastRun($type = 'cron') {
                        // Validate input value
-               if ($type != 'manual') {
+               if ($type !== 'manual' && $type !== 'cli-by-id') {
                        $type = 'cron';
                }
 
@@ -296,17 +296,15 @@ class tx_scheduler implements t3lib_Singleton {
                        // Otherwise unserialize the task and return it
                } else {
                        $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                       /** @var $task tx_scheduler_Task */
                        $task = unserialize($row['serialized_task_object']);
 
                        if ($this->isValidTaskObject($task)) {
-                               // The task is valid, return it
-
+                                       // The task is valid, return it
                                $task->setScheduler();
-
                        } else {
-                               // Forcibly set the disable flag to 1 in the database,
-                               // so that the task does not come up again and again for execution
-
+                                       // Forcibly set the disable flag to 1 in the database,
+                                       // so that the task does not come up again and again for execution
                                $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_scheduler_task', 'uid = ' . $row['uid'], array('disable' => 1));
                                        // Throw an exception to raise the problem
                                throw new UnexpectedValueException('Could not unserialize task', 1255083671);
@@ -348,6 +346,7 @@ class tx_scheduler implements t3lib_Singleton {
         * @return array List of task objects
         */
        public function fetchTasksWithCondition($where, $includeDisabledTasks = FALSE) {
+               $whereClause = '';
                $tasks = array();
                if (!empty($where)) {
                        $whereClause = $where;
@@ -361,6 +360,7 @@ class tx_scheduler implements t3lib_Singleton {
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('serialized_task_object', 'tx_scheduler_task', $whereClause);
                if ($res) {
                        while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                               /** @var $task tx_scheduler_task */
                                $task = unserialize($row['serialized_task_object']);
                                        // Add the task to the list only if it is valid
                                if ($this->isValidTaskObject($task)) {
index a97b3fe..13bb46b 100644 (file)
@@ -35,37 +35,67 @@ if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) && basename(PATH_thisScript) ==
                // Create an instance of the scheduler object
                /** @var $scheduler tx_scheduler */
        $scheduler = t3lib_div::makeInstance('tx_scheduler');
-               // 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 tx_scheduler::fetchTask()
-               try {
-                               /** @var $task tx_scheduler_Task */
-                       $task = $scheduler->fetchTask();
-                       $hasTask = TRUE;
-                       try {
-                               $scheduler->executeTask($task);
+       /** @var t3lib_cli $cli */
+       $cli = t3lib_div::makeInstance('t3lib_cli');
+               // If a specific id is given in arguments, then run that task. Otherwise run scheduled tasks.
+       if ($cli->cli_isArg('-i')) {
+               $taskId = intval($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);
                        }
-                       catch (Exception $e) {
+                       if ($scheduler->isValidTaskObject($task)) {
+                               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;
+                                       // The exception message has been recorded to the database anyway.
+                               }
+                                       // Record the run in the system registry
+                               $scheduler->recordLastRun('cli-by-id');
                        }
                }
-                       // There are no more tasks, quit the run
-               catch (OutOfBoundsException $e) {
-                       $hasTask = FALSE;
-               }
-                       // A task could not be unserialized properly, skip to next task
-               catch (UnexpectedValueException $e) {
-                       continue;
-               }
-       } while ($hasTask);
-               // Record the run in the system registry
-       $scheduler->recordLastRun();
+
+       } 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 tx_scheduler::fetchTask()
+                       try {
+                                       /** @var $task tx_scheduler_Task */
+                               $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;
+                               }
+                       }
+                               // There are no more tasks, quit the run
+                       catch (OutOfBoundsException $e) {
+                               $hasTask = FALSE;
+                       }
+                               // A task could not be unserialized properly, skip to next task
+                       catch (UnexpectedValueException $e) {
+                               continue;
+                       }
+               } while ($hasTask);
+                       // Record the run in the system registry
+               $scheduler->recordLastRun();
+       }
 } else {
        die('This script must be included by the "CLI module dispatcher"');
 }
 
-?>
\ No newline at end of file
+?>