progress_controller.js 9.9 KB

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