This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
celery.js
74 lines (67 loc) · 1.89 KB
/
celery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
jQuery.fn.djcelery = function(options) {
var interval_id;
var el = this;
var url = '/task/' + options.task_id + '/status/';
var number_of_errors = 0;
if (options.task_id.length !== 36) {
url = '';
}
if (typeof(options.on_failure) !== 'function') {
options.on_failure = function(){};
}
if (typeof(options.on_error) !== 'function') {
options.on_error = function(){};
}
if (typeof(options.on_other) !== 'function') {
options.on_other = function(){};
}
function handle_status(data) {
if (data === null){
return;
}
var task = data.task;
if (task === null) {
return;
}
if (task.status == 'PENDING') {
return;
}
if (task.status == 'SUCCESS') {
clearInterval(interval_id);
options.on_success(task, el);
}
else if (task.status == 'FAILURE' ) {
clearInterval(interval_id);
options.on_failure(task, el);
} else {
options.on_other(task, el);
}
}
function handle_error() {
++number_of_errors;
// Wait after first error, just in case there is a timing issue
if (number_of_errors >= 2) {
clearInterval(interval_id);
options.on_error(null, el);
}
}
function check_status() {
$.ajax({
url: url,
data: {},
success: handle_status,
cache: false, // append a timestamp to the end of the URL
dataType: 'json',
error: handle_error
});
}
$(document).ready(function(){
if (url !== '') {
setTimeout(check_status, 0);
interval_id = setInterval(check_status, options.check_interval);
} else {
number_of_errors = 3;
handle_error();
}
});
};