[FOLLOWUP][TASK] Add -h --help as CLI option for scheduler
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Controller / SchedulerCliController.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * CLI controller for the 'scheduler' extension.
21 */
22 class SchedulerCliController {
23
24 /**
25 * @var \TYPO3\CMS\Core\Controller\CommandLineController
26 */
27 protected $cli;
28
29 /**
30 * @var bool
31 */
32 protected $hasTask = TRUE;
33
34 /**
35 * @var \TYPO3\CMS\Scheduler\Scheduler
36 */
37 protected $scheduler;
38
39 /**
40 * Constructor
41 */
42 public function __construct() {
43 $this->cli = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Controller\CommandLineController::class);
44 $this->scheduler = GeneralUtility::makeInstance(\TYPO3\CMS\Scheduler\Scheduler::class);
45
46 // Empty options array here because Scheduler uses "-s" as argument too
47 $this->cli->cli_options = array();
48 $this->cli->cli_options[] = array('-h', 'Show this output');
49 $this->cli->cli_options[] = array('--help', 'Same as -h');
50 $this->cli->cli_options[] = array('-s', 'Stop the task which is passed with -i option');
51 $this->cli->cli_options[] = array('-i', 'UID of an task');
52 $this->cli->cli_options[] = array('-f', 'Force execution of the task which is passed with -i option');
53
54 // Setting help texts:
55 $this->cli->cli_help['name'] = 'scheduler -- Start the TYPO3 Scheduler from the command line';
56 $this->cli->cli_help['synopsis'] = '###OPTIONS###';
57 $this->cli->cli_help['description'] = 'This command line starts any task';
58 $this->cli->cli_help['examples'] = 'typo3/cli_dispatch.phpsh scheduler';
59 unset($this->cli->cli_help['author']);
60 }
61
62 /**
63 * Check cli is help
64 *
65 * @return bool
66 */
67 protected function isHelp() {
68 return ($this->cli->cli_isArg('--help') && $this->cli->cli_isArg('--help') > 0)
69 || ($this->cli->cli_isArg('-h') && $this->cli->cli_isArg('-h') > 0);
70 }
71
72
73 /**
74 * Execute scheduler tasks
75 */
76 public function run() {
77 if ($this->isHelp()) {
78 $this->cli->cli_help();
79 return;
80 }
81
82 if ($this->cli->cli_isArg('-i') && $this->cli->cli_isArg('-i') > 0) {
83 /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
84 $task = $this->getTask();
85 if ($this->scheduler->isValidTaskObject($task)) {
86 if ($this->cli->cli_isArg('-s')) {
87 $this->stopTask($task);
88 } else {
89 $this->scheduler->executeTask($task);
90 }
91
92 // Record the run in the system registry
93 $this->scheduler->recordLastRun('cli-by-id');
94 }
95 return;
96 }
97 $this->loopTasks();
98 }
99
100 /**
101 * Stop task
102 *
103 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
104 */
105 protected function stopTask($task) {
106 if ($this->scheduler->isValidTaskObject($task)) {
107 $result = $task->unmarkAllExecutions();
108 }
109 }
110
111 /**
112 * Return task
113 *
114 * @return \TYPO3\CMS\Scheduler\Task\AbstractTask
115 */
116 protected function getTask() {
117 $taskId = (int)$this->cli->cli_argValue('-i');
118
119 if ($this->cli->cli_isArg('-f') || $this->cli->cli_isArg('-s')) {
120 $task = $this->scheduler->fetchTask($taskId);
121 } else {
122 $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' . $GLOBALS['EXEC_TIME'];
123 list($task) = $this->scheduler->fetchTasksWithCondition($whereClause);
124 }
125
126 return $task;
127 }
128
129 /**
130 * Execute tasks in loop
131 */
132 protected function loopTasks() {
133 do {
134 // Try getting the next task and execute it
135 // If there are no more tasks to execute, an exception is thrown by \TYPO3\CMS\Scheduler\Scheduler::fetchTask()
136 try {
137 /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
138 $task = $this->scheduler->fetchTask();
139 try {
140 $this->scheduler->executeTask($task);
141 } catch (\Exception $e) {
142 // We ignore any exception that may have been thrown during execution,
143 // as this is a background process.
144 // The exception message has been recorded to the database anyway
145 continue;
146 }
147 } catch (\OutOfBoundsException $e) {
148 $this->hasTask = FALSE;
149 } catch (\UnexpectedValueException $e) {
150 continue;
151 }
152 } while ($this->hasTask);
153 // Record the run in the system registry
154 $this->scheduler->recordLastRun();
155 }
156
157 }