open-nomad/ui/app/components/task-log.js
Buck Doyle 89136cbf6a Add massaged results of class codemod
Manual interventions:
• decorators on the same line for service and controller
  injections and most computed property macros
• preserving import order when possible, both per-line
  and intra-line
• moving new imports to the bottom
• removal of classic decorator for trivial cases
• conversion of init to constructor when appropriate
2020-06-10 16:18:42 -05:00

111 lines
2.7 KiB
JavaScript

import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { action, computed } from '@ember/object';
import RSVP from 'rsvp';
import { logger } from 'nomad-ui/utils/classes/log';
import timeout from 'nomad-ui/utils/timeout';
import { AbortController } from 'fetch';
import { classNames } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
@classic
@classNames('boxed-section', 'task-log')
export default class TaskLog extends Component {
@service token;
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';
@computed('allocation.{id,node.httpAddr}', 'useServer')
get logUrl() {
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}`;
}
@computed('task', 'mode')
get logParams() {
return {
task: this.task,
type: this.mode,
};
}
@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 aborter = new AbortController();
const timing = this.useServer ? this.serverTimeout : this.clientTimeout;
// Capture the state of useServer at logger create time to avoid a race
// between the stdout logger and stderr logger running at once.
const useServer = this.useServer;
return url =>
RSVP.race([
this.token.authorizedRequest(url, { signal: aborter.signal }),
timeout(timing),
]).then(
response => {
return response;
},
error => {
aborter.abort();
if (useServer) {
this.set('noConnection', true);
} else {
this.send('failoverToServer');
}
throw error;
}
);
})
logger;
@action
setMode(mode) {
if (this.mode === mode) return;
this.logger.stop();
this.set('mode', mode);
}
@action
toggleStream() {
this.set('streamMode', 'streaming');
this.toggleProperty('isStreaming');
}
@action
gotoHead() {
this.set('streamMode', 'head');
this.set('isStreaming', false);
}
@action
gotoTail() {
this.set('streamMode', 'tail');
this.set('isStreaming', false);
}
@action
failoverToServer() {
this.set('useServer', true);
}
}