progress_controller.js 9.7 KB

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