Feature #11555: Integrated extension "gabriel" into TYPO3 Core as sysext "scheduler"
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / class.tx_scheduler_execution.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005 Christian Jul Jensen (julle@typo3.org)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * This class manages the logic of a particular execution of a task
28 *
29 * @author Francois Suter <francois@typo3.org>
30 * @author Christian Jul Jensen <julle@typo3.org>
31 * @author Markus Friedrich <markus.friedrich@dkd.de>
32 *
33 * @package TYPO3
34 * @subpackage tx_scheduler
35 *
36 * $Id: class.tx_scheduler_execution.php 1262 2009-09-15 21:04:22Z francois $
37 */
38 class tx_scheduler_Execution {
39
40 /**
41 * Start date of a task (timestamp)
42 *
43 * @var integer $start
44 */
45 protected $start;
46
47 /**
48 * End date of a task (timestamp)
49 *
50 * @var integer $end
51 */
52 protected $end;
53
54 /**
55 * Interval between executions (in seconds)
56 *
57 * @var integer $interval
58 */
59 protected $interval;
60
61 /**
62 * Flag for concurrent executions: true if allowed, false otherwise (default)
63 *
64 * @var boolean $multiple
65 */
66 protected $multiple = false;
67
68 /**
69 * The cron command string of this task,
70 *
71 * @var string $cronCmd
72 */
73 protected $cronCmd;
74
75
76 /**********************************
77 * Setters and getters
78 **********************************/
79
80 /**
81 * This method is used to set the start date
82 *
83 * @param integer $start: start date (timestamp)
84 * @return void
85 */
86 public function setStart($start) {
87 $this->start = $start;
88 }
89
90 /**
91 * This method is used to get the start date
92 *
93 * @return integer start date (timestamp)
94 */
95 public function getStart() {
96 return $this->start;
97 }
98
99 /**
100 * This method is used to set the end date
101 *
102 * @param integer $end: end date (timestamp)
103 * @return void
104 */
105 public function setEnd($end) {
106 $this->end = $end;
107 }
108
109 /**
110 * This method is used to get the end date
111 *
112 * @return integer end date (timestamp)
113 */
114 public function getEnd() {
115 return $this->end;
116 }
117
118 /**
119 * This method is used to set the interval
120 *
121 * @param integer $interval: interval (in seconds)
122 * @return void
123 */
124 public function setInterval($interval) {
125 $this->interval = $interval;
126 }
127
128 /**
129 * This method is used to get the interval
130 *
131 * @return integer interval (in seconds)
132 */
133 public function getInterval() {
134 return $this->interval;
135 }
136
137 /**
138 * This method is used to set the multiple execution flag
139 *
140 * @param boolean $multiple: true if concurrent executions are allowed, false otherwise
141 * @return void
142 */
143 public function setMultiple($multiple) {
144 $this->multiple = $multiple;
145 }
146
147 /**
148 * This method is used to get the multiple execution flag
149 *
150 * @return boolean true if concurrent executions are allowed, false otherwise
151 */
152 public function getMultiple() {
153 return $this->multiple;
154 }
155
156 /**
157 * Set the value of the cron command
158 *
159 * @param string $cmd: cron command, using cron-like syntax
160 * @return void
161 */
162 public function setCronCmd($cmd) {
163 $this->cronCmd = $cmd;
164 }
165
166 /**
167 * Get the value of the cron command
168 *
169 * @return string cron command, using cron-like syntax
170 */
171 public function getCronCmd() {
172 return $this->cronCmd;
173 }
174
175 /**********************************
176 * Execution calculations and logic
177 **********************************/
178
179 /**
180 * This method gets or calculates the next execution date
181 *
182 * @return integer Timestamp of the next execution
183 */
184 public function getNextExecution() {
185
186 if (!$this->isEnded()) {
187 // If the schedule has not yet run out, find out the next date
188
189 if (!$this->isStarted()) {
190 // If the schedule hasn't started yet, next date is start date
191 $date = $this->start;
192 } else {
193 // If the schedule has already started, calculate next date
194
195 if ($this->cronCmd) {
196 // If it uses cron-like syntax, calculate next date
197 $date = $this->getNextCronExecution();
198 } else if ($this->interval == 0) {
199 // If not and there's no interval either, it's a singe execution: use start date
200 $date = $this->start;
201 } else {
202 // Otherwise calculate date based on interval
203 $now = time();
204 $date = $now + $this->interval - (($now - $this->start) % $this->interval);
205 }
206 // If date is in the future, throw an exception
207 if (!empty($this->end) && $date > $this->end) {
208 throw new OutOfBoundsException('Next execution date is past end date.', 1250715528);
209 }
210 }
211 } else {
212 // The event has ended, throw an exception
213 throw new OutOfBoundsException('Task is past end date.', 1250715544);
214 }
215
216 return $date;
217 }
218
219 /**
220 * Calulates the next execution from a cron command
221 *
222 * @return integer Next execution (timestamp)
223 */
224 public function getNextCronExecution() {
225 $cronCmd = t3lib_div::makeInstance('tx_scheduler_CronCmd', $this->getCronCmd());
226 $cronCmd->calculateNextValue(0);
227
228 return $cronCmd->getTstamp();
229 }
230
231 /**
232 * Checks if the schedule for a task is started or not
233 *
234 * @return boolean True if the schedule is already active, false otherwise
235 */
236 public function isStarted() {
237 return $this->start < time();
238 }
239
240 /**
241 * Checks if the schedule for a task is passed or not
242 *
243 * @return boolean True if the schedule is not active anymore, false otherwise
244 */
245 public function isEnded() {
246 if (empty($this->end)) {
247 // If no end is defined, the schedule never ends
248 $result = false;
249 } else {
250 // Otherwise check if end is in the past
251 $result = $this->end < time();
252 }
253
254 return $result;
255 }
256 }
257
258 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/scheduler/class.tx_scheduler_execution.php']) {
259 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/scheduler/class.tx_scheduler_execution.php']);
260 }
261
262 ?>