From: Matthias Schmidt Date: Wed, 1 May 2013 07:29:34 +0000 (+0200) Subject: Improves feedback when executing cronjobs in the ACP X-Git-Tag: 2.0.0_Beta_1~269^2~3 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=02b148adc31895668879374b3545b05f3837bd84;p=GitHub%2FWoltLab%2FWCF.git Improves feedback when executing cronjobs in the ACP --- diff --git a/wcfsetup/install/files/acp/js/WCF.ACP.js b/wcfsetup/install/files/acp/js/WCF.ACP.js index 062f76f328..81d70fade3 100644 --- a/wcfsetup/install/files/acp/js/WCF.ACP.js +++ b/wcfsetup/install/files/acp/js/WCF.ACP.js @@ -82,6 +82,77 @@ WCF.ACP.Application.SetAsPrimary = Class.extend({ */ WCF.ACP.Cronjob = { }; +/** + * Handles the manual execution of cronjobs. + */ +WCF.ACP.Cronjob.ExecutionHandler = Class.extend({ + /** + * notification object + * @var WCF.System.Notification + */ + _notification: null, + + /** + * action proxy + * @var WCF.Action.Proxy + */ + _proxy: null, + + /** + * Initializes WCF.ACP.Cronjob.ExecutionHandler object. + */ + init: function() { + this._proxy = new WCF.Action.Proxy({ + success: $.proxy(this._success, this) + }); + + $('.jsCronjobRow .jsExecuteButton').click($.proxy(this._click, this)); + + this._notification = new WCF.System.Notification(WCF.Language.get('wcf.global.success'), 'success'); + }, + + /** + * Handles a click on an execute button. + * + * @param object event + */ + _click: function(event) { + this._proxy.setOption('data', { + actionName: 'execute', + className: 'wcf\\data\\cronjob\\CronjobAction', + objectIDs: [ $(event.target).data('objectID') ] + }); + + this._proxy.sendRequest(); + }, + + /** + * Handles successful cronjob execution. + * + * @param object data + * @param string textStatus + * @param jQuery jqXHR + */ + _success: function(data, textStatus, jqXHR) { + $('.jsCronjobRow').each($.proxy(function(index, row) { + var $button = $(row).find('.jsExecuteButton'); + var $objectID = ($button).data('objectID'); + + if (WCF.inArray($objectID, data.objectIDs)) { + if (data.returnValues[$objectID]) { + // insert feedback here + $(row).find('td.columnNextExec').html(data.returnValues[$objectID].formatted); + $(row).wcfHighlight(); + } + + this._notification.show(); + + return false; + } + }, this)); + } +}); + /** * Handles the cronjob log list. */ diff --git a/wcfsetup/install/files/acp/templates/cronjobList.tpl b/wcfsetup/install/files/acp/templates/cronjobList.tpl index 6e4ca79098..8f14d8fd8c 100644 --- a/wcfsetup/install/files/acp/templates/cronjobList.tpl +++ b/wcfsetup/install/files/acp/templates/cronjobList.tpl @@ -10,24 +10,8 @@ $(function() { new WCF.Action.Delete('wcf\\data\\cronjob\\CronjobAction', '.jsCronjobRow'); new WCF.Action.Toggle('wcf\\data\\cronjob\\CronjobAction', '.jsCronjobRow'); - new WCF.Action.SimpleProxy({ - action: 'execute', - className: 'wcf\\data\\cronjob\\CronjobAction', - elements: $('.jsCronjobRow .jsExecuteButton') - }, { - success: function(data, statusText, jqXHR) { - $('.jsCronjobRow').each(function(index, row) { - var $button = $(row).find('.jsExecuteButton'); - var $objectID = ($button).data('objectID'); - - if (WCF.inArray($objectID, data.objectIDs) && data.returnValues[$objectID]) { - // insert feedback here - $(row).find('td.columnNextExec').html(data.returnValues[$objectID].formatted); - $(row).wcfHighlight(); - } - }); - } - }); + + new WCF.ACP.Cronjob.ExecutionHandler(); }); //]]> diff --git a/wcfsetup/install/files/lib/data/cronjob/CronjobAction.class.php b/wcfsetup/install/files/lib/data/cronjob/CronjobAction.class.php index 99996f272f..ce5b2807a1 100644 --- a/wcfsetup/install/files/lib/data/cronjob/CronjobAction.class.php +++ b/wcfsetup/install/files/lib/data/cronjob/CronjobAction.class.php @@ -125,19 +125,17 @@ class CronjobAction extends AbstractDatabaseObjectAction implements IToggleActio $executable = new $className(); // execute cronjob - $error = ''; + $exception = null; try { $executable->execute(new Cronjob($cronjob->cronjobID)); } - catch (\Exception $e) { - $error = $e->getMessage(); - } + catch (\Exception $exception) { } CronjobLogEditor::create(array( 'cronjobID' => $cronjob->cronjobID, 'execTime' => TIME_NOW, - 'success' => ($error == '' ? 1 : 0), - 'error' => $error + 'success' => ($exception ? 0 : 1), + 'error' => ($exception ? $exception->getMessage() : '') )); // calculate next exec-time @@ -148,18 +146,8 @@ class CronjobAction extends AbstractDatabaseObjectAction implements IToggleActio 'afterNextExec' => $cronjob->getNextExec(($nextExec + 120)) ); - // if no error: reset fail counter - if ($error == '') { - $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 failed - else { + if ($exception) { if ($cronjob->failCount < Cronjob::MAX_FAIL_COUNT) { $data['failCount'] = $cronjob->failCount + 1; } @@ -169,26 +157,43 @@ class CronjobAction extends AbstractDatabaseObjectAction implements IToggleActio $data['isDisabled'] = 1; } } + // 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 - $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 ($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; + } } return $return;