polling.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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({
  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: function () {
  41. return 'width: ' + this.get('progress') + '%;';
  42. }.property('progress'),
  43. isCompleted: function () {
  44. return (this.get('isError') || this.get('isSuccess'));
  45. }.property('isError', 'isSuccess'),
  46. showLink: function () {
  47. return (this.get('isPolling') === true && this.get('isStarted') === true);
  48. }.property('isPolling', 'isStarted'),
  49. start: function () {
  50. if (Em.isNone(this.get('requestId'))) {
  51. this.setRequestId();
  52. } else {
  53. this.startPolling();
  54. }
  55. },
  56. setRequestId: function () {
  57. if (App.get('testMode')) {
  58. this.set('requestId', '1');
  59. this.doPolling();
  60. return;
  61. }
  62. var self = this;
  63. var url = this.get('url');
  64. var method = 'PUT';
  65. var data = this.get('data');
  66. $.ajax({
  67. type: method,
  68. url: url,
  69. data: data,
  70. dataType: 'text',
  71. timeout: App.timeout,
  72. success: function (data) {
  73. var jsonData = jQuery.parseJSON(data);
  74. console.log("TRACE: Polling -> value of the url is: " + url);
  75. console.log("TRACE: Polling-> value of the sent data is: " + self.get('data'));
  76. console.log("TRACE: Polling-> value of the received data is: " + jsonData);
  77. if (Em.isNone(jsonData)) {
  78. self.set('isSuccess', true);
  79. self.set('isError', false);
  80. } else {
  81. var requestId = jsonData.Requests.id;
  82. self.set('requestId', requestId);
  83. self.doPolling();
  84. console.log('requestId is: ' + requestId);
  85. }
  86. },
  87. error: function () {
  88. console.log("ERROR");
  89. self.set('isError', true);
  90. self.set('isSuccess', false);
  91. },
  92. statusCode: require('data/statusCodes')
  93. });
  94. },
  95. /**
  96. * set current task id and send request
  97. * @param taskId
  98. */
  99. updateTaskLog: function (taskId) {
  100. this.set('currentTaskId', taskId);
  101. this.pollTaskLog();
  102. },
  103. doPolling: function () {
  104. if (this.get('requestId')) {
  105. this.startPolling();
  106. }
  107. },
  108. /**
  109. * server call to obtain task logs
  110. */
  111. pollTaskLog: function () {
  112. if (this.get('currentTaskId')) {
  113. App.ajax.send({
  114. name: 'background_operations.get_by_task',
  115. sender: this,
  116. data: {
  117. requestId: this.get('requestId'),
  118. taskId: this.get('currentTaskId')
  119. },
  120. success: 'pollTaskLogSuccessCallback'
  121. })
  122. }
  123. },
  124. /**
  125. * update logs of current task
  126. * @param data
  127. */
  128. pollTaskLogSuccessCallback: function (data) {
  129. var currentTask = this.get('polledData').findProperty('Tasks.id', data.Tasks.id);
  130. currentTask.Tasks.stdout = data.Tasks.stdout;
  131. currentTask.Tasks.stderr = data.Tasks.stderr;
  132. Em.propertyDidChange(this, 'polledData');
  133. },
  134. /**
  135. * start polling operation data
  136. * @return {Boolean}
  137. */
  138. startPolling: function () {
  139. if (!this.get('requestId')) return false;
  140. this.pollTaskLog();
  141. App.ajax.send({
  142. name: 'background_operations.get_by_request',
  143. sender: this,
  144. data: {
  145. requestId: this.get('requestId')
  146. },
  147. success: 'startPollingSuccessCallback',
  148. error: 'startPollingErrorCallback'
  149. })
  150. .retry({times: App.maxRetries, timeout: App.timeout})
  151. .then(null, function () {
  152. App.showReloadPopup();
  153. console.log('Install services all retries failed');
  154. });
  155. return true;
  156. },
  157. startPollingSuccessCallback: function (data) {
  158. var self = this;
  159. var result = this.parseInfo(data);
  160. if (!result) {
  161. window.setTimeout(function () {
  162. self.startPolling();
  163. }, this.POLL_INTERVAL);
  164. }
  165. },
  166. startPollingErrorCallback: function (request, ajaxOptions, error) {
  167. console.log("TRACE: In error function for the GET data");
  168. console.log("TRACE: value of the url is: " + url);
  169. console.log("TRACE: error code status is: " + request.status);
  170. if (!this.get('isSuccess')) {
  171. this.set('isError', true);
  172. }
  173. },
  174. stopPolling: function () {
  175. //this.set('isSuccess', true);
  176. },
  177. replacePolledData: function (polledData) {
  178. var currentTaskId = this.get('currentTaskId');
  179. if (currentTaskId) {
  180. var task = this.get('polledData').findProperty('Tasks.id', currentTaskId);
  181. var currentTask = polledData.findProperty('Tasks.id', currentTaskId);
  182. if (task && currentTask) {
  183. currentTask.Tasks.stdout = task.Tasks.stdout;
  184. currentTask.Tasks.stderr = task.Tasks.stderr;
  185. }
  186. }
  187. this.set('polledData', polledData);
  188. },
  189. calculateProgressByTasks: function (tasksData) {
  190. var queuedTasks = tasksData.filterProperty('Tasks.status', 'QUEUED').length;
  191. var completedTasks = tasksData.filter(function (task) {
  192. return ['COMPLETED', 'FAILED', 'ABORTED', 'TIMEDOUT'].contains(task.Tasks.status);
  193. }).length;
  194. var inProgressTasks = tasksData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  195. return Math.ceil(((queuedTasks * 0.09) + (inProgressTasks * 0.35) + completedTasks ) / tasksData.length * 100)
  196. },
  197. isPollingFinished: function (polledData) {
  198. var runningTasks;
  199. runningTasks = polledData.filterProperty('Tasks.status', 'QUEUED').length;
  200. runningTasks += polledData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  201. runningTasks += polledData.filterProperty('Tasks.status', 'PENDING').length;
  202. if (runningTasks === 0) {
  203. if (polledData.everyProperty('Tasks.status', 'COMPLETED')) {
  204. this.set('isSuccess', true);
  205. this.set('isError', false);
  206. } else if (polledData.someProperty('Tasks.status', 'FAILED') || polledData.someProperty('Tasks.status', 'TIMEDOUT') || polledData.someProperty('Tasks.status', 'ABORTED')) {
  207. this.set('isSuccess', false);
  208. this.set('isError', true);
  209. }
  210. return true;
  211. } else {
  212. return false;
  213. }
  214. },
  215. parseInfo: function (polledData) {
  216. console.log('TRACE: Entering task info function');
  217. var self = this;
  218. var totalProgress = 0;
  219. var tasksData = polledData.tasks;
  220. console.log("The value of tasksData is: ", tasksData);
  221. if (!tasksData) {
  222. console.log("ERROR: NO tasks available to process");
  223. }
  224. var requestId = this.get('requestId');
  225. if (polledData.Requests && polledData.Requests.id && polledData.Requests.id != requestId) {
  226. // We don't want to use non-current requestId's tasks data to
  227. // determine the current install status.
  228. // Also, we don't want to keep polling if it is not the
  229. // current requestId.
  230. return false;
  231. }
  232. this.replacePolledData(tasksData);
  233. var totalProgress = this.calculateProgressByTasks(tasksData);
  234. this.set('progress', totalProgress.toString());
  235. console.log("INFO: right now the progress is: " + this.get('progress'));
  236. return this.isPollingFinished(tasksData);
  237. }
  238. });