progress_controller.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. loadStep: function () {
  28. this.clearStep();
  29. this.loadTasks();
  30. this.addObserver('tasks.@each.status', this, 'onTaskStatusChange');
  31. this.onTaskStatusChange();
  32. },
  33. clearStep: function () {
  34. this.set('isSubmitDisabled', true);
  35. this.set('tasks', []);
  36. this.set('logs', []);
  37. this.set('currentRequestIds', []);
  38. var commands = this.get('commands');
  39. var currentStep = App.router.get('highAvailabilityWizardController.currentStep');
  40. for (var i = 0; i < commands.length; i++) {
  41. this.get('tasks').pushObject(Ember.Object.create({
  42. title: Em.I18n.t('admin.highAvailability.wizard.step' + currentStep + '.task' + i + '.title'),
  43. status: 'PENDING',
  44. id: i,
  45. command: commands[i],
  46. showRetry: false
  47. }));
  48. }
  49. },
  50. loadTasks: function () {
  51. //load and set tasks statuses form server
  52. },
  53. setTaskStatus: function (taskId, status) {
  54. this.get('tasks').findProperty('id', taskId).set('status', status)
  55. },
  56. showRetry: function (taskId) {
  57. this.get('tasks').findProperty('id', taskId).set('showRetry', true);
  58. },
  59. retryTask: function () {
  60. var task = this.get('tasks').findProperty('status', 'FAILED');
  61. task.set('showRetry', false);
  62. task.set('status', 'PENDING');
  63. },
  64. onTaskStatusChange: function () {
  65. if (!this.get('tasks').someProperty('status', 'IN_PROGRESS') && !this.get('tasks').someProperty('status', 'QUEUED') && !this.get('tasks').someProperty('status', 'FAILED')) {
  66. var nextTask = this.get('tasks').findProperty('status', 'PENDING');
  67. if (nextTask) {
  68. this.setTaskStatus(nextTask.get('id'), 'QUEUED');
  69. this.set('currentTaskId', nextTask.get('id'));
  70. this.runTask(nextTask.get('id'));
  71. } else {
  72. this.set('isSubmitDisabled', false);
  73. }
  74. }
  75. },
  76. /*
  77. run command of appropriate task
  78. */
  79. runTask: function (taskId) {
  80. this[this.get('tasks').findProperty('id', taskId).get('command')]();
  81. },
  82. onTaskError: function () {
  83. this.setTaskStatus(this.get('currentTaskId'), 'FAILED');
  84. this.showRetry(this.get('currentTaskId'));
  85. },
  86. onTaskCompleted: function () {
  87. this.setTaskStatus(this.get('currentTaskId'), 'COMPLETED');
  88. },
  89. createComponent: function (componentName, hostName) {
  90. if (!(hostName instanceof Array)) {
  91. hostName = [hostName];
  92. }
  93. var hostComponents = [];
  94. for (var i = 0; i < hostName.length; i++) {
  95. hostComponents = App.HostComponent.find().filterProperty('componentName', componentName);
  96. if (!hostComponents.length || !hostComponents.mapProperty('host.hostName').contains(hostName[i])) {
  97. App.ajax.send({
  98. name: 'admin.high_availability.create_component',
  99. sender: this,
  100. data: {
  101. hostName: hostName[i],
  102. componentName: componentName,
  103. taskNum: hostName.length
  104. },
  105. success: 'onCreateComponent',
  106. error: 'onTaskError'
  107. });
  108. } else {
  109. var taskNum = hostName.length;
  110. this.installComponent(componentName, hostName[i], taskNum);
  111. }
  112. }
  113. },
  114. onCreateComponent: function () {
  115. var hostName = arguments[2].hostName;
  116. var componentName = arguments[2].componentName;
  117. var taskNum = arguments[2].taskNum;
  118. this.installComponent(componentName, hostName, taskNum);
  119. },
  120. installComponent: function (componentName, hostName, taskNum) {
  121. if (!(hostName instanceof Array)) {
  122. hostName = [hostName];
  123. }
  124. for (var i = 0; i < hostName.length; i++) {
  125. App.ajax.send({
  126. name: 'admin.high_availability.install_component',
  127. sender: this,
  128. data: {
  129. hostName: hostName[i],
  130. componentName: componentName,
  131. displayName: App.format.role(componentName),
  132. taskNum: taskNum || hostName.length
  133. },
  134. success: 'startPolling',
  135. error: 'onTaskError'
  136. });
  137. }
  138. },
  139. startComponent: function (componentName, hostName) {
  140. if (!(hostName instanceof Array)) {
  141. hostName = [hostName];
  142. }
  143. for (var i = 0; i < hostName.length; i++) {
  144. App.ajax.send({
  145. name: 'admin.high_availability.start_component',
  146. sender: this,
  147. data: {
  148. hostName: hostName[i],
  149. componentName: componentName,
  150. displayName: App.format.role(componentName),
  151. taskNum: hostName.length
  152. },
  153. success: 'startPolling',
  154. error: 'onTaskError'
  155. });
  156. }
  157. },
  158. startPolling: function (data) {
  159. if (data) {
  160. this.get('currentRequestIds').push(data.Requests.id);
  161. var tasksCount = arguments[2].taskNum || 1;
  162. if (tasksCount === this.get('currentRequestIds').length) {
  163. this.doPolling();
  164. }
  165. } else {
  166. this.onTaskCompleted();
  167. }
  168. },
  169. doPolling: function () {
  170. var requestIds = this.get('currentRequestIds');
  171. for (var i = 0; i < requestIds.length; i++) {
  172. App.ajax.send({
  173. name: 'admin.high_availability.polling',
  174. sender: this,
  175. data: {
  176. requestId: requestIds[i]
  177. },
  178. success: 'parseLogs',
  179. error: 'onTaskError'
  180. });
  181. }
  182. },
  183. parseLogs: function (logs) {
  184. this.get('logs').push(logs.tasks);
  185. if (this.get('currentRequestIds').length === this.get('logs').length) {
  186. var tasks = [];
  187. this.get('logs').forEach(function (logs) {
  188. tasks.pushObjects(logs);
  189. }, this);
  190. var self = this;
  191. var currentTaskId = this.get('currentTaskId');
  192. if (!tasks.someProperty('Tasks.status', 'PENDING') && !tasks.someProperty('Tasks.status', 'QUEUED') && !tasks.someProperty('Tasks.status', 'IN_PROGRESS')) {
  193. if (tasks.someProperty('Tasks.status', 'FAILED')) {
  194. this.setTaskStatus(currentTaskId, 'FAILED');
  195. } else {
  196. this.setTaskStatus(currentTaskId, 'COMPLETED');
  197. }
  198. this.set('currentRequestIds', []);
  199. } else {
  200. var progress = Math.round((tasks.filterProperty('Tasks.status', 'COMPLETED').length + tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length / 2) / tasks.length * 100);
  201. this.get('tasks').findProperty('id', currentTaskId).set('progress', progress);
  202. this.setTaskStatus(currentTaskId, 'IN_PROGRESS');
  203. window.setTimeout(function () {
  204. self.doPolling()
  205. }, self.POLL_INTERVAL);
  206. }
  207. this.set('logs', []);
  208. }
  209. },
  210. done: function () {
  211. if (!this.get('isSubmitDisabled')) {
  212. this.removeObserver('tasks.@each.status', this, 'onTaskStatusChange');
  213. App.router.send('next');
  214. }
  215. }
  216. });