polling.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.Poll = Em.Object.extend(App.ReloadPopupMixin, {
  20. name: '',
  21. stage: '',
  22. label: '',
  23. isVisible: true,
  24. isStarted: false,
  25. isPolling: true,
  26. clusterName: null,
  27. requestId: null,
  28. temp: false,
  29. progress: 0,
  30. url: null,
  31. testUrl: null,
  32. data: null,
  33. isError: false,
  34. isSuccess: false,
  35. POLL_INTERVAL: 4000,
  36. polledData: [],
  37. numPolls: 0,
  38. mockDataPrefix: '/data/wizard/deploy/5_hosts',
  39. currentTaskId: null,
  40. barWidth: Em.computed.format('width: {0}%;', 'progress'),
  41. isCompleted: Em.computed.or('isError', 'isSuccess'),
  42. showLink: Em.computed.or('isPolling', 'isStarted'),
  43. start: function () {
  44. if (Em.isNone(this.get('requestId'))) {
  45. this.setRequestId();
  46. } else {
  47. this.startPolling();
  48. }
  49. },
  50. setRequestId: function () {
  51. if (App.get('testMode')) {
  52. this.set('requestId', '1');
  53. this.doPolling();
  54. return;
  55. }
  56. var self = this;
  57. var url = this.get('url');
  58. var method = 'PUT';
  59. var data = this.get('data');
  60. $.ajax({
  61. type: method,
  62. url: url,
  63. data: data,
  64. dataType: 'text',
  65. timeout: App.timeout,
  66. success: function (data) {
  67. var jsonData = jQuery.parseJSON(data);
  68. if (Em.isNone(jsonData)) {
  69. self.set('isSuccess', true);
  70. self.set('isError', false);
  71. } else {
  72. var requestId = jsonData.Requests.id;
  73. self.set('requestId', requestId);
  74. self.doPolling();
  75. }
  76. },
  77. error: function () {
  78. self.set('isError', true);
  79. self.set('isSuccess', false);
  80. },
  81. statusCode: require('data/statusCodes')
  82. });
  83. },
  84. /**
  85. * set current task id and send request
  86. * @param taskId
  87. */
  88. updateTaskLog: function (taskId) {
  89. this.set('currentTaskId', taskId);
  90. this.pollTaskLog();
  91. },
  92. doPolling: function () {
  93. if (this.get('requestId')) {
  94. this.startPolling();
  95. }
  96. },
  97. /**
  98. * server call to obtain task logs
  99. */
  100. pollTaskLog: function () {
  101. if (this.get('currentTaskId')) {
  102. App.ajax.send({
  103. name: 'background_operations.get_by_task',
  104. sender: this,
  105. data: {
  106. requestId: this.get('requestId'),
  107. taskId: this.get('currentTaskId')
  108. },
  109. success: 'pollTaskLogSuccessCallback'
  110. })
  111. }
  112. },
  113. /**
  114. * update logs of current task
  115. * @param data
  116. */
  117. pollTaskLogSuccessCallback: function (data) {
  118. var currentTask = this.get('polledData').findProperty('Tasks.id', data.Tasks.id);
  119. currentTask.Tasks.stdout = data.Tasks.stdout;
  120. currentTask.Tasks.stderr = data.Tasks.stderr;
  121. Em.propertyDidChange(this, 'polledData');
  122. },
  123. /**
  124. * start polling operation data
  125. * @return {Boolean}
  126. */
  127. startPolling: function () {
  128. if (!this.get('requestId')) return false;
  129. this.pollTaskLog();
  130. App.ajax.send({
  131. name: 'background_operations.get_by_request',
  132. sender: this,
  133. data: {
  134. requestId: this.get('requestId'),
  135. callback: this.startPolling,
  136. errorLogMessage: 'Install services all retries failed'
  137. },
  138. success: 'reloadSuccessCallback',
  139. error: 'reloadErrorCallback'
  140. });
  141. return true;
  142. },
  143. reloadSuccessCallback: function (data) {
  144. var self = this;
  145. var result = this.parseInfo(data);
  146. this._super();
  147. if (!result) {
  148. window.setTimeout(function () {
  149. self.startPolling();
  150. }, this.POLL_INTERVAL);
  151. }
  152. },
  153. reloadErrorCallback: function (request, ajaxOptions, error, opt, params) {
  154. this._super(request, ajaxOptions, error, opt, params);
  155. if (request.status) {
  156. if (!this.get('isSuccess')) {
  157. this.set('isError', true);
  158. }
  159. }
  160. },
  161. stopPolling: function () {
  162. //this.set('isSuccess', true);
  163. },
  164. replacePolledData: function (polledData) {
  165. var currentTaskId = this.get('currentTaskId');
  166. if (currentTaskId) {
  167. var task = this.get('polledData').findProperty('Tasks.id', currentTaskId);
  168. var currentTask = polledData.findProperty('Tasks.id', currentTaskId);
  169. if (task && currentTask) {
  170. currentTask.Tasks.stdout = task.Tasks.stdout;
  171. currentTask.Tasks.stderr = task.Tasks.stderr;
  172. }
  173. }
  174. this.set('polledData', polledData);
  175. },
  176. calculateProgressByTasks: function (tasksData) {
  177. var queuedTasks = tasksData.filterProperty('Tasks.status', 'QUEUED').length;
  178. var completedTasks = tasksData.filter(function (task) {
  179. return ['COMPLETED', 'FAILED', 'ABORTED', 'TIMEDOUT'].contains(task.Tasks.status);
  180. }).length;
  181. var inProgressTasks = tasksData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  182. return Math.ceil(((queuedTasks * 0.09) + (inProgressTasks * 0.35) + completedTasks ) / tasksData.length * 100)
  183. },
  184. isPollingFinished: function (polledData) {
  185. var runningTasks;
  186. runningTasks = polledData.filterProperty('Tasks.status', 'QUEUED').length;
  187. runningTasks += polledData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  188. runningTasks += polledData.filterProperty('Tasks.status', 'PENDING').length;
  189. if (runningTasks === 0) {
  190. if (polledData.everyProperty('Tasks.status', 'COMPLETED')) {
  191. this.set('isSuccess', true);
  192. this.set('isError', false);
  193. } else if (polledData.someProperty('Tasks.status', 'FAILED') || polledData.someProperty('Tasks.status', 'TIMEDOUT') || polledData.someProperty('Tasks.status', 'ABORTED')) {
  194. this.set('isSuccess', false);
  195. this.set('isError', true);
  196. }
  197. return true;
  198. } else {
  199. return false;
  200. }
  201. },
  202. parseInfo: function (polledData) {
  203. var tasksData = polledData.tasks;
  204. var requestId = this.get('requestId');
  205. if (polledData.Requests && polledData.Requests.id && polledData.Requests.id != requestId) {
  206. // We don't want to use non-current requestId's tasks data to
  207. // determine the current install status.
  208. // Also, we don't want to keep polling if it is not the
  209. // current requestId.
  210. return false;
  211. }
  212. this.replacePolledData(tasksData);
  213. var totalProgress = this.calculateProgressByTasks(tasksData);
  214. this.set('progress', totalProgress.toString());
  215. return this.isPollingFinished(tasksData);
  216. }
  217. });