progress_controller.js 11 KB

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