progress_controller.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. var loadedStauses = this.get('content.tasksStatuses');
  61. if (loadedStauses && loadedStauses.length === this.get('tasks').length) {
  62. for (var i = 0; i < loadedStauses.length; i++) {
  63. this.setTaskStatus(i, loadedStauses[i]);
  64. }
  65. if (loadedStauses.contains('FAILED')) {
  66. this.showRetry();
  67. }
  68. if (loadedStauses.contains('IN_PROGRESS')) {
  69. this.set('currentRequestIds', this.get('content.requestIds'));
  70. this.set('currentTaskId', loadedStauses.indexOf('IN_PROGRESS'));
  71. this.doPolling();
  72. }
  73. }
  74. },
  75. setTaskStatus: function (taskId, status) {
  76. this.get('tasks').findProperty('id', taskId).set('status', status);
  77. },
  78. setTaskLogs: function (taskId, tasks) {
  79. var hosts = [];
  80. var uniqHosts = tasks.mapProperty('Tasks.host_name').uniq();
  81. uniqHosts.forEach(function (host) {
  82. var curHostTasks = tasks.filterProperty('Tasks.host_name', host);
  83. hosts.push(
  84. {
  85. name: host,
  86. publicName: host,
  87. logTasks: curHostTasks
  88. }
  89. );
  90. });
  91. this.get('tasks').findProperty('id', taskId).set('hosts', hosts);
  92. this.set('serviceTimestamp', new Date().getTime());
  93. },
  94. showRetry: function () {
  95. this.get('tasks').findProperty('status', 'FAILED').set('showRetry', true);
  96. },
  97. retryTask: function () {
  98. var task = this.get('tasks').findProperty('status', 'FAILED');
  99. task.set('showRetry', false);
  100. task.set('status', 'PENDING');
  101. },
  102. onTaskStatusChange: function () {
  103. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  104. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  105. if (nextTask) {
  106. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  107. this.set('currentTaskId', nextTask.get('id'));
  108. this.runTask(nextTask.get('id'));
  109. } else {
  110. this.set('isSubmitDisabled', false);
  111. }
  112. }
  113. var statuses = this.get('tasks').mapProperty('status');
  114. var requestIds = this.get('currentRequestIds');
  115. App.router.get(this.get('content.controllerName')).saveTasksStatuses(statuses);
  116. App.router.get(this.get('content.controllerName')).saveRequestIds(requestIds);
  117. App.clusterStatus.setClusterStatus({
  118. clusterName: this.get('content.cluster.name'),
  119. clusterState: 'HIGH_AVAILABILITY_DEPLOY',
  120. wizardControllerName: this.get('content.controllerName'),
  121. localdb: App.db.data
  122. });
  123. },
  124. /*
  125. run command of appropriate task
  126. */
  127. runTask: function (taskId) {
  128. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  129. },
  130. onTaskError: function () {
  131. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  132. this.showRetry();
  133. },
  134. onTaskCompleted: function () {
  135. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  136. },
  137. createComponent: function (componentName, hostName) {
  138. if (!(hostName instanceof Array)) {
  139. hostName = [hostName];
  140. }
  141. var hostComponents = [];
  142. for (var i = 0; i < hostName.length; i++) {
  143. hostComponents = App.HostComponent.find().filterProperty('componentName', componentName);
  144. if (!hostComponents.length || !hostComponents.mapProperty('host.hostName').contains(hostName[i])) {
  145. App.ajax.send({
  146. name: 'admin.high_availability.create_component',
  147. sender: this,
  148. data: {
  149. hostName: hostName[i],
  150. componentName: componentName,
  151. taskNum: hostName.length
  152. },
  153. success: 'onCreateComponent',
  154. error: 'onTaskError'
  155. });
  156. } else {
  157. var taskNum = hostName.length;
  158. this.installComponent(componentName, hostName[i], taskNum);
  159. }
  160. }
  161. },
  162. onCreateComponent: function () {
  163. var hostName = arguments[2].hostName;
  164. var componentName = arguments[2].componentName;
  165. var taskNum = arguments[2].taskNum;
  166. this.installComponent(componentName, hostName, taskNum);
  167. },
  168. installComponent: function (componentName, hostName, taskNum) {
  169. if (!(hostName instanceof Array)) {
  170. hostName = [hostName];
  171. }
  172. for (var i = 0; i < hostName.length; i++) {
  173. App.ajax.send({
  174. name: 'admin.high_availability.install_component',
  175. sender: this,
  176. data: {
  177. hostName: hostName[i],
  178. componentName: componentName,
  179. displayName: App.format.role(componentName),
  180. taskNum: taskNum || hostName.length
  181. },
  182. success: 'startPolling',
  183. error: 'onTaskError'
  184. });
  185. }
  186. },
  187. startComponent: function (componentName, hostName) {
  188. if (!(hostName instanceof Array)) {
  189. hostName = [hostName];
  190. }
  191. for (var i = 0; i < hostName.length; i++) {
  192. App.ajax.send({
  193. name: 'admin.high_availability.start_component',
  194. sender: this,
  195. data: {
  196. hostName: hostName[i],
  197. componentName: componentName,
  198. displayName: App.format.role(componentName),
  199. taskNum: hostName.length
  200. },
  201. success: 'startPolling',
  202. error: 'onTaskError'
  203. });
  204. }
  205. },
  206. startPolling: function (data) {
  207. if (data) {
  208. this.get('currentRequestIds').push(data.Requests.id);
  209. var tasksCount = arguments[2].taskNum || 1;
  210. if (tasksCount === this.get('currentRequestIds').length) {
  211. this.doPolling();
  212. }
  213. } else {
  214. this.onTaskCompleted();
  215. }
  216. },
  217. doPolling: function () {
  218. this.setTaskStatus(this.get('currentTaskId'), 'IN_PROGRESS');
  219. var requestIds = this.get('currentRequestIds');
  220. for (var i = 0; i < requestIds.length; i++) {
  221. App.ajax.send({
  222. name: 'admin.high_availability.polling',
  223. sender: this,
  224. data: {
  225. requestId: requestIds[i]
  226. },
  227. success: 'parseLogs',
  228. error: 'onTaskError'
  229. });
  230. }
  231. },
  232. parseLogs: function (logs) {
  233. this.get('logs').push(logs.tasks);
  234. if (this.get('currentRequestIds').length === this.get('logs').length) {
  235. var tasks = [];
  236. this.get('logs').forEach(function (logs) {
  237. tasks.pushObjects(logs);
  238. }, this);
  239. var self = this;
  240. var currentTaskId = this.get('currentTaskId');
  241. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  242. this.set('currentRequestIds', []);
  243. if (tasks.someProperty('Tasks.status', 'FAILED')) {
  244. this.setTaskStatus(currentTaskId, 'FAILED');
  245. this.showRetry();
  246. } else {
  247. this.setTaskStatus(currentTaskId, 'COMPLETED');
  248. }
  249. } else {
  250. var actionsPerHost = tasks.length;
  251. var completedActions = tasks.filterProperty('Tasks.status', 'COMPLETED').length
  252. + tasks.filterProperty('Tasks.status', 'FAILED').length
  253. + tasks.filterProperty('Tasks.status', 'ABORTED').length
  254. + tasks.filterProperty('Tasks.status', 'TIMEDOUT').length;
  255. var queuedActions = tasks.filterProperty('Tasks.status', 'QUEUED').length;
  256. var inProgressActions = tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  257. var progress = Math.ceil(((queuedActions * 0.09) + (inProgressActions * 0.35) + completedActions ) / actionsPerHost * 100);
  258. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  259. window.setTimeout(function () {
  260. self.doPolling()
  261. }, self.POLL_INTERVAL);
  262. }
  263. this.setTaskLogs(currentTaskId, tasks);
  264. this.set('logs', []);
  265. }
  266. },
  267. done: function () {
  268. if (!this.get('isSubmitDisabled')) {
  269. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  270. App.router.send('next');
  271. }
  272. }
  273. });