github.com/aminovpavel/nomad@v0.11.8/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  class MockAbortController {
     9    abort() {
    10      /* noop */
    11    }
    12  }
    13  
    14  export default Component.extend({
    15    token: service(),
    16  
    17    classNames: ['boxed-section', 'task-log'],
    18  
    19    allocation: null,
    20    task: null,
    21  
    22    // When true, request logs from the server agent
    23    useServer: false,
    24  
    25    // When true, logs cannot be fetched from either the client or the server
    26    noConnection: false,
    27  
    28    clientTimeout: 1000,
    29    serverTimeout: 5000,
    30  
    31    isStreaming: true,
    32    streamMode: 'streaming',
    33  
    34    mode: 'stdout',
    35  
    36    logUrl: computed('allocation.id', 'allocation.node.httpAddr', 'useServer', function() {
    37      const address = this.get('allocation.node.httpAddr');
    38      const allocation = this.get('allocation.id');
    39  
    40      const url = `/v1/client/fs/logs/${allocation}`;
    41      return this.useServer ? url : `//${address}${url}`;
    42    }),
    43  
    44    logParams: computed('task', 'mode', function() {
    45      return {
    46        task: this.task,
    47        type: this.mode,
    48      };
    49    }),
    50  
    51    logger: logger('logUrl', 'logParams', function logFetch() {
    52      // If the log request can't settle in one second, the client
    53      // must be unavailable and the server should be used instead
    54  
    55      // AbortControllers don't exist in IE11, so provide a mock if it doesn't exist
    56      const aborter = window.AbortController ? new AbortController() : new MockAbortController();
    57      const timing = this.useServer ? this.serverTimeout : this.clientTimeout;
    58  
    59      // Capture the state of useServer at logger create time to avoid a race
    60      // between the stdout logger and stderr logger running at once.
    61      const useServer = this.useServer;
    62      return url =>
    63        RSVP.race([
    64          this.token.authorizedRequest(url, { signal: aborter.signal }),
    65          timeout(timing),
    66        ]).then(
    67          response => {
    68            return response;
    69          },
    70          error => {
    71            aborter.abort();
    72            if (useServer) {
    73              this.set('noConnection', true);
    74            } else {
    75              this.send('failoverToServer');
    76            }
    77            throw error;
    78          }
    79        );
    80    }),
    81  
    82    actions: {
    83      setMode(mode) {
    84        if (this.mode === mode) return;
    85        this.logger.stop();
    86        this.set('mode', mode);
    87      },
    88      toggleStream() {
    89        this.set('streamMode', 'streaming');
    90        this.toggleProperty('isStreaming');
    91      },
    92      gotoHead() {
    93        this.set('streamMode', 'head');
    94        this.set('isStreaming', false);
    95      },
    96      gotoTail() {
    97        this.set('streamMode', 'tail');
    98        this.set('isStreaming', false);
    99      },
   100      failoverToServer() {
   101        this.set('useServer', true);
   102      },
   103    },
   104  });