polling.js 7.9 KB

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