polling.js 7.1 KB

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