github.com/manicqin/nomad@v0.9.5/ui/app/components/task-log.js (about) 1 import { inject as service } from '@ember/service'; 2 import Component from '@ember/component'; 3 import { computed } from '@ember/object'; 4 import RSVP from 'rsvp'; 5 import { logger } from 'nomad-ui/utils/classes/log'; 6 import timeout from 'nomad-ui/utils/timeout'; 7 8 export default Component.extend({ 9 token: service(), 10 11 classNames: ['boxed-section', 'task-log'], 12 13 allocation: null, 14 task: null, 15 16 // When true, request logs from the server agent 17 useServer: false, 18 19 // When true, logs cannot be fetched from either the client or the server 20 noConnection: false, 21 22 clientTimeout: 1000, 23 serverTimeout: 5000, 24 25 isStreaming: true, 26 streamMode: 'streaming', 27 28 mode: 'stdout', 29 30 logUrl: computed('allocation.id', 'allocation.node.httpAddr', 'useServer', function() { 31 const address = this.get('allocation.node.httpAddr'); 32 const allocation = this.get('allocation.id'); 33 34 const url = `/v1/client/fs/logs/${allocation}`; 35 return this.useServer ? url : `//${address}${url}`; 36 }), 37 38 logParams: computed('task', 'mode', function() { 39 return { 40 task: this.task, 41 type: this.mode, 42 }; 43 }), 44 45 logger: logger('logUrl', 'logParams', function logFetch() { 46 // If the log request can't settle in one second, the client 47 // must be unavailable and the server should be used instead 48 const timing = this.useServer ? this.serverTimeout : this.clientTimeout; 49 return url => 50 RSVP.race([this.token.authorizedRequest(url), timeout(timing)]).then( 51 response => response, 52 error => { 53 if (this.useServer) { 54 this.set('noConnection', true); 55 } else { 56 this.send('failoverToServer'); 57 } 58 throw error; 59 } 60 ); 61 }), 62 63 actions: { 64 setMode(mode) { 65 this.logger.stop(); 66 this.set('mode', mode); 67 }, 68 toggleStream() { 69 this.set('streamMode', 'streaming'); 70 this.toggleProperty('isStreaming'); 71 }, 72 gotoHead() { 73 this.set('streamMode', 'head'); 74 this.set('isStreaming', false); 75 }, 76 gotoTail() { 77 this.set('streamMode', 'tail'); 78 this.set('isStreaming', false); 79 }, 80 failoverToServer() { 81 this.set('useServer', true); 82 }, 83 }, 84 });