progress_controller.js 6.6 KB

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