progress_controller.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.HighAvailabilityProgressPageController = Em.Controller.extend({
  20. tasks: [],
  21. commands: [],
  22. currentRequestIds: [],
  23. logs: [],
  24. currentTaskId: null,
  25. POLL_INTERVAL: 4000,
  26. isSubmitDisabled: true,
  27. serviceTimestamp: null,
  28. loadStep: function () {
  29. this.clearStep();
  30. this.loadTasks();
  31. this.addObserver('tasks.@each.status', this, 'onTaskStatusChange');
  32. this.onTaskStatusChange();
  33. },
  34. clearStep: function () {
  35. this.set('isSubmitDisabled', true);
  36. this.set('tasks', []);
  37. this.set('logs', []);
  38. this.set('currentRequestIds', []);
  39. var commands = this.get('commands');
  40. var currentStep = App.router.get('highAvailabilityWizardController.currentStep');
  41. for (var i = 0; i < commands.length; i++) {
  42. this.get('tasks').pushObject(Ember.Object.create({
  43. title: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  44. status: 'PENDING',
  45. id: i,
  46. command: commands[i],
  47. showRetry: false,
  48. name: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  49. displayName: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  50. progress: 0,
  51. isRunning: false,
  52. hosts: []
  53. }));
  54. }
  55. },
  56. services: function(){
  57. return this.get('tasks');
  58. }.property('tasks'),
  59. loadTasks: function () {
  60. //load and set tasks statuses form server
  61. },
  62. setTaskStatus: function (taskId, status) {
  63. this.get('tasks').findProperty('id', taskId).set('status', status);
  64. },
  65. setTaskLogs: function (taskId, tasks) {
  66. var hosts = [];
  67. tasks.forEach(function (task) {
  68. hosts.push(
  69. {
  70. name: task.Tasks.host_name,
  71. publicName: task.Tasks.host_name,
  72. logTasks: [task]
  73. }
  74. );
  75. });
  76. this.get('tasks').findProperty('id', taskId).set('hosts', hosts);
  77. this.set('serviceTimestamp', new Date().getTime());
  78. },
  79. showRetry: function (taskId) {
  80. this.get('tasks').findProperty('id', taskId).set('showRetry', true);
  81. },
  82. retryTask: function () {
  83. var task = this.get('tasks').findProperty('status', 'FAILED');
  84. task.set('showRetry', false);
  85. task.set('status', 'PENDING');
  86. },
  87. onTaskStatusChange: function () {
  88. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  89. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  90. if (nextTask) {
  91. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  92. this.set('currentTaskId', nextTask.get('id'));
  93. this.runTask(nextTask.get('id'));
  94. } else {
  95. this.set('isSubmitDisabled', false);
  96. }
  97. }
  98. },
  99. /*
  100. run command of appropriate task
  101. */
  102. runTask: function (taskId) {
  103. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  104. },
  105. onTaskError: function () {
  106. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  107. this.showRetry(this.get('currentTaskId'));
  108. },
  109. onTaskCompleted: function () {
  110. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  111. },
  112. createComponent: function (componentName, hostName) {
  113. if (!(hostName instanceof Array)) {
  114. hostName = [hostName];
  115. }
  116. var hostComponents = [];
  117. for (var i = 0; i < hostName.length; i++) {
  118. hostComponents = App.HostComponent.find().filterProperty('componentName', componentName);
  119. if (!hostComponents.length || !hostComponents.mapProperty('host.hostName').contains(hostName[i])) {
  120. App.ajax.send({
  121. name: 'admin.high_availability.create_component',
  122. sender: this,
  123. data: {
  124. hostName: hostName[i],
  125. componentName: componentName,
  126. taskNum: hostName.length
  127. },
  128. success: 'onCreateComponent',
  129. error: 'onTaskError'
  130. });
  131. } else {
  132. var taskNum = hostName.length;
  133. this.installComponent(componentName, hostName[i], taskNum);
  134. }
  135. }
  136. },
  137. onCreateComponent: function () {
  138. var hostName = arguments[2].hostName;
  139. var componentName = arguments[2].componentName;
  140. var taskNum = arguments[2].taskNum;
  141. this.installComponent(componentName, hostName, taskNum);
  142. },
  143. installComponent: function (componentName, hostName, taskNum) {
  144. if (!(hostName instanceof Array)) {
  145. hostName = [hostName];
  146. }
  147. for (var i = 0; i < hostName.length; i++) {
  148. App.ajax.send({
  149. name: 'admin.high_availability.install_component',
  150. sender: this,
  151. data: {
  152. hostName: hostName[i],
  153. componentName: componentName,
  154. displayName: App.format.role(componentName),
  155. taskNum: taskNum || hostName.length
  156. },
  157. success: 'startPolling',
  158. error: 'onTaskError'
  159. });
  160. }
  161. },
  162. startComponent: function (componentName, hostName) {
  163. if (!(hostName instanceof Array)) {
  164. hostName = [hostName];
  165. }
  166. for (var i = 0; i < hostName.length; i++) {
  167. App.ajax.send({
  168. name: 'admin.high_availability.start_component',
  169. sender: this,
  170. data: {
  171. hostName: hostName[i],
  172. componentName: componentName,
  173. displayName: App.format.role(componentName),
  174. taskNum: hostName.length
  175. },
  176. success: 'startPolling',
  177. error: 'onTaskError'
  178. });
  179. }
  180. },
  181. startPolling: function (data) {
  182. if (data) {
  183. this.get('currentRequestIds').push(data.Requests.id);
  184. var tasksCount = arguments[2].taskNum || 1;
  185. if (tasksCount === this.get('currentRequestIds').length) {
  186. this.doPolling();
  187. }
  188. } else {
  189. this.onTaskCompleted();
  190. }
  191. },
  192. doPolling: function () {
  193. var requestIds = this.get('currentRequestIds');
  194. for (var i = 0; i < requestIds.length; i++) {
  195. App.ajax.send({
  196. name: 'admin.high_availability.polling',
  197. sender: this,
  198. data: {
  199. requestId: requestIds[i]
  200. },
  201. success: 'parseLogs',
  202. error: 'onTaskError'
  203. });
  204. }
  205. },
  206. parseLogs: function (logs) {
  207. this.get('logs').push(logs.tasks);
  208. if (this.get('currentRequestIds').length === this.get('logs').length) {
  209. var tasks = [];
  210. this.get('logs').forEach(function (logs) {
  211. tasks.pushObjects(logs);
  212. }, this);
  213. var self = this;
  214. var currentTaskId = this.get('currentTaskId');
  215. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  216. if (tasks.someProperty('Tasks.status', 'FAILED')) {
  217. this.setTaskStatus(currentTaskId, 'FAILED');
  218. this.showRetry(currentTaskId);
  219. } else {
  220. this.setTaskStatus(currentTaskId, 'COMPLETED');
  221. }
  222. this.set('currentRequestIds', []);
  223. } else {
  224. var actionsPerHost = tasks.length;
  225. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  226. + tasks.filterProperty('Tasks.status', 'FAILED').length
  227. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  228. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  229. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  230. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  231. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  232. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  233. this.setTaskStatus(currentTaskId, 'IN_PROGRESS');
  234. window.setTimeout(function () {
  235. self.doPolling()
  236. }, self.POLL_INTERVAL);
  237. }
  238. this.setTaskLogs(currentTaskId, tasks);
  239. this.set('logs', []);
  240. }
  241. },
  242. done: function () {
  243. if (!this.get('isSubmitDisabled')) {
  244. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  245. App.router.send('next');
  246. }
  247. }
  248. });