Blame SOURCES/bz1189857-03-web-UI-prevents-running-update-multiple-times-at-onc.patch

71541a
From 032a2571656c646f17bb3453b6a7d4883241ad46 Mon Sep 17 00:00:00 2001
71541a
From: Ondrej Mular <omular@redhat.com>
71541a
Date: Tue, 1 Sep 2015 12:06:20 +0200
71541a
Subject: [PATCH] web UI: prevents running update multiple times at once
71541a
71541a
---
71541a
 pcsd/public/js/nodes-ember.js | 106 ++++++++++++++++++++++++++++++++++++------
71541a
 1 file changed, 91 insertions(+), 15 deletions(-)
71541a
71541a
diff --git a/pcsd/public/js/nodes-ember.js b/pcsd/public/js/nodes-ember.js
71541a
index 172c00a..d2f85bd 100644
71541a
--- a/pcsd/public/js/nodes-ember.js
71541a
+++ b/pcsd/public/js/nodes-ember.js
71541a
@@ -49,22 +49,25 @@ Pcs = Ember.Application.createWithMixins({
71541a
     });
71541a
     return retArray;
71541a
   },
71541a
-  update_timeout: null,
71541a
-  update: function(first_run) {
71541a
+  updater: null,
71541a
+
71541a
+  update: function() {
71541a
+    Pcs.get('updater').update();
71541a
+  },
71541a
+
71541a
+  _update: function(first_run) {
71541a
     if (window.location.pathname.lastIndexOf('/manage', 0) !== 0) {
71541a
       return;
71541a
     }
71541a
-    clearTimeout(Pcs.get('update_timeout'));
71541a
-    Pcs.set('update_timeout', null);
71541a
+    if (first_run) {
71541a
+      show_loading_screen();
71541a
+    }
71541a
     var self = Pcs;
71541a
     var cluster_name = self.cluster_name;
71541a
     if (cluster_name == null) {
71541a
       if (location.pathname.indexOf("/manage") != 0) {
71541a
         return;
71541a
       }
71541a
-      if (first_run) {
71541a
-        show_loading_screen();
71541a
-      }
71541a
       Ember.debug("Empty Cluster Name");
71541a
       $.ajax({
71541a
         url: "/clusters_overview",
71541a
@@ -77,8 +80,6 @@ Pcs = Ember.Application.createWithMixins({
71541a
           });
71541a
           if (data["not_current_data"]) {
71541a
             self.update();
71541a
-          } else {
71541a
-            Pcs.set('update_timeout', window.setTimeout(self.update,20000));
71541a
           }
71541a
           hide_loading_screen();
71541a
         },
71541a
@@ -93,15 +94,14 @@ Pcs = Ember.Application.createWithMixins({
71541a
               console.log("Error: Unable to parse json for clusters_overview");
71541a
             }
71541a
           }
71541a
-          Pcs.set('update_timeout', window.setTimeout(self.update,20000));
71541a
           hide_loading_screen();
71541a
+        },
71541a
+        complete: function() {
71541a
+          Pcs.get('updater').update_finished();
71541a
         }
71541a
       });
71541a
       return;
71541a
     }
71541a
-    if (first_run) {
71541a
-      show_loading_screen();
71541a
-    }
71541a
     $.ajax({
71541a
       url: "cluster_status",
71541a
       dataType: "json",
71541a
@@ -191,12 +191,84 @@ Pcs = Ember.Application.createWithMixins({
71541a
       },
71541a
       complete: function() {
71541a
         hide_loading_screen();
71541a
-        Pcs.update_timeout = window.setTimeout(Pcs.update,20000);
71541a
+        Pcs.get('updater').update_finished();
71541a
       }
71541a
     });
71541a
   }
71541a
 });
71541a
 
71541a
+Pcs.Updater = Ember.Object.extend({
71541a
+  timeout: 20000,
71541a
+  first_run: true,
71541a
+  async: true,
71541a
+  autostart: true,
71541a
+  started: false,
71541a
+  in_progress: false,
71541a
+  waiting: false,
71541a
+  update_function: null,
71541a
+  update_target: null,
71541a
+  timer: null,
71541a
+
71541a
+  start: function() {
71541a
+    this.set('started', true);
71541a
+    this.update();
71541a
+  },
71541a
+
71541a
+  stop: function() {
71541a
+    this.set('started', false);
71541a
+    this.cancel_timer();
71541a
+  },
71541a
+
71541a
+  cancel_timer: function() {
71541a
+    var self = this;
71541a
+    var timer = self.get('timer');
71541a
+    if (timer) {
71541a
+      self.set('timer', null);
71541a
+      Ember.run.cancel(timer);
71541a
+    }
71541a
+  },
71541a
+
71541a
+  update: function() {
71541a
+    var self = this;
71541a
+    if (!self.get('update_function')) {
71541a
+      console.log('No update_function defined!');
71541a
+      return;
71541a
+    }
71541a
+    self.cancel_timer();
71541a
+    self.set('waiting', false);
71541a
+    if (self.get('in_progress')) {
71541a
+      self.set('waiting', true);
71541a
+    } else {
71541a
+      self.set('in_progress', true);
71541a
+      self.get('update_function').apply(self.get('update_target'), [self.get('first_run')]);
71541a
+      self.set('first_run', false);
71541a
+      if (!self.get('async')) {
71541a
+        self.update_finished();
71541a
+      }
71541a
+    }
71541a
+  },
71541a
+
71541a
+  update_finished: function() {
71541a
+    var self = this;
71541a
+    if (self.get('waiting')) {
71541a
+      Ember.run.next(self, self.update);
71541a
+    } else if (self.get('started')) {
71541a
+      self.set('timer', Ember.run.later(self, self.update, self.get('timeout')));
71541a
+    }
71541a
+    self.set('in_progress', false);
71541a
+  },
71541a
+
71541a
+  init: function() {
71541a
+    var self = this;
71541a
+    if (!self.get('update_target')) {
71541a
+      self.set('update_target', self);
71541a
+    }
71541a
+    if (self.get('autostart')) {
71541a
+      self.start();
71541a
+    }
71541a
+  }
71541a
+});
71541a
+
71541a
 Pcs.resourcesContainer = Ember.Object.create({
71541a
   resource_map: {},
71541a
   top_level_resource_map: {},
71541a
@@ -1742,4 +1814,8 @@ function myUpdate() {
71541a
 //  window.setTimeout(myUpdate,4000);
71541a
 }
71541a
 
71541a
-Pcs.update(true);
71541a
+Pcs.set('updater', Pcs.Updater.create({
71541a
+  timeout: 20000,
71541a
+  update_function: Pcs._update,
71541a
+  update_target: Pcs
71541a
+}));
71541a
-- 
71541a
1.9.1
71541a