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