[BUGFIX] Rethrow Extbase scheduler task exceptions
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Scheduler / Task.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Scheduler;
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 /**
18 * Scheduler task to execute CommandController commands
19 */
20 class Task extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
21
22 /**
23 * @var string
24 */
25 protected $commandIdentifier;
26
27 /**
28 * @var array
29 */
30 protected $arguments = array();
31
32 /**
33 * @var array
34 */
35 protected $defaults = array();
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
39 */
40 protected $objectManager;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
44 */
45 protected $commandManager;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Scheduler\TaskExecutor
49 */
50 protected $taskExecutor;
51
52 /**
53 * Instantiates the Object Manager
54 */
55 public function __construct() {
56 parent::__construct();
57 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
58 $this->commandManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
59 $this->taskExecutor = $this->objectManager->get(\TYPO3\CMS\Extbase\Scheduler\TaskExecutor::class);
60 }
61
62 /**
63 * Sleep
64 *
65 * @return array Properties to serialize
66 */
67 public function __sleep() {
68 $properties = get_object_vars($this);
69 unset($properties['commandManager']);
70 unset($properties['objectManager']);
71 unset($properties['taskExecutor']);
72 return array_keys($properties);
73 }
74
75 /**
76 * Wakeup
77 *
78 * @return void
79 */
80 public function __wakeup() {
81 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
82 $this->commandManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
83 $this->taskExecutor = $this->objectManager->get(\TYPO3\CMS\Extbase\Scheduler\TaskExecutor::class);
84 }
85
86 /**
87 * Function execute from the Scheduler
88 *
89 * @return bool TRUE on successful execution
90 * @throws \Exception If an error occurs
91 */
92 public function execute() {
93 try {
94 $this->taskExecutor->execute($this);
95 } catch (\Exception $e) {
96 $this->logException($e);
97 // Make sure the Scheduler gets exception details
98 throw $e;
99 }
100 return TRUE;
101 }
102
103 /**
104 * @param string $commandIdentifier
105 */
106 public function setCommandIdentifier($commandIdentifier) {
107 $this->commandIdentifier = $commandIdentifier;
108 }
109
110 /**
111 * @return string
112 */
113 public function getCommandIdentifier() {
114 return $this->commandIdentifier;
115 }
116
117 /**
118 * @param array $arguments
119 */
120 public function setArguments($arguments) {
121 $this->arguments = $arguments;
122 }
123
124 /**
125 * @return array
126 */
127 public function getArguments() {
128 return $this->arguments;
129 }
130
131 /**
132 * @param array $defaults
133 */
134 public function setDefaults(array $defaults) {
135 $this->defaults = $defaults;
136 }
137
138 /**
139 * @return array
140 */
141 public function getDefaults() {
142 return $this->defaults;
143 }
144
145 /**
146 * @param string $argumentName
147 * @param mixed $argumentValue
148 */
149 public function addDefaultValue($argumentName, $argumentValue) {
150 if (is_bool($argumentValue)) {
151 $argumentValue = (int)$argumentValue;
152 }
153 $this->defaults[$argumentName] = $argumentValue;
154 }
155
156 /**
157 * Return a text representation of the selected command and arguments
158 *
159 * @return string Information to display
160 */
161 public function getAdditionalInformation() {
162 $label = $this->commandIdentifier;
163 if (count($this->arguments) > 0) {
164 $arguments = array();
165 foreach ($this->arguments as $argumentName => $argumentValue) {
166 if ($argumentValue != $this->defaults[$argumentName]) {
167 array_push($arguments, $argumentName . '=' . $argumentValue);
168 }
169 }
170 $label .= ' ' . implode(', ', $arguments);
171 }
172 return $label;
173 }
174
175 /**
176 * @param \Exception $e
177 */
178 protected function logException(\Exception $e) {
179 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($e->getMessage(), $this->commandIdentifier, 3);
180 }
181
182 }