forked from Stii/nodervisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax_supervisorlog.js
86 lines (80 loc) · 2.63 KB
/
ajax_supervisorlog.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
75
76
77
78
79
80
81
82
83
84
85
86
/*
* GET downloadctl page.
*/
exports.ajax_supervisorlog = function(params) {
var config = params.config;
var supervisordapi = params.supervisordapi;
return function(req, res) {
if (!req.session.loggedIn) {
res.send({error: 'Not logged in'});
} else {
var host = req.param('host');
var process = req.param('process');
var offset = parseInt(req.param('offset'), 10);
var length = 16384;
if (config.hosts[host] !== undefined) {
var supclient = supervisordapi.connect(config.hosts[host].Url);
switch (req.param('type')) {
case 'out': {
if (offset === 0) {
// If we dont know the offset to start, lets do two calls to fetch the current logsize and then offset
supclient.tailProcessStdoutLog(process, 0, 0, function(err, data){
if (!err) {
offset = data[1] - length;
supclient.tailProcessStdoutLog(process, offset, length, function(err, data){
res.send({result: 'success', data: data});
});
} else {
res.send({result: 'error', error: err});
}
});
} else {
supclient.tailProcessStdoutLog(process, offset, length, function(err, data){
if (!err) {
// For some reason it doesnt use the length properly, so trim it to expected length now
length = data[1] - offset;
var log = data[0];
data[0] = log.substr(length * -1);
res.send({result: 'success', data: data});
} else {
res.send({result: 'error', error: err});
}
});
}
}
break;
case 'err': {
if (offset === 0) {
supclient.tailProcessStderrLog(process, 0, 0, function(err, data){
if (!err) {
// If we dont know the offset to start, lets do two calls to fetch the current logsize and then offset
offset = data[1] - length;
supclient.tailProcessStderrLog(process, offset, length, function(err, data){
res.send({result: 'success', data: data});
});
} else {
res.send({result: 'error', error: err});
}
});
} else {
supclient.tailProcessStderrLog(process, offset, length, function(err, data){
if (!err) {
// For some reason it doesnt use the length properly, so trim it to expected length now
length = data[1] - offset;
var log = data[0];
data[0] = log.substr(length * -1);
res.send({result: 'success', data: data});
} else {
res.send({result: 'error', error: err});
}
});
}
}
break;
}
} else {
res.send({result: 'error', message: 'Host not found'});
}
}
};
};