Improves feedback when executing cronjobs in the ACP
authorMatthias Schmidt <gravatronics@live.com>
Wed, 1 May 2013 07:29:34 +0000 (09:29 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Wed, 1 May 2013 07:29:34 +0000 (09:29 +0200)
wcfsetup/install/files/acp/js/WCF.ACP.js
wcfsetup/install/files/acp/templates/cronjobList.tpl
wcfsetup/install/files/lib/data/cronjob/CronjobAction.class.php

index 062f76f328db5e2ba395ac63072edbf64a9439db..81d70fade3069ec4877bd292f54615a065f49d93 100644 (file)
@@ -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.
  */
index 6e4ca7909844fd7d6eb3247c409b9d51fd6efb6c..8f14d8fd8c53b9fe0feebe3c26418fde260e416f 100644 (file)
        $(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();
        });
        //]]>
 </script>
index 99996f272f923b133b75c86c6f0ce16cba032012..ce5b2807a104d95fe869a1220422b0ba958e2b7c 100644 (file)
@@ -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;