summaryrefslogtreecommitdiffstats
path: root/script/taskmanager.js
blob: ae1d2f09f08d0edae1a5ff1ee1e97c1ed8fa6772 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var tmItems = false;
var tmErrors = 0;
var TM_MAX_ERRORS = 5;
var tmIsRunning = false;

function tmInit()
{
	tmItems = $("div[data-tm-id]");
	if (tmItems.length === 0)
		return;
	tmItems.each(function(i, item) {
		item = $(item);
		if (item.find('.data-tm-icon').length !== 0)
			return;
		if (item.is('[data-tm-progress]')) {
			item.append('<div class="data-tm-progress"><div class="progress"><div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%"></div></div></div>');
		}
		if (item.is('[data-tm-log]')) {
			var lh = item.data('tm-log-height');
			if (!lh) {
				lh = '';
			} else {
				lh = 'min-height:' + lh;
			}
			item.append('<pre class="data-tm-log collapse" style="' + lh + '" />');
		}
		item.prepend('<span class="data-tm-icon" />');
	});
	if (!tmIsRunning) {
		setTimeout(tmUpdate, 50);
	}
	tmIsRunning = true;
}

function tmUpdate()
{
	var active = [];
	tmItems.each(function(i, item) {
		item = $(item);
		var id = item.attr('data-tm-id');
		if (typeof id === 'undefined' || id === false || id === '')
			return;
		active.push(id);
	});
	if (active.length === 0) {
		tmIsRunning = false;
		return;
	}
	$.post('api.php?do=taskmanager', {'ids[]': active, token: TOKEN}, function(data, status) {
		// POST success
		tmIsRunning = tmResult(data, status);
		if (tmIsRunning) {
			setTimeout(tmUpdate, 1000);
		}
	}, 'json').fail(function(jqXHR, textStatus, errorThrown) {
		// POST failure
		console.log("TaskManager Error: " + textStatus + " - " + errorThrown);
		tmIsRunning = (++tmErrors < TM_MAX_ERRORS);
		if (tmIsRunning) {
			setTimeout(tmUpdate, 3000);
		}
	});
}

function tmResult(data, status)
{
	// Bail out on error
	if (typeof data.error !== 'undefined') {
		$('#mainpage').prepend($('<div class="alert alert-danger"/>').append(document.createTextNode(data.error)));
		console.log("Error ERROR");
		return false;
	}
	// No task list is also bad
	if (typeof data.tasks === 'undefined') {
		$('#mainpage').prepend('<div class="alert alert-danger">Internal Error #67452</div>');
		console.log("Error UNEXPECTED");
		return false;
	}
	var lastRunOnError = (tmErrors > TM_MAX_ERRORS);
	// Let's continue handling stuff
	var counter = 0;
	for (var idx in data.tasks) {
		var task = data.tasks[idx];
		if (!task.id)
			continue;
		counter++;
		if (lastRunOnError) {
			task.statusCode = 'TASK_ERROR';
		} else if (task.error) {
			++tmErrors;
			continue;
		} else if (tmErrors > 0) {
			--tmErrors;
		}
		var obj = $('div[data-tm-id="' + task.id + '"]');
		if (!obj)
			continue;
		if (task.statusCode !== 'TASK_WAITING' && task.statusCode !== 'TASK_PROCESSING') {
			obj.attr('data-tm-id', '');
		}
		var icon = obj.find('.data-tm-icon');
		if (icon) {
			if (typeof task.statusCode === 'undefined') {
				icon.attr('class', 'data-tm-icon glyphicon glyphicon-question-sign');
			} else if (task.statusCode === 'TASK_WAITING') {
				icon.attr('class', 'data-tm-icon glyphicon glyphicon-pause');
			} else if (task.statusCode === 'TASK_PROCESSING') {
				icon.attr('class', 'data-tm-icon glyphicon glyphicon-refresh slx-rotation');
			} else if (task.statusCode === 'TASK_FINISHED') {
				icon.attr('class', 'data-tm-icon glyphicon glyphicon-ok');
			} else if (task.statusCode === 'TASK_ERROR') {
				icon.attr('class', 'data-tm-icon glyphicon glyphicon-remove');
			} else {
				icon.attr('class', 'data-tm-icon glyphicon glyphicon-trash');
			}
		} else {
			console.log('Icon for ' + obj + ': ' + icon);
		}
		var progress = obj.find('.data-tm-progress');
		if (progress) {
			var pKey = obj.attr('data-tm-progress');
			if (task.data && task.data[pKey]) {
				tmSetProgress(progress, task.data[pKey], task.statusCode);
			} else {
				tmSetProgress(progress, false, task.statusCode);
			}
		}
		var log = obj.find('.data-tm-log');
		if (log) {
			var lKey = obj.attr('data-tm-log');
			if (task.data && task.data[lKey]) {
				log.text(task.data[lKey]).show();
			}
		}
		var cb = obj.attr('data-tm-callback');
		if (cb && window[cb]) {
			window[cb](task);
		}
	}
	if (lastRunOnError) {
		$('#mainpage').prepend($('<div class="alert alert-danger"/>').append(document.createTextNode(task.error)));
		return false;
	}
	return counter > 0;
}

function tmSetProgress(elem, percent, status)
{
	var outer = '', inner = '';
	if (status === 'TASK_PROCESSING') {
		outer = ' active';
	} else if (status === 'TASK_ERROR') {
		inner = ' progress-bar-danger';
	} else if (status === 'TASK_FINISHED') {
		inner = ' progress-bar-success';
	}
	elem.find('.progress').attr('class', 'progress progress-striped' + outer);
	var bar = elem.find('.progress-bar');
	bar.attr('class', 'progress-bar' + inner);
	if (percent !== false) {
		bar.attr('aria-valuenow', percent);
		bar.attr('style', 'width: ' + percent + '%');
	}
}

tmInit();