Finished the open points in cronjobs
authorTim Düsterhus <timwolla@arcor.de>
Mon, 22 Aug 2011 14:16:17 +0000 (16:16 +0200)
committerTim Düsterhus <timwolla@arcor.de>
Mon, 22 Aug 2011 14:38:59 +0000 (16:38 +0200)
- Log the result
- Calculate afterNextExec
- Care about the state

wcfsetup/install/files/lib/data/cronjob/CronjobAction.class.php

index 4816dff890b5511312181cd49b6828e86e052ca5..c351702843a8e7f9a30c826cab61e299d9e3fc8d 100644 (file)
@@ -7,7 +7,7 @@ use wcf\util\DateUtil;
 /**
  * Executes cronjob-related actions.
  * 
- * @author     Alexander Ebert
+ * @author     Tim Düsterhus, Alexander Ebert
  * @copyright  2001-2011 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
@@ -95,19 +95,69 @@ class CronjobAction extends AbstractDatabaseObjectAction {
         * Executes cronjobs.
         */
        public function execute() {
-               $return = array();
+               $cronjobs = $return = array();
+               
+               foreach ($this->objects as $key => $cronjob) {
+                       // skip jobs that are already being processed
+                       if ($cronjob->state == Cronjob::PENDING || $cronjob->state == Cronjob::EXECUTING) {
+                               unset($this->objects[$key]);
+                               continue;
+                       }
+                       
+                       // mark them as pending
+                       $cronjob->update(array('state' => Cronjob::PENDING));
+               }
+               
                foreach ($this->objects as $cronjob) {
+                       // it now time for executing
+                       $cronjob->update(array('state' => Cronjob::EXECUTING));
                        $className = $cronjob->className;
                        $executable = new $className();
-                       $executable->execute(new Cronjob($cronjob->cronjobID));
+                       
+                       // execute cronjob
+                       $success = true;
+                       $error = '';
+                       try {
+                               $executable->execute(new Cronjob($cronjob->cronjobID));
+                       }
+                       catch (\Exception $e) {
+                               $success = false;
+                               $error = (string) $e;
+                       }
+                       
+                       log\CronjobLogEditor::create(array(
+                               'cronjobID' => $cronjob->cronjobID,
+                               'execTime' => TIME_NOW,
+                               'success' => (int) $success,
+                               'error' => $error
+                       ));
+                               
+                       // calculate next exec-time
                        $nextExec = $cronjob->getNextExec();
-                       $cronjob->update(array('nextExec' => $nextExec));
+                       $cronjob->update(array(
+                               'nextExec' => $nextExec, 
+                               'afterNextExec' => $cronjob->getNextExec($nextExec)
+                       ));
+                       
+                       // build the return value
                        $dateTime = DateUtil::getDateTimeByTimestamp($nextExec);
                        $return[$cronjob->cronjobID] = array(
                                'time' => $nextExec,
-                               'formatted' => str_replace('%time%', DateUtil::format($dateTime, DateUtil::TIME_FORMAT), str_replace('%date%', DateUtil::format($dateTime, DateUtil::DATE_FORMAT), WCF::getLanguage()->get('wcf.global.date.dateTimeFormat')))
+                               'formatted' => str_replace(
+                                       '%time%', 
+                                       DateUtil::format($dateTime, DateUtil::TIME_FORMAT), 
+                                       str_replace(
+                                               '%date%', 
+                                               DateUtil::format($dateTime, DateUtil::DATE_FORMAT), 
+                                               WCF::getLanguage()->get('wcf.global.date.dateTimeFormat')
+                                       )
+                               )
                        );
+                       
+                       // we are finished
+                       $cronjob->update(array('state' => Cronjob::READY));
                }
+               
                return $return;
        }
 }