Browse Source

HUE-7244 [presentation] Support cancelling the main execution

Cleaning of the execute all logic, so that it does not leak and
can be easily stopped.
Romain Rigaux 8 năm trước cách đây
mục cha
commit
09a8bfaa30

+ 29 - 14
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -1645,6 +1645,14 @@ var EditorViewModel = (function() {
                   }
                 }
               }
+              if (notebook.isExecutingAll()) {
+                notebook.executingAllIndex(notebook.executingAllIndex() + 1);
+                if (notebook.executingAllIndex() < notebook.snippets().length) {
+                  notebook.snippets()[notebook.executingAllIndex()].execute();
+                } else {
+                  notebook.isExecutingAll(false);
+                }
+              }
               if (! self.result.handle().has_more_statements && vm.successUrl()) {
                 window.location.href = vm.successUrl(); // Not used anymore in Hue 4
               }
@@ -1653,8 +1661,10 @@ var EditorViewModel = (function() {
             }
           } else if (data.status === -3) {
             self.status('expired');
+            notebook.isExecutingAll(false);
           } else {
             self._ajaxError(data);
+            notebook.isExecutingAll(false);
           }
         }
       }).fail(function (xhr, textStatus, errorThrown) {
@@ -1662,6 +1672,7 @@ var EditorViewModel = (function() {
           $(document).trigger("error", xhr.responseText || textStatus);
         }
         self.status('failed');
+        notebook.isExecutingAll(false);
       });
     };
 
@@ -1683,6 +1694,7 @@ var EditorViewModel = (function() {
         self.statusForButtons('canceled');
         self.status('failed');
         self.isCanceling(false);
+        notebook.isExecutingAll(false);
       } else {
         self.statusForButtons('canceling');
         $.post("/notebook/api/cancel_statement", {
@@ -1692,6 +1704,7 @@ var EditorViewModel = (function() {
           self.statusForButtons('canceled');
           if (data.status == 0) {
             self.status('canceled');
+            notebook.isExecutingAll(false);
           } else {
             self._ajaxError(data);
           }
@@ -1701,6 +1714,7 @@ var EditorViewModel = (function() {
           }
           self.statusForButtons('canceled');
           self.status('failed');
+          notebook.isExecutingAll(false);
         }).always(function (){
           self.isCanceling(false);
         });
@@ -2006,6 +2020,17 @@ var EditorViewModel = (function() {
         }).length == self.snippets().length;
     });
 
+    self.isExecutingAll = ko.observable(typeof notebook.isExecutingAll != "undefined" && notebook.isExecutingAll != null ? notebook.isExecutingAll : false);
+    self.cancelExecutingAll = function() {
+      var index = self.executingAllIndex();
+      if (self.snippets()[index]) {
+        self.snippets()[index].cancel();
+      } else {
+        self.isExecutingAll(false);
+      }
+    };
+    self.executingAllIndex = ko.observable(typeof notebook.executingAllIndex != "undefined" && notebook.executingAllIndex != null ? notebook.executingAllIndex : 0);
+
     self.retryModalConfirm = null;
     self.retryModalCancel = null;
 
@@ -2296,24 +2321,14 @@ var EditorViewModel = (function() {
     };
 
     self.executeAll = function () {
-      if (self.snippets().length < 1) {
+      if (self.isExecutingAll()) {
         return;
       }
 
-      var index = 0;
-      self.snippets()[index].execute();
-      var clock = window.setInterval(next, 100, 'editor');
+      self.isExecutingAll(true);
+      self.executingAllIndex(0);
 
-      function next() {
-        if (self.snippets()[index].status() == 'available' || self.snippets()[index].status() == 'failed') {
-          index = index + 1;
-          if (self.snippets().length > index) {
-            self.snippets()[index].execute();
-          } else {
-            window.clearInterval(clock);
-          }
-        }
-      }
+      self.snippets()[self.executingAllIndex()].execute();
     };
 
     self.saveDefaultUserProperties = function (session) {

+ 4 - 1
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -1157,9 +1157,12 @@ ${ sqlSyntaxDropdown.sqlSyntaxDropdown() }
 
 <script type="text/html" id="notebook-actions">
   <div class="btn-group">
-    <a class="btn" rel="tooltip" data-placement="bottom" title="${ _("Execute all") }" data-bind="click: function() { $root.selectedNotebook().executeAll() }">
+    <a class="btn" rel="tooltip" data-placement="bottom" title="${ _("Execute all") }" data-bind="visible: $root.selectedNotebook() && ! $root.selectedNotebook().isExecutingAll(), click: function() { $root.selectedNotebook().executeAll(); }">
       <i class="fa fa-fw fa-play"></i>
     </a>
+    <a class="btn red" rel="tooltip" data-placement="bottom" title="${ _("Stop all") }" data-bind="visible: $root.selectedNotebook() && $root.selectedNotebook().isExecutingAll(), click: function() { $root.selectedNotebook().cancelExecutingAll(); }">
+      <i class="fa fa-fw fa-stop"></i>
+    </a>
 
     <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
       <span class="caret"></span>