background_operations_controller.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. timeoutId : null,
  37. /**
  38. * Background operations will not be working if receive <code>attemptsCount</code> response with errors
  39. */
  40. attemptsCount: 20,
  41. errorsCount: 0,
  42. /**
  43. * Call this.loadOperations with delay
  44. * @param delay time in milliseconds (updateInterval by default)
  45. * @param reason reason why we call it(used to calculate count of errors)
  46. */
  47. loadOperationsDelayed: function(delay, reason){
  48. delay = delay || this.get('updateInterval');
  49. var self = this;
  50. if(reason && reason.indexOf('error:clusterName:') === 0){
  51. var errors = this.get('errorsCount') + 1;
  52. this.set('errorsCount', errors);
  53. if(errors > this.get('attemptsCount')){
  54. console.log('Stop loading background operations: clusterName is undefined');
  55. return;
  56. }
  57. }
  58. this.set('timeoutId',
  59. setTimeout(function(){
  60. self.loadOperations();
  61. }, delay)
  62. );
  63. },
  64. /**
  65. * Reload operations
  66. * We can call it manually <code>controller.loadOperations();</code>
  67. * or it fires automatically, when <code>isWorking</code> becomes <code>true</code>
  68. */
  69. loadOperations : function(){
  70. var timeoutId = this.get('timeoutId');
  71. if(timeoutId){
  72. clearTimeout(timeoutId);
  73. this.set('timeoutId', null);
  74. }
  75. if(!this.get('isWorking')){
  76. return;
  77. }
  78. if(!App.router.getClusterName()){
  79. this.loadOperationsDelayed(this.get('updateInterval')/2, 'error:clusterName');
  80. return;
  81. }
  82. App.ajax.send({
  83. 'name': 'background_operations',
  84. 'sender': this,
  85. 'success': 'ajaxSuccess', //todo provide interfaces for strings and functions
  86. 'error': 'ajaxError'
  87. })
  88. }.observes('isWorking'),
  89. ajaxSuccess: function(data) {
  90. this.updateBackgroundOperations(data);
  91. this.loadOperationsDelayed();
  92. },
  93. ajaxError: function(request, ajaxOptions, error) {
  94. this.loadOperationsDelayed(null, 'error:response error');
  95. },
  96. /**
  97. * Update info about background operations
  98. * Put all tasks with command 'EXECUTE' into <code>executeTasks</code>, other tasks with it they are still running put into <code>runningTasks</code>
  99. * Put all task that should be shown in popup modal window into <code>this.allOperations</code>
  100. * @param data json loaded from server
  101. */
  102. updateBackgroundOperations: function (data) {
  103. var runningTasks = [];
  104. var executeTasks = this.get('executeTasks');
  105. data.items.forEach(function (item) {
  106. item.tasks.forEach(function (task) {
  107. if (task.Tasks.command == 'EXECUTE') {
  108. if (!executeTasks.someProperty('id', task.Tasks.id)) {
  109. executeTasks.push(task.Tasks);
  110. }
  111. } else {
  112. if (task.Tasks.status == 'QUEUED' || task.Tasks.status == 'PENDING' || task.Tasks.status == 'IN_PROGRESS') {
  113. runningTasks.push(task.Tasks);
  114. }
  115. }
  116. });
  117. });
  118. for (var i = 0; i < executeTasks.length; i++) {
  119. if (executeTasks[i].status == 'QUEUED' || executeTasks[i].status == 'PENDING' || executeTasks[i].status == 'IN_PROGRESS') {
  120. var url = App.testMode ? '/data/background_operations/list_on_start.json' :
  121. App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/requests/' + executeTasks[i].request_id + '/tasks/' + executeTasks[i].id;
  122. $.ajax({
  123. type: "GET",
  124. url: url,
  125. dataType: 'json',
  126. timeout: App.timeout,
  127. success: function (data) {
  128. if (data) {
  129. for(var i = 0;i < executeTasks.length; i++){
  130. if(data.Tasks.id == executeTasks[i].id){
  131. executeTasks[i] = data.Tasks;
  132. }
  133. }
  134. }
  135. },
  136. error: function () {
  137. console.log('ERROR: error during executeTask update');
  138. },
  139. statusCode: require('data/statusCodes')
  140. });
  141. }
  142. }
  143. var currentTasks;
  144. currentTasks = runningTasks.concat(executeTasks);
  145. currentTasks = currentTasks.sort(function (a, b) {
  146. return a.id - b.id;
  147. });
  148. // If the server is returning 999 as the return code, display blank and not 999
  149. currentTasks.forEach( function (task) {
  150. if (task.exit_code == 999) {
  151. task.display_exit_code = false;
  152. } else {
  153. task.display_exit_code = true;
  154. }
  155. });
  156. this.get('allOperations').filterProperty('isOpen').mapProperty('id').forEach(function(id){
  157. if (currentTasks.someProperty('id', id)) {
  158. currentTasks.findProperty('id', id).isOpen = true;
  159. }
  160. });
  161. this.set('allOperations', currentTasks);
  162. this.set('allOperationsCount', runningTasks.length + executeTasks.filterProperty('status', 'PENDING').length + executeTasks.filterProperty('status', 'QUEUED').length + executeTasks.filterProperty('status', 'IN_PROGRESS').length);
  163. var eventsArray = this.get('eventsArray');
  164. if (eventsArray.length) {
  165. var itemsToRemove = [];
  166. eventsArray.forEach(function(item){
  167. //if when returns true
  168. if(item.when(this)){
  169. //fire do method
  170. item.do();
  171. //and remove it
  172. itemsToRemove.push(item);
  173. }
  174. }, this);
  175. itemsToRemove.forEach(function(item){
  176. eventsArray.splice(eventsArray.indexOf(item), 1);
  177. });
  178. }
  179. },
  180. /**
  181. * Onclick handler for background operations number located right to logo
  182. */
  183. showPopup: function(){
  184. this.set('executeTasks', []);
  185. this.loadOperations();
  186. App.ModalPopup.show({
  187. headerClass: Ember.View.extend({
  188. controllerBinding: 'App.router.backgroundOperationsController',
  189. template:Ember.Handlebars.compile('{{allOperationsCount}} Background Operations Running')
  190. }),
  191. bodyClass: Ember.View.extend({
  192. controllerBinding: 'App.router.backgroundOperationsController',
  193. templateName: require('templates/main/background_operations_popup')
  194. }),
  195. onPrimary: function() {
  196. this.hide();
  197. },
  198. secondary : null
  199. });
  200. },
  201. /**
  202. * Exaple of data inside:
  203. * {
  204. * when : function(backgroundOperationsController){
  205. * return backgroundOperationsController.getOperationsForRequestId(requestId).length == 0;
  206. * },
  207. * do : function(){
  208. * component.set('status', 'cool');
  209. * }
  210. * }
  211. *
  212. * Function <code>do</code> will be fired once, when <code>when</code> returns true.
  213. * Example, how to use it, you can see in app\controllers\main\host\details.js
  214. */
  215. eventsArray : []
  216. });