Skip to content

Instantly share code, notes, and snippets.

@DylanFM
Forked from benaskins/snippet.js
Created October 28, 2009 03:55
Show Gist options
  • Save DylanFM/220219 to your computer and use it in GitHub Desktop.
Save DylanFM/220219 to your computer and use it in GitHub Desktop.
var GAImporter = function() {
this.busy = false;
this.poller = setInterval((function(that) {
return function() {
that.pollJob();
};
})(this), 5000);
this.pollJob = function() {
if (this.busy === false) {
this.busy = true;
this.checkJob();
}
};
this.checkJob = function() {
// Check job state
$.getJSON('/admin/play_golf_offers/check_job/', {
play_golf_offer_spreadsheet_id: id,
t: (new Date()).getTime()
},
(function(that) {
return function(json){
// If the job isn't running anymore
if (json.state !== "running" && json.state !== "pending") {
// Stop polling
clearInterval(that.poller);
}
// Show status
that.showJobStatus(json);
// No longer busy
that.busy = false;
};
})(this));
};
this.showJobStatus = function(json) {
var activity = $("#activity"),
html = "";
switch(json.state) {
case "running":
html += '<h2><img src="/images/spinner.gif" alt="" width="16" height="16" /> Processing data&hellip;</h2>';
break;
case "pending":
html += '<h2><img src="/images/spinner.gif" alt="" width="16" height="16" /> Import will begin shortly&hellip;</h2>';
break;
case "failed":
html += '<h2><img src="/images/exclamation.png" alt="" width="16" height="16" /> Import failed</h2>\
<p>' + json.message + '</p>\
<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
break;
case "finished":
html += '<h2><img src="/images/tick.png" alt="" width="16" height="16" /> Finished processing data</h2>\
<p>' + json.message + '</p>\
<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
break;
default:
html += '<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
}
// Append the update
$(activity).html(html);
// Log for more info's sake
console.log(json);
};
};
$(function() {
var importer = new GAImporter();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment