public function execute() {
$return = array();
- foreach ($this->objects as $key => $cronjob) {
- // mark them as pending
- $cronjob->update(array('state' => Cronjob::PENDING));
- }
+ // switch session owner to 'system' during execution of cronjobs
+ $actualUser = WCF::getUser();
+ WCF::getSession()->changeUser(new User(null, array('userID' => 0, 'username' => 'System')), true);
+ WCF::getSession()->disableUpdate();
- foreach ($this->objects as $cronjob) {
- // it now time for executing
- $cronjob->update(array('state' => Cronjob::EXECUTING));
- $className = $cronjob->className;
- $executable = new $className();
-
- // execute cronjob
- $exception = null;
- try {
- $executable->execute(new Cronjob($cronjob->cronjobID));
+ try {
+ foreach ($this->objects as $key => $cronjob) {
+ // mark them as pending
+ $cronjob->update(array('state' => Cronjob::PENDING));
}
- catch (\Exception $exception) { }
-
- CronjobLogEditor::create(array(
- 'cronjobID' => $cronjob->cronjobID,
- 'execTime' => TIME_NOW,
- 'success' => ($exception ? 0 : 1),
- 'error' => ($exception ? $exception->getMessage() : '')
- ));
-
- // calculate next exec-time
- $nextExec = $cronjob->getNextExec();
- $data = array(
- 'lastExec' => TIME_NOW,
- 'nextExec' => $nextExec,
- 'afterNextExec' => $cronjob->getNextExec(($nextExec + 120))
- );
- // cronjob failed
- if ($exception) {
- if ($cronjob->failCount < Cronjob::MAX_FAIL_COUNT) {
- $data['failCount'] = $cronjob->failCount + 1;
- }
+ foreach ($this->objects as $cronjob) {
+ // it now time for executing
+ $cronjob->update(array('state' => Cronjob::EXECUTING));
+ $className = $cronjob->className;
+ $executable = new $className();
- // cronjob failed too often: disable it
- if ($cronjob->failCount + 1 == Cronjob::MAX_FAIL_COUNT) {
- $data['isDisabled'] = 1;
+ // execute cronjob
+ $exception = null;
+ try {
+ $executable->execute(new Cronjob($cronjob->cronjobID));
}
- }
- // if no error: reset fail counter
- else {
- $data['failCount'] = 0;
+ catch (\Exception $exception) { }
+
+ CronjobLogEditor::create(array(
+ 'cronjobID' => $cronjob->cronjobID,
+ 'execTime' => TIME_NOW,
+ 'success' => ($exception ? 0 : 1),
+ 'error' => ($exception ? $exception->getMessage() : '')
+ ));
+
+ // calculate next exec-time
+ $nextExec = $cronjob->getNextExec();
+ $data = array(
+ 'lastExec' => TIME_NOW,
+ 'nextExec' => $nextExec,
+ 'afterNextExec' => $cronjob->getNextExec(($nextExec + 120))
+ );
- // if cronjob has been disabled because of too many
- // failed executions, enable it again
- if ($cronjob->failCount == Cronjob::MAX_FAIL_COUNT && $cronjob->isDisabled) {
- $data['isDisabled'] = 0;
+ // cronjob failed
+ if ($exception) {
+ if ($cronjob->failCount < Cronjob::MAX_FAIL_COUNT) {
+ $data['failCount'] = $cronjob->failCount + 1;
+ }
+
+ // cronjob failed too often: disable it
+ if ($cronjob->failCount + 1 == Cronjob::MAX_FAIL_COUNT) {
+ $data['isDisabled'] = 1;
+ }
}
- }
-
- $cronjob->update($data);
-
- // build the return value
- if ($exception === null && !$cronjob->isDisabled) {
- $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.date.dateTimeFormat')
+ // if no error: reset fail counter
+ else {
+ $data['failCount'] = 0;
+
+ // if cronjob has been disabled because of too many
+ // failed executions, enable it again
+ if ($cronjob->failCount == Cronjob::MAX_FAIL_COUNT && $cronjob->isDisabled) {
+ $data['isDisabled'] = 0;
+ }
+ }
+
+ $cronjob->update($data);
+
+ // build the return value
+ if ($exception === null && !$cronjob->isDisabled) {
+ $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.date.dateTimeFormat')
+ )
)
- )
- );
- }
-
- // we are finished
- $cronjob->update(array('state' => Cronjob::READY));
-
- // throw exception again to show error message
- if ($exception) {
- throw $exception;
+ );
+ }
+
+ // we are finished
+ $cronjob->update(array('state' => Cronjob::READY));
+
+ // throw exception again to show error message
+ if ($exception) {
+ throw $exception;
+ }
}
+ }
+ finally {
+ // switch session back to the actual user
+ WCF::getSession()->changeUser($actualUser, true);
}
return $return;