polling.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 = Em.get(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 (!Em.isNone(this.get('requestId'))) {
  94. this.startPolling();
  95. }
  96. },
  97. /**
  98. * server call to obtain task logs
  99. */
  100. pollTaskLog: function () {
  101. if (!Em.isNone(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 (Em.isNone(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 && !this.get('isSuccess')) {
  156. this.set('isError', true);
  157. }
  158. },
  159. stopPolling: function () {
  160. //this.set('isSuccess', true);
  161. },
  162. replacePolledData: function (polledData) {
  163. var currentTaskId = this.get('currentTaskId');
  164. if (!Em.isNone(currentTaskId)) {
  165. var task = this.get('polledData').findProperty('Tasks.id', currentTaskId);
  166. var currentTask = polledData.findProperty('Tasks.id', currentTaskId);
  167. if (task && currentTask) {
  168. currentTask.Tasks.stdout = task.Tasks.stdout;
  169. currentTask.Tasks.stderr = task.Tasks.stderr;
  170. }
  171. }
  172. this.set('polledData', polledData);
  173. },
  174. calculateProgressByTasks: function (tasksData) {
  175. var queuedTasks = tasksData.filterProperty('Tasks.status', 'QUEUED').length;
  176. var completedTasks = tasksData.filter(function (task) {
  177. return ['COMPLETED', 'FAILED', 'ABORTED', 'TIMEDOUT'].contains(task.Tasks.status);
  178. }).length;
  179. var inProgressTasks = tasksData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  180. return Math.ceil(((queuedTasks * 0.09) + (inProgressTasks * 0.35) + completedTasks ) / tasksData.length * 100)
  181. },
  182. isPollingFinished: function (polledData) {
  183. var runningTasks;
  184. runningTasks = polledData.filterProperty('Tasks.status', 'QUEUED').length;
  185. runningTasks += polledData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  186. runningTasks += polledData.filterProperty('Tasks.status', 'PENDING').length;
  187. if (runningTasks === 0) {
  188. if (polledData.everyProperty('Tasks.status', 'COMPLETED')) {
  189. this.set('isSuccess', true);
  190. this.set('isError', false);
  191. } else if (polledData.someProperty('Tasks.status', 'FAILED') || polledData.someProperty('Tasks.status', 'TIMEDOUT') || polledData.someProperty('Tasks.status', 'ABORTED')) {
  192. this.set('isSuccess', false);
  193. this.set('isError', true);
  194. }
  195. return true;
  196. } else {
  197. return false;
  198. }
  199. },
  200. parseInfo: function (polledData) {
  201. var tasksData = polledData.tasks;
  202. var requestId = this.get('requestId');
  203. if (polledData.Requests && !Em.isNone(polledData.Requests.id) && polledData.Requests.id != requestId) {
  204. // We don't want to use non-current requestId's tasks data to
  205. // determine the current install status.
  206. // Also, we don't want to keep polling if it is not the
  207. // current requestId.
  208. return false;
  209. }
  210. this.replacePolledData(tasksData);
  211. var totalProgress = this.calculateProgressByTasks(tasksData);
  212. this.set('progress', totalProgress.toString());
  213. return this.isPollingFinished(tasksData);
  214. }
  215. });