[FEATURE] Trigger execution of a specific task from CLI
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / cli / scheduler_cli_dispatch.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 Markus Friedrich (markus.friedrich@dkd.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Starts all due tasks, used by the command line interface
27 * This script must be included by the "CLI module dispatcher"
28 *
29 * @author Markus Friedrich <markus.friedrich@dkd.de>
30 * @package TYPO3
31 * @subpackage tx_scheduler
32 */
33 if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) && basename(PATH_thisScript) == 'cli_dispatch.phpsh') {
34 $hasTask = TRUE;
35 // Create an instance of the scheduler object
36 /** @var $scheduler tx_scheduler */
37 $scheduler = t3lib_div::makeInstance('tx_scheduler');
38 /** @var t3lib_cli $cli */
39 $cli = t3lib_div::makeInstance('t3lib_cli');
40 // If a specific id is given in arguments, then run that task. Otherwise run scheduled tasks.
41 if ($cli->cli_isArg('-i')) {
42 $taskId = intval($cli->cli_argValue('-i'));
43 if ($taskId > 0) {
44 // Force the execution of the task even if it is disabled or no execution scheduled
45 if ($cli->cli_isArg('-f')) {
46 $task = $scheduler->fetchTask($taskId);
47 } else {
48 $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' .
49 $GLOBALS['EXEC_TIME'];
50 list($task) = $scheduler->fetchTasksWithCondition($whereClause);
51 }
52 if ($scheduler->isValidTaskObject($task)) {
53 try {
54 $scheduler->executeTask($task);
55 }
56 catch (Exception $e) {
57 // We ignore any exception that may have been thrown during execution,
58 // as this is a background process.
59 // The exception message has been recorded to the database anyway.
60 }
61 // Record the run in the system registry
62 $scheduler->recordLastRun('cli-by-id');
63 }
64 }
65
66 } else {
67 // Loop as long as there are tasks
68 do {
69 // Try getting the next task and execute it
70 // If there are no more tasks to execute, an exception is thrown by tx_scheduler::fetchTask()
71 try {
72 /** @var $task tx_scheduler_Task */
73 $task = $scheduler->fetchTask();
74 $hasTask = TRUE;
75 try {
76 $scheduler->executeTask($task);
77 }
78 catch (Exception $e) {
79 // We ignore any exception that may have been thrown during execution,
80 // as this is a background process.
81 // The exception message has been recorded to the database anyway
82 continue;
83 }
84 }
85 // There are no more tasks, quit the run
86 catch (OutOfBoundsException $e) {
87 $hasTask = FALSE;
88 }
89 // A task could not be unserialized properly, skip to next task
90 catch (UnexpectedValueException $e) {
91 continue;
92 }
93 } while ($hasTask);
94 // Record the run in the system registry
95 $scheduler->recordLastRun();
96 }
97 } else {
98 die('This script must be included by the "CLI module dispatcher"');
99 }
100
101 ?>