background_operations_controller.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.BackgroundOperationsController = Em.Controller.extend({
  20. name: 'backgroundOperationsController',
  21. /**
  22. * Whether we need to refresh background operations or not
  23. */
  24. isWorking : false,
  25. allOperations: [],
  26. allOperationsCount : 0,
  27. executeTasks: [],
  28. getTasksByRole: function (role) {
  29. return this.get('allOperations').filterProperty('role', role);
  30. },
  31. getOperationsForRequestId: function(requestId){
  32. return this.get('allOperations').filterProperty('request_id', requestId);
  33. },
  34. updateInterval: App.bgOperationsUpdateInterval,
  35. url : '',
  36. generateUrl: function(){
  37. var url = App.testMode ?
  38. '/data/background_operations/list_on_start.json' :
  39. App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/requests/?fields=tasks/*&tasks/Tasks/status!=COMPLETED';
  40. this.set('url', url);
  41. return url;
  42. },
  43. timeoutId : null,
  44. /**
  45. * Background operations will not be working if receive <code>attemptsCount</code> response with errors
  46. */
  47. attemptsCount: 20,
  48. errorsCount: 0,
  49. /**
  50. * Call this.loadOperations with delay
  51. * @param delay time in milliseconds (updateInterval by default)
  52. * @param reason reason why we call it(used to calculate count of errors)
  53. */
  54. loadOperationsDelayed: function(delay, reason){
  55. delay = delay || this.get('updateInterval');
  56. var self = this;
  57. if(reason && reason.indexOf('error:clusterName:') === 0){
  58. var errors = this.get('errorsCount') + 1;
  59. this.set('errorsCount', errors);
  60. if(errors > this.get('attemptsCount')){
  61. console.log('Stop loading background operations: clusterName is undefined');
  62. return;
  63. }
  64. }
  65. this.set('timeoutId',
  66. setTimeout(function(){
  67. self.loadOperations();
  68. }, delay)
  69. );
  70. },
  71. /**
  72. * Reload operations
  73. * We can call it manually <code>controller.loadOperations();</code>
  74. * or it fires automatically, when <code>isWorking</code> becomes <code>true</code>
  75. */
  76. loadOperations : function(){
  77. var timeoutId = this.get('timeoutId');
  78. if(timeoutId){
  79. clearTimeout(timeoutId);
  80. this.set('timeoutId', null);
  81. }
  82. if(!this.get('isWorking')){
  83. return;
  84. }
  85. var self = this;
  86. if(!App.router.getClusterName()){
  87. this.loadOperationsDelayed(this.get('updateInterval')/2, 'error:clusterName');
  88. return;
  89. }
  90. var url = this.get('url');
  91. if(!url){
  92. url = this.generateUrl();
  93. }
  94. $.ajax({
  95. type: "GET",
  96. url: url,
  97. dataType: 'json',
  98. timeout: App.timeout,
  99. success: function (data) {
  100. //refresh model
  101. self.updateBackgroundOperations(data);
  102. self.loadOperationsDelayed();
  103. },
  104. error: function (request, ajaxOptions, error) {
  105. self.loadOperationsDelayed(null, 'error:response error');
  106. },
  107. statusCode: require('data/statusCodes')
  108. });
  109. }.observes('isWorking'),
  110. /**
  111. * Update info about background operations
  112. * Put all tasks with command 'EXECUTE' into <code>executeTasks</code>, other tasks with it they are still running put into <code>runningTasks</code>
  113. * Put all task that should be shown in popup modal window into <code>this.allOperations</code>
  114. * @param data json loaded from server
  115. */
  116. updateBackgroundOperations: function (data) {
  117. var runningTasks = [];
  118. var executeTasks = this.get('executeTasks');
  119. data.items.forEach(function (item) {
  120. item.tasks.forEach(function (task) {
  121. if (task.Tasks.command == 'EXECUTE') {
  122. if (!executeTasks.someProperty('id', task.Tasks.id)) {
  123. executeTasks.push(task.Tasks);
  124. }
  125. } else {
  126. if (task.Tasks.status == 'QUEUED' || task.Tasks.status == 'PENDING' || task.Tasks.status == 'IN_PROGRESS') {
  127. runningTasks.push(task.Tasks);
  128. }
  129. }
  130. });
  131. });
  132. for (var i = 0; i < executeTasks.length; i++) {
  133. if (executeTasks[i].status == 'QUEUED' || executeTasks[i].status == 'PENDING' || executeTasks[i].status == 'IN_PROGRESS') {
  134. var url = App.testMode ? '/data/background_operations/list_on_start.json' :
  135. App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/requests/' + executeTasks[i].request_id + '/tasks/' + executeTasks[i].id;
  136. var j = i;
  137. $.ajax({
  138. type: "GET",
  139. url: url,
  140. dataType: 'json',
  141. timeout: App.timeout,
  142. success: function (data) {
  143. if (data) {
  144. executeTasks[j] = data.Tasks;
  145. }
  146. },
  147. error: function () {
  148. console.log('ERROR: error during executeTask update');
  149. },
  150. statusCode: require('data/statusCodes')
  151. });
  152. }
  153. }
  154. ;
  155. var currentTasks;
  156. currentTasks = runningTasks.concat(executeTasks);
  157. currentTasks = currentTasks.sort(function (a, b) {
  158. return a.id - b.id;
  159. });
  160. this.get('allOperations').filterProperty('isOpen').mapProperty('id').forEach(function(id){
  161. if (currentTasks.someProperty('id', id)) {
  162. currentTasks.findProperty('id', id).isOpen = true;
  163. }
  164. });
  165. this.set('allOperations', currentTasks);
  166. this.set('allOperationsCount', runningTasks.length + executeTasks.filterProperty('status', 'PENDING').length + executeTasks.filterProperty('status', 'QUEUED').length + executeTasks.filterProperty('status', 'IN_PROGRESS').length);
  167. var eventsArray = this.get('eventsArray');
  168. if (eventsArray.length) {
  169. var itemsToRemove = [];
  170. eventsArray.forEach(function(item){
  171. //if when returns true
  172. if(item.when(this)){
  173. //fire do method
  174. item.do();
  175. //and remove it
  176. itemsToRemove.push(item);
  177. }
  178. }, this);
  179. itemsToRemove.forEach(function(item){
  180. eventsArray.splice(eventsArray.indexOf(item), 1);
  181. });
  182. }
  183. },
  184. /**
  185. * Onclick handler for background operations number located right to logo
  186. */
  187. showPopup: function(){
  188. this.set('executeTasks', []);
  189. this.loadOperations();
  190. App.ModalPopup.show({
  191. headerClass: Ember.View.extend({
  192. controllerBinding: 'App.router.backgroundOperationsController',
  193. template:Ember.Handlebars.compile('{{allOperationsCount}} Background Operations Running')
  194. }),
  195. bodyClass: Ember.View.extend({
  196. controllerBinding: 'App.router.backgroundOperationsController',
  197. templateName: require('templates/main/background_operations_popup')
  198. }),
  199. onPrimary: function() {
  200. this.hide();
  201. },
  202. secondary : null
  203. });
  204. },
  205. /**
  206. * Exaple of data inside:
  207. * {
  208. * when : function(backgroundOperationsController){
  209. * return backgroundOperationsController.getOperationsForRequestId(requestId).length == 0;
  210. * },
  211. * do : function(){
  212. * component.set('status', 'cool');
  213. * }
  214. * }
  215. *
  216. * Function <code>do</code> will be fired once, when <code>when</code> returns true.
  217. * Example, how to use it, you can see in app\controllers\main\host\details.js
  218. */
  219. eventsArray : []
  220. });