cleanups and lowered timeout values

This commit is contained in:
Stefan Midjich 2017-11-15 23:03:45 +01:00
parent ebc9d160e5
commit 0397f806fc

View file

@ -32,7 +32,7 @@ function do_success() {
if (!url.startsWith('http')) {
url = 'http://' + url;
}
console.log('success: ' + url);
//console.log('success: ' + url);
$('#error-box').html('<p>If you\'re not automatically redirected <a href="https://www.google.com/">click here</a>.</p>');
$('#error-box').show();
$('#statusDiv').html('');
@ -51,8 +51,7 @@ function do_error(message) {
$('#error-box').show();
$('#error-box').html('<p>Failed. Reload page and try again or contact support.</p> ');
if (message) {
console.log('server: ' + message);
$('#error-box').append('<p>System response: ' + message + '</p>');
//console.log(message);
}
}
@ -62,19 +61,21 @@ function poll_jobs(data) {
var promises = [];
if (debug) {
console.log('Jobs data: ', data);
//console.log('Jobs data: ', data);
}
// Push promises into array
// Push promises into array, one for each job returned from POST /approve
for (var job in data) {
var job_id = data[job].id;
var api_url = '/job/' + job_id;
if (debug) {
console.log('Processing job: ', data[job]);
//console.log('Processing job: ', data[job]);
}
// Each promise will poll the job ID for status
promises.push(new Promise(function(resolve, reject) {
var plugin_timeout = 30;
var maxRun = plugin_timeout / 2;
var timesRun = 0;
@ -86,27 +87,33 @@ function poll_jobs(data) {
var job_result = getResponse;
if (debug) {
console.log('Job results: ', job_result);
//console.log(`Job result: ${job_result}`);
}
console.log(job_result);
//console.log(job_result);
if (job_result.is_finished) {
console.log('Resolving job: ', job_result);
console.log(`Resolved job: ${job_result.id}`);
resolve(job_result);
clearTimeout(timer);
return (true);
}
if (job_result.is_failed) {
console.log('Job failed: ', job_result);
if (job_result.is_failed && job_result.meta.mandatory) {
console.log(`Job failed: ${job_result.id}`);
reject(job_result);
clearTimeout(timer);
return (false);
}
if (job_result.is_failed && !job_result.meta.mandatory) {
console.log(`Resolved non-mandatory failed job: ${job_result.id}`);
resolve(job_result);
clearTimeout(timer);
return (true);
}
});
ajaxReq.fail(function(XMLHttpRequest, textStatus, errorThrown) {
console.log('Request Error: ' + XMLHttpRequest.responseText + ', status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText);
reject(XMLHttpRequest.responseText);
});
@ -116,11 +123,12 @@ function poll_jobs(data) {
reject("Job polling timed out");
return;
} else {
timer = setTimeout(pollJob, 2000);
timer = setTimeout(pollJob, 1500);
}
};
var timer = setTimeout(pollJob, 500);
// Here I just want the pollJob function to run immediately
var timer = setTimeout(pollJob, 200);
}));
}
@ -129,24 +137,12 @@ function poll_jobs(data) {
Promise.all(promises).then(function(result) {
var success = true;
for (var i = 0; i < result.length; i++) {
var r = result[i].result;
var meta = result[i].meta;
if (meta.mandatory) {
if (result[i].is_finished && result[i].is_failed) {
do_error(r.error);
success = false;
break;
}
}
}
console.log(result);
if (success) {
// This is for Steve...
// Apple devices don't poll their captiveportal URL,
// so this is for them. Android devices will do their
// own polling and close the wifi-portal before this.
setTimeout(do_success, 1000);
if (success === true) {
// This timeout might be important if device wifi prompts seem to
// hang on the portal without redirecting on their own.
setTimeout(do_success, 200);
}
// This is reject() above.
@ -170,9 +166,8 @@ $('#approveForm').submit(function(event) {
ajaxReq.done(poll_jobs);
ajaxReq.fail(function(XMLHttpRequest, textStatus, errorThrown) {
console.log('Request Error: ' + XMLHttpRequest.responseText + ', status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText);
do_error(XMLHttpRequest.responseText);
});
}
});
});