trying to solve issues with promises.

This commit is contained in:
Stefan Midjich 2016-04-18 22:57:49 +02:00
parent e642ea5c9e
commit dfc2071ce0

View file

@ -3,6 +3,8 @@
// //
// //
var debug = true;
// This function ensures the user gets redirect to the correct destination once // This function ensures the user gets redirect to the correct destination once
// all jobs have succeeded in the portal software. // all jobs have succeeded in the portal software.
function do_success() { function do_success() {
@ -14,9 +16,9 @@ function do_success() {
function do_error(message) { function do_error(message) {
$('#error-row').show(); $('#error-row').show();
$('#form-row').hide(); $('#form-row').hide();
$('#error-msg').val('Failed. Reload page and try again or contact support. '); $('#error-message').val('Failed. Reload page and try again or contact support. ');
if (message) { if (message) {
$('#error-msg').append('System response: '+message); $('#error-message').append('System response: '+message);
} }
} }
@ -24,13 +26,21 @@ function do_error(message) {
function poll_jobs(data) { function poll_jobs(data) {
var promises = []; var promises = [];
if(debug) {
console.log('Jobs data: ', data);
}
// Push promises into array // Push promises into array
for(var job in data) { for(var job in data) {
var job_id = data[job].id; var job_id = data[job].id;
var api_url = '/job/'+job_id; var api_url = '/job/'+job_id;
if (debug) {
console.log('Processing job: ', data[job]);
}
promises.push(new Promise(function(resolve, reject) { promises.push(new Promise(function(resolve, reject) {
var maxRun = 3; var maxRun = plugin_ttl/2;
var timesRun = 0; var timesRun = 0;
// Timer function that polls the API for job results // Timer function that polls the API for job results
@ -39,8 +49,17 @@ function poll_jobs(data) {
ajaxReq.done(function(getResponse) { ajaxReq.done(function(getResponse) {
// Verify job data // Verify job data
var job_result = getResponse; var job_result = getResponse;
if (debug) {
console.log('Job results: ', job_result);
}
console.log(job_result);
if(job_result.is_finished) { if(job_result.is_finished) {
resolve(job_result.result); console.log('Resolving job: ', job_result.id);
resolve(job_result);
clearTimeout(timer);
return;
} }
}); });
@ -53,6 +72,7 @@ function poll_jobs(data) {
if (++timesRun == maxRun) { if (++timesRun == maxRun) {
clearTimeout(timer); clearTimeout(timer);
reject("Job polling timed out"); reject("Job polling timed out");
return;
} else { } else {
timer = setTimeout(pollJob, 2000); timer = setTimeout(pollJob, 2000);
} }
@ -64,9 +84,20 @@ function poll_jobs(data) {
// Run .all() on promises array until all promises resolve // Run .all() on promises array until all promises resolve
Promise.all(promises).then(function(result) { Promise.all(promises).then(function(result) {
if(result.failed) { var success = true;
do_error(result.error);
} else { for(var i=0;i<result.length;i++) {
console.log('Job result: ', result[i]);
var r = result[i].result;
var m = result[i].meta;
if (r.failed && m.mandatory) {
do_error(r.error);
success = false;
break;
}
}
if (success) {
do_success(); do_success();
} }
}, function(reason) { }, function(reason) {
@ -79,7 +110,8 @@ $('#approveForm').submit(function (event) {
var api_url = '/approve'; var api_url = '/approve';
event.preventDefault(); event.preventDefault();
// Had some issues trying to set a background image on the button. // Had some issues trying to set a background image on the button, so I'm
// just replacing it.
if ($('#approveCheckbox').is(':checked')) { if ($('#approveCheckbox').is(':checked')) {
$('#approveButton').prop('disabled', true); $('#approveButton').prop('disabled', true);
$('#approveButton').val(''); $('#approveButton').val('');
@ -92,6 +124,7 @@ $('#approveForm').submit(function (event) {
ajaxReq.fail(function(XMLHttpRequest, textStatus, errorThrown) { ajaxReq.fail(function(XMLHttpRequest, textStatus, errorThrown) {
console.log('Request Error: '+ XMLHttpRequest.responseText + ', status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText); console.log('Request Error: '+ XMLHttpRequest.responseText + ', status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText);
do_error(XMLHttpRequest.responseText);
}); });
} }
}); });