[FEATURE] 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 $this->cli->cli_options[] = array('-h', 'Show this output');
46 $this->cli->cli_options[] = array('--help', 'Same as -h');
47 // Setting help texts:
48 $this->cli->cli_help['name'] = 'scheduler -- Start the TYPO3 Scheduler from the command line';
49 $this->cli->cli_help['synopsis'] = '###OPTIONS###';
50 $this->cli->cli_help['description'] = 'This command line starts any task';
51 $this->cli->cli_help['examples'] = 'typo3/cli_dispatch.phpsh scheduler';
52 unset($this->cli->cli_help['author']);
53 }
54
55 /**
56 * Check cli is help
57 *
58 * @return bool
59 */
60 protected function isHelp() {
61 return ($this->cli->cli_isArg('--help') && $this->cli->cli_isArg('--help') > 0)
62 || ($this->cli->cli_isArg('-h') && $this->cli->cli_isArg('-h') > 0);
63 }
64
65
66 /**
67 * Execute scheduler tasks
68 */
69 public function run() {
70 if ($this->isHelp()) {
71 $this->cli->cli_help();
72 return;
73 }
74
75 if ($this->cli->cli_isArg('-i') && $this->cli->cli_isArg('-i') > 0) {
76 /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
77 $task = $this->getTask();
78 if ($this->scheduler->isValidTaskObject($task)) {
79 if ($this->cli->cli_isArg('-s')) {
80 $this->stopTask($task);
81 } else {
82 $this->scheduler->executeTask($task);
83 }
84
85 // Record the run in the system registry
86 $this->scheduler->recordLastRun('cli-by-id');
87 }
88 return;
89 }
90 $this->loopTasks();
91 }
92
93 /**
94 * Stop task
95 *
96 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
97 */
98 protected function stopTask($task) {
99 if ($this->scheduler->isValidTaskObject($task)) {
100 $result = $task->unmarkAllExecutions();
101 }
102 }
103
104 /**
105 * Return task
106 *
107 * @return \TYPO3\CMS\Scheduler\Task\AbstractTask
108 */
109 protected function getTask() {
110 $taskId = (int)$this->cli->cli_argValue('-i');
111
112 if ($this->cli->cli_isArg('-f') || $this->cli->cli_isArg('-s')) {
113 $task = $this->scheduler->fetchTask($taskId);
114 } else {
115 $whereClause = 'uid = ' . $taskId . ' AND nextexecution != 0 AND nextexecution <= ' . $GLOBALS['EXEC_TIME'];
116 list($task) = $this->scheduler->fetchTasksWithCondition($whereClause);
117 }
118
119 return $task;
120 }
121
122 /**
123 * Execute tasks in loop
124 */
125 protected function loopTasks() {
126 do {
127 // Try getting the next task and execute it
128 // If there are no more tasks to execute, an exception is thrown by \TYPO3\CMS\Scheduler\Scheduler::fetchTask()
129 try {
130 /** @var $task \TYPO3\CMS\Scheduler\Task\AbstractTask */
131 $task = $this->scheduler->fetchTask();
132 try {
133 $this->scheduler->executeTask($task);
134 } catch (\Exception $e) {
135 // We ignore any exception that may have been thrown during execution,
136 // as this is a background process.
137 // The exception message has been recorded to the database anyway
138 continue;
139 }
140 } catch (\OutOfBoundsException $e) {
141 $this->hasTask = FALSE;
142 } catch (\UnexpectedValueException $e) {
143 continue;
144 }
145 } while ($this->hasTask);
146 // Record the run in the system registry
147 $this->scheduler->recordLastRun();
148 }
149
150 }