progress_controller.js 11 KB

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