-
Notifications
You must be signed in to change notification settings - Fork 2k
/
task-log.js
84 lines (71 loc) · 2.17 KB
/
task-log.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
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { computed } from '@ember/object';
import RSVP from 'rsvp';
import { logger } from 'nomad-ui/utils/classes/log';
import timeout from 'nomad-ui/utils/timeout';
export default Component.extend({
token: service(),
classNames: ['boxed-section', 'task-log'],
allocation: null,
task: null,
// When true, request logs from the server agent
useServer: false,
// When true, logs cannot be fetched from either the client or the server
noConnection: false,
clientTimeout: 1000,
serverTimeout: 5000,
isStreaming: true,
streamMode: 'streaming',
mode: 'stdout',
logUrl: computed('allocation.id', 'allocation.node.httpAddr', 'useServer', function() {
const address = this.get('allocation.node.httpAddr');
const allocation = this.get('allocation.id');
const url = `/v1/client/fs/logs/${allocation}`;
return this.useServer ? url : `//${address}${url}`;
}),
logParams: computed('task', 'mode', function() {
return {
task: this.task,
type: this.mode,
};
}),
logger: logger('logUrl', 'logParams', function logFetch() {
// If the log request can't settle in one second, the client
// must be unavailable and the server should be used instead
const timing = this.useServer ? this.serverTimeout : this.clientTimeout;
return url =>
RSVP.race([this.token.authorizedRequest(url), timeout(timing)]).then(
response => response,
error => {
if (this.useServer) {
this.set('noConnection', true);
} else {
this.send('failoverToServer');
}
throw error;
}
);
}),
actions: {
setMode(mode) {
this.logger.stop();
this.set('mode', mode);
},
toggleStream() {
this.set('streamMode', 'streaming');
this.toggleProperty('isStreaming');
},
gotoHead() {
this.set('streamMode', 'head');
this.set('isStreaming', false);
},
gotoTail() {
this.set('streamMode', 'tail');
this.set('isStreaming', false);
},
failoverToServer() {
this.set('useServer', true);
},
},
});