progress_controller.js 6.2 KB

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