polling.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. stage: '',
  21. label: '',
  22. isStarted: false,
  23. isPolling: true,
  24. clusterName: null,
  25. requestId: undefined,
  26. temp: false,
  27. progress: 0,
  28. url: null,
  29. testUrl: null,
  30. data: null,
  31. isError: false,
  32. isSuccess: false,
  33. POLL_INTERVAL: 4000,
  34. polledData: [],
  35. numPolls: 0,
  36. mockDataPrefix: '/data/wizard/deploy/5_hosts',
  37. barWidth: function () {
  38. var barWidth = 'width: ' + this.get('progress') + '%;';
  39. return barWidth;
  40. }.property('progress'),
  41. isCompleted: function () {
  42. return (this.get('isError') || this.get('isSuccess'));
  43. }.property('isError', 'isSuccess'),
  44. start: function () {
  45. if (this.get('requestId') === undefined) {
  46. this.setRequestId();
  47. } else {
  48. this.startPolling();
  49. }
  50. },
  51. setRequestId: function () {
  52. if (App.testMode) {
  53. this.set('requestId', '1');
  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. async: false,
  64. data: data,
  65. dataType: 'text',
  66. timeout: App.timeout,
  67. success: function (data) {
  68. var jsonData = jQuery.parseJSON(data);
  69. console.log("TRACE: Polling -> value of the url is: " + url);
  70. console.log("TRACE: Polling-> value of the sent data is: " + self.get('data'));
  71. console.log("TRACE: Polling-> value of the received data is: " + jsonData);
  72. if (jsonData === null) {
  73. self.set('isSuccess', true);
  74. } else {
  75. var requestId = jsonData.Requests.id;
  76. self.set('requestId', requestId);
  77. console.log('requestId is: ' + requestId);
  78. }
  79. },
  80. error: function () {
  81. console.log("ERROR");
  82. self.set('isError', true);
  83. },
  84. statusCode: require('data/statusCodes')
  85. });
  86. },
  87. doPolling: function () {
  88. if (this.get('requestId')) {
  89. this.startPolling();
  90. }
  91. }.observes('requestId'),
  92. startPolling: function () {
  93. var self = this;
  94. var url = App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/requests/' + this.get('requestId') + '?fields=tasks/*';
  95. if (App.testMode) {
  96. this.set('POLL_INTERVAL', 1);
  97. this.numPolls++;
  98. url = this.get('mockDataPrefix') + '/poll_' + this.get('numPolls') + '.json';
  99. }
  100. $.ajax({
  101. type: 'GET',
  102. url: url,
  103. async: true,
  104. dataType: 'text',
  105. timeout: App.timeout,
  106. success: function (data) {
  107. console.log("TRACE: In success function for the GET logs data");
  108. console.log("TRACE: The value is: ", jQuery.parseJSON(data));
  109. var result = self.parseInfo(jQuery.parseJSON(data));
  110. if (result !== true) {
  111. window.setTimeout(function () {
  112. self.startPolling();
  113. }, self.POLL_INTERVAL);
  114. }
  115. },
  116. error: function (request, ajaxOptions, error) {
  117. console.log("TRACE: In error function for the GET data");
  118. console.log("TRACE: value of the url is: " + url);
  119. console.log("TRACE: error code status is: " + request.status);
  120. self.set('requestId', undefined);
  121. self.set('isError', true);
  122. },
  123. statusCode: require('data/statusCodes')
  124. }).retry({times: App.maxRetries, timeout: App.timeout}).then(null,
  125. function () {
  126. App.showReloadPopup();
  127. console.log('Install services all retries failed');
  128. }
  129. );
  130. },
  131. stopPolling: function () {
  132. //this.set('isSuccess', true);
  133. },
  134. replacePolledData: function (polledData) {
  135. this.polledData.clear();
  136. this.set('polledData', polledData);
  137. },
  138. getExecutedTasks: function (tasksData) {
  139. var succededTasks = tasksData.filterProperty('Tasks.status', 'COMPLETED');
  140. var failedTasks = tasksData.filterProperty('Tasks.status', 'FAILED');
  141. var abortedTasks = tasksData.filterProperty('Tasks.status', 'ABORTED');
  142. var timedoutTasks = tasksData.filterProperty('Tasks.status', 'TIMEDOUT');
  143. var inProgressTasks = tasksData.filterProperty('Tasks.status', 'IN_PROGRESS');
  144. return (succededTasks.length + failedTasks.length + abortedTasks.length + timedoutTasks.length + inProgressTasks.length);
  145. },
  146. isPollingFinished: function (polledData) {
  147. if (polledData.everyProperty('Tasks.status', 'COMPLETED')) {
  148. this.set('isSuccess', true);
  149. return true;
  150. } else if (polledData.someProperty('Tasks.status', 'FAILED') || polledData.someProperty('Tasks.status', 'TIMEDOUT') || polledData.someProperty('Tasks.status', 'ABORTED')) {
  151. this.set('isError', true);
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. },
  157. parseInfo: function (polledData) {
  158. console.log('TRACE: Entering task info function');
  159. var self = this;
  160. var totalProgress = 0;
  161. var tasksData = polledData.tasks;
  162. console.log("The value of tasksData is: ", tasksData);
  163. if (!tasksData) {
  164. console.log("ERROR: NO tasks available to process");
  165. }
  166. var requestId = this.get('requestId');
  167. if (!App.testMode && polledData.Requests && polledData.Requests.id && polledData.Requests.id != requestId) {
  168. // We dont want to use non-current requestId's tasks data to
  169. // determine the current install status.
  170. // Also, we dont want to keep polling if it is not the
  171. // current requestId.
  172. return false;
  173. }
  174. this.replacePolledData(tasksData);
  175. /* this.hosts.forEach(function (_host) {
  176. var actionsPerHost = tasksData.filterProperty('Tasks.host_name', _host.name); // retrieved from polled Data
  177. if (actionsPerHost.length === 0) {
  178. _host.set('message', this.t('installer.step9.host.status.nothingToInstall'));
  179. console.log("INFO: No task is hosted on the host");
  180. }
  181. if (actionsPerHost !== null && actionsPerHost !== undefined && actionsPerHost.length !== 0) {
  182. this.setLogTasksStatePerHost(actionsPerHost, _host);
  183. this.onSuccessPerHost(actionsPerHost, _host); // every action should be a success
  184. this.onErrorPerHost(actionsPerHost, _host); // any action should be a failure
  185. this.onInProgressPerHost(actionsPerHost, _host); // current running action for a host
  186. totalProgress += self.progressPerHost(actionsPerHost, _host);
  187. }
  188. }, this); */
  189. var executedTasks = this.getExecutedTasks(tasksData);
  190. totalProgress = Math.floor((executedTasks / tasksData.length) * 100);
  191. this.set('progress', totalProgress.toString());
  192. console.log("INFO: right now the progress is: " + this.get('progress'));
  193. return this.isPollingFinished(tasksData);
  194. }
  195. });