TYPO3 API  SVNRelease
class.tx_scheduler_execution.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2005 Christian Jul Jensen (julle@typo3.org)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 
00026 /**
00027  * This class manages the logic of a particular execution of a task
00028  *
00029  * @author  François Suter <francois@typo3.org>
00030  * @author  Christian Jul Jensen <julle@typo3.org>
00031  * @author  Markus Friedrich <markus.friedrich@dkd.de>
00032  *
00033  * @package     TYPO3
00034  * @subpackage  tx_scheduler
00035  *
00036  * $Id: class.tx_scheduler_execution.php 9901 2010-12-26 20:35:39Z lolli $
00037  */
00038 class tx_scheduler_Execution {
00039 
00040     /**
00041      * Start date of a task (timestamp)
00042      *
00043      * @var integer $start
00044      */
00045     protected $start;
00046 
00047     /**
00048      * End date of a task (timestamp)
00049      *
00050      * @var integer $end
00051      */
00052     protected $end;
00053 
00054     /**
00055      * Interval between executions (in seconds)
00056      *
00057      * @var integer $interval
00058      */
00059     protected $interval;
00060 
00061     /**
00062      * Flag for concurrent executions: true if allowed, false otherwise (default)
00063      *
00064      * @var boolean $multiple
00065      */
00066     protected $multiple = FALSE;
00067 
00068     /**
00069      * The cron command string of this task,
00070      *
00071      * @var string      $cronCmd
00072      */
00073     protected $cronCmd;
00074 
00075     /**
00076      * This flag is used to mark a new single execution
00077      * See explanations in method setIsNewSingleExecution()
00078      *
00079      * @var boolean     $isNewSingleExecution
00080      * @see tx_scheduler_Execution::setIsNewSingleExecution()
00081      */
00082     protected $isNewSingleExecution = FALSE;
00083 
00084 
00085     /**********************************
00086      * Setters and getters
00087      **********************************/
00088 
00089     /**
00090      * This method is used to set the start date
00091      *
00092      * @param   integer     $start: start date (timestamp)
00093      * @return  void
00094      */
00095     public function setStart($start) {
00096         $this->start = $start;
00097     }
00098 
00099     /**
00100      * This method is used to get the start date
00101      *
00102      * @return  integer     start date (timestamp)
00103      */
00104     public function getStart() {
00105         return $this->start;
00106     }
00107 
00108     /**
00109      * This method is used to set the end date
00110      *
00111      * @param   integer     $end: end date (timestamp)
00112      * @return  void
00113      */
00114     public function setEnd($end) {
00115         $this->end = $end;
00116     }
00117 
00118     /**
00119      * This method is used to get the end date
00120      *
00121      * @return  integer     end date (timestamp)
00122      */
00123     public function getEnd() {
00124         return $this->end;
00125     }
00126 
00127     /**
00128      * This method is used to set the interval
00129      *
00130      * @param   integer     $interval: interval (in seconds)
00131      * @return  void
00132      */
00133     public function setInterval($interval) {
00134         $this->interval = $interval;
00135     }
00136 
00137     /**
00138      * This method is used to get the interval
00139      *
00140      * @return  integer     interval (in seconds)
00141      */
00142     public function getInterval() {
00143         return $this->interval;
00144     }
00145 
00146     /**
00147      * This method is used to set the multiple execution flag
00148      *
00149      * @param   boolean     $multiple: true if concurrent executions are allowed, false otherwise
00150      * @return  void
00151      */
00152     public function setMultiple($multiple) {
00153         $this->multiple = $multiple;
00154     }
00155 
00156     /**
00157      * This method is used to get the multiple execution flag
00158      *
00159      * @return  boolean     true if concurrent executions are allowed, false otherwise
00160      */
00161     public function getMultiple() {
00162         return $this->multiple;
00163     }
00164 
00165     /**
00166      * Set the value of the cron command
00167      *
00168      * @param   string      $cmd: cron command, using cron-like syntax
00169      * @return  void
00170      */
00171     public function setCronCmd($cmd) {
00172         $this->cronCmd = $cmd;
00173     }
00174 
00175     /**
00176      * Get the value of the cron command
00177      *
00178      * @return  string      cron command, using cron-like syntax
00179      */
00180     public function getCronCmd() {
00181         return $this->cronCmd;
00182     }
00183 
00184     /**
00185      * Set whether this is a newly created single execution.
00186      * This is necessary for the following reason: if a new single-running task
00187      * is created and its start date is in the past (even for only a few seconds),
00188      * the next run time calculation (which happens upon saving) will disable
00189      * that task, because it was meant to run only once and is in the past.
00190      * Setting this flag to true preserves this task for a single run.
00191      * Upon next execution, this flag is set to false.
00192      *
00193      * @param   boolean     Is newly created single execution?
00194      * @return  void
00195      * @see tx_scheduler_Execution::getNextExecution()
00196      */
00197     public function setIsNewSingleExecution($isNewSingleExecution) {
00198         $this->isNewSingleExecution = $isNewSingleExecution;
00199     }
00200 
00201     /**
00202      * Get whether this is a newly created single execution
00203      *
00204      * @return  boolean     Is newly created single execution?
00205      */
00206     public function getIsNewSingleExecution() {
00207         return $this->isNewSingleExecution;
00208     }
00209 
00210     /**********************************
00211      * Execution calculations and logic
00212      **********************************/
00213 
00214     /**
00215      * This method gets or calculates the next execution date
00216      *
00217      * @return  integer     Timestamp of the next execution
00218      */
00219     public function getNextExecution() {
00220 
00221         if ($this->getIsNewSingleExecution()) {
00222             $this->setIsNewSingleExecution(FALSE);
00223             return $this->start;
00224         }
00225 
00226         if (!$this->isEnded()) {
00227                 // If the schedule has not yet run out, find out the next date
00228 
00229             if (!$this->isStarted()) {
00230                     // If the schedule hasn't started yet, next date is start date
00231                 $date = $this->start;
00232             } else {
00233                     // If the schedule has already started, calculate next date
00234 
00235                 if ($this->cronCmd) {
00236                         // If it uses cron-like syntax, calculate next date
00237                     $date = $this->getNextCronExecution();
00238                 } else if ($this->interval == 0) {
00239                         // If not and there's no interval either, it's a singe execution: use start date
00240                     $date = $this->start;
00241                 } else {
00242                         // Otherwise calculate date based on interval
00243                     $now = time();
00244                     $date = $now + $this->interval - (($now - $this->start) % $this->interval);
00245                 }
00246                     // If date is in the future, throw an exception
00247                 if (!empty($this->end) && $date > $this->end) {
00248                     throw new OutOfBoundsException('Next execution date is past end date.', 1250715528);
00249                 }
00250             }
00251         } else {
00252                 // The event has ended, throw an exception
00253             throw new OutOfBoundsException('Task is past end date.', 1250715544);
00254         }
00255 
00256         return $date;
00257     }
00258 
00259     /**
00260      * Calulates the next execution from a cron command
00261      *
00262      * @return  integer     Next execution (timestamp)
00263      */
00264     public function getNextCronExecution() {
00265         $cronCmd = t3lib_div::makeInstance('tx_scheduler_CronCmd', $this->getCronCmd());
00266         $cronCmd->calculateNextValue();
00267 
00268         return $cronCmd->getTimestamp();
00269     }
00270 
00271     /**
00272      * Checks if the schedule for a task is started or not
00273      *
00274      * @return  boolean     True if the schedule is already active, false otherwise
00275      */
00276     public function isStarted() {
00277         return $this->start < time();
00278     }
00279 
00280     /**
00281      * Checks if the schedule for a task is passed or not
00282      *
00283      * @return  boolean     True if the schedule is not active anymore, false otherwise
00284      */
00285     public function isEnded() {
00286         if (empty($this->end)) {
00287                 // If no end is defined, the schedule never ends
00288             $result = FALSE;
00289         } else {
00290                 // Otherwise check if end is in the past
00291             $result = $this->end < time();
00292         }
00293 
00294         return $result;
00295     }
00296 }
00297 
00298 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/class.tx_scheduler_execution.php'])) {
00299     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/class.tx_scheduler_execution.php']);
00300 }
00301 
00302 ?>