2 namespace TYPO3\CMS\Scheduler
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
18 * This class manages the logic of a particular execution of a task
20 * @author François Suter <francois@typo3.org>
21 * @author Christian Jul Jensen <julle@typo3.org>
22 * @author Markus Friedrich <markus.friedrich@dkd.de>
27 * Start date of a task (timestamp)
34 * End date of a task (timestamp)
41 * Interval between executions (in seconds)
48 * Flag for concurrent executions: TRUE if allowed, FALSE otherwise (default)
52 protected $multiple = FALSE;
55 * The cron command string of this task,
62 * This flag is used to mark a new single execution
63 * See explanations in method setIsNewSingleExecution()
66 * @see \TYPO3\CMS\Scheduler\Execution::setIsNewSingleExecution()
68 protected $isNewSingleExecution = FALSE;
70 /**********************************
72 **********************************/
74 * This method is used to set the start date
76 * @param int $start Start date (timestamp)
79 public function setStart($start) {
80 $this->start
= $start;
84 * This method is used to get the start date
86 * @return int Start date (timestamp)
88 public function getStart() {
93 * This method is used to set the end date
95 * @param int $end End date (timestamp)
98 public function setEnd($end) {
103 * This method is used to get the end date
105 * @return int End date (timestamp)
107 public function getEnd() {
112 * This method is used to set the interval
114 * @param int $interval Interval (in seconds)
117 public function setInterval($interval) {
118 $this->interval
= $interval;
122 * This method is used to get the interval
124 * @return int Interval (in seconds)
126 public function getInterval() {
127 return $this->interval
;
131 * This method is used to set the multiple execution flag
133 * @param bool $multiple TRUE if concurrent executions are allowed, FALSE otherwise
136 public function setMultiple($multiple) {
137 $this->multiple
= $multiple;
141 * This method is used to get the multiple execution flag
143 * @return bool TRUE if concurrent executions are allowed, FALSE otherwise
145 public function getMultiple() {
146 return $this->multiple
;
150 * Set the value of the cron command
152 * @param string $cmd Cron command, using cron-like syntax
155 public function setCronCmd($cmd) {
156 $this->cronCmd
= $cmd;
160 * Get the value of the cron command
162 * @return string Cron command, using cron-like syntax
164 public function getCronCmd() {
165 return $this->cronCmd
;
169 * Set whether this is a newly created single execution.
170 * This is necessary for the following reason: if a new single-running task
171 * is created and its start date is in the past (even for only a few seconds),
172 * the next run time calculation (which happens upon saving) will disable
173 * that task, because it was meant to run only once and is in the past.
174 * Setting this flag to TRUE preserves this task for a single run.
175 * Upon next execution, this flag is set to FALSE.
177 * @param bool $isNewSingleExecution Is newly created single execution?
179 * @see \TYPO3\CMS\Scheduler\Execution::getNextExecution()
181 public function setIsNewSingleExecution($isNewSingleExecution) {
182 $this->isNewSingleExecution
= $isNewSingleExecution;
186 * Get whether this is a newly created single execution
188 * @return bool Is newly created single execution?
190 public function getIsNewSingleExecution() {
191 return $this->isNewSingleExecution
;
194 /**********************************
195 * Execution calculations and logic
196 **********************************/
198 * This method gets or calculates the next execution date
200 * @return int Timestamp of the next execution
201 * @throws \OutOfBoundsException
203 public function getNextExecution() {
204 if ($this->getIsNewSingleExecution()) {
205 $this->setIsNewSingleExecution(FALSE);
208 if (!$this->isEnded()) {
209 // If the schedule has not yet run out, find out the next date
210 if (!$this->isStarted()) {
211 // If the schedule hasn't started yet, next date is start date
212 $date = $this->start
;
214 // If the schedule has already started, calculate next date
215 if ($this->cronCmd
) {
216 // If it uses cron-like syntax, calculate next date
217 $date = $this->getNextCronExecution();
218 } elseif ($this->interval
== 0) {
219 // If not and there's no interval either, it's a singe execution: use start date
220 $date = $this->start
;
222 // Otherwise calculate date based on interval
224 $date = $now +
$this->interval
- ($now - $this->start
) %
$this->interval
;
226 // If date is in the future, throw an exception
227 if (!empty($this->end
) && $date > $this->end
) {
228 throw new \
OutOfBoundsException('Next execution date is past end date.', 1250715528);
232 // The event has ended, throw an exception
233 throw new \
OutOfBoundsException('Task is past end date.', 1250715544);
239 * Calculates the next execution from a cron command
241 * @return int Next execution (timestamp)
243 public function getNextCronExecution() {
244 /** @var $cronCmd \TYPO3\CMS\Scheduler\CronCommand\CronCommand */
245 $cronCmd = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(\TYPO3\CMS\Scheduler\CronCommand\CronCommand
::class, $this->getCronCmd());
246 $cronCmd->calculateNextValue();
247 return $cronCmd->getTimestamp();
251 * Checks if the schedule for a task is started or not
253 * @return bool TRUE if the schedule is already active, FALSE otherwise
255 public function isStarted() {
256 return $this->start
< time();
260 * Checks if the schedule for a task is passed or not
262 * @return bool TRUE if the schedule is not active anymore, FALSE otherwise
264 public function isEnded() {
265 if (empty($this->end
)) {
266 // If no end is defined, the schedule never ends
269 // Otherwise check if end is in the past
270 $result = $this->end
< time();