github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/components/task-log.js (about) 1 import { inject as service } from '@ember/service'; 2 import Component from '@ember/component'; 3 import { action, computed } from '@ember/object'; 4 import { alias } from '@ember/object/computed'; 5 import RSVP from 'rsvp'; 6 import { logger } from 'nomad-ui/utils/classes/log'; 7 import timeout from 'nomad-ui/utils/timeout'; 8 import { classNames } from '@ember-decorators/component'; 9 import classic from 'ember-classic-decorator'; 10 11 class MockAbortController { 12 abort() { 13 /* noop */ 14 } 15 } 16 17 @classic 18 @classNames('boxed-section', 'task-log') 19 export default class TaskLog extends Component { 20 @service token; 21 @service userSettings; 22 23 allocation = null; 24 task = null; 25 26 // When true, request logs from the server agent 27 useServer = false; 28 29 // When true, logs cannot be fetched from either the client or the server 30 noConnection = false; 31 32 clientTimeout = 1000; 33 serverTimeout = 5000; 34 35 isStreaming = true; 36 streamMode = 'streaming'; 37 38 @alias('userSettings.logMode') mode; 39 40 @computed('allocation.{id,node.httpAddr}', 'useServer') 41 get logUrl() { 42 const address = this.get('allocation.node.httpAddr'); 43 const allocation = this.get('allocation.id'); 44 45 const url = `/v1/client/fs/logs/${allocation}`; 46 return this.useServer ? url : `//${address}${url}`; 47 } 48 49 @computed('task', 'mode') 50 get logParams() { 51 return { 52 task: this.task, 53 type: this.mode, 54 }; 55 } 56 57 @logger('logUrl', 'logParams', function logFetch() { 58 // If the log request can't settle in one second, the client 59 // must be unavailable and the server should be used instead 60 61 const aborter = window.AbortController ? new AbortController() : new MockAbortController(); 62 const timing = this.useServer ? this.serverTimeout : this.clientTimeout; 63 64 // Capture the state of useServer at logger create time to avoid a race 65 // between the stdout logger and stderr logger running at once. 66 const useServer = this.useServer; 67 return url => 68 RSVP.race([ 69 this.token.authorizedRequest(url, { signal: aborter.signal }), 70 timeout(timing), 71 ]).then( 72 response => { 73 return response; 74 }, 75 error => { 76 aborter.abort(); 77 if (useServer) { 78 this.set('noConnection', true); 79 } else { 80 this.send('failoverToServer'); 81 } 82 throw error; 83 } 84 ); 85 }) 86 logger; 87 88 @action 89 setMode(mode) { 90 if (this.mode === mode) return; 91 this.logger.stop(); 92 this.set('mode', mode); 93 } 94 95 @action 96 toggleStream() { 97 this.set('streamMode', 'streaming'); 98 this.toggleProperty('isStreaming'); 99 } 100 101 @action 102 gotoHead() { 103 this.set('streamMode', 'head'); 104 this.set('isStreaming', false); 105 } 106 107 @action 108 gotoTail() { 109 this.set('streamMode', 'tail'); 110 this.set('isStreaming', false); 111 } 112 113 @action 114 failoverToServer() { 115 this.set('useServer', true); 116 } 117 }