2017-12-15 21:39:18 +00:00
|
|
|
import EmberObject, { computed } from '@ember/object';
|
2017-11-14 18:54:36 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import TextDecoder from 'nomad-ui/utils/classes/text-decoder';
|
2019-06-26 21:52:41 +00:00
|
|
|
import { decode } from 'nomad-ui/utils/stream-frames';
|
2017-11-21 01:09:28 +00:00
|
|
|
import AbstractLogger from './abstract-logger';
|
2018-02-26 20:23:01 +00:00
|
|
|
import { fetchFailure } from './log';
|
2020-06-11 21:23:00 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
@classic
|
|
|
|
export default class StreamLogger extends EmberObject.extend(AbstractLogger) {
|
|
|
|
reader = null;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2022-03-08 17:28:36 +00:00
|
|
|
get isSupported() {
|
|
|
|
return !!window.ReadableStream;
|
|
|
|
}
|
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
@computed()
|
|
|
|
get additionalParams() {
|
2020-06-09 21:03:28 +00:00
|
|
|
return {
|
|
|
|
follow: true,
|
|
|
|
};
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2017-11-21 01:09:28 +00:00
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
start() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.poll.perform();
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
stop() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const reader = this.reader;
|
2017-11-14 18:54:36 +00:00
|
|
|
if (reader) {
|
|
|
|
reader.cancel();
|
|
|
|
}
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.poll.cancelAll();
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2019-03-26 07:46:44 +00:00
|
|
|
const url = this.fullUrl;
|
|
|
|
const logFetch = this.logFetch;
|
2017-11-21 01:09:28 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
const reader = yield logFetch(url).then((res) => {
|
2019-12-05 08:17:14 +00:00
|
|
|
const reader = res.body.getReader();
|
|
|
|
// It's possible that the logger was stopped between the time
|
|
|
|
// polling was started and the log request responded.
|
|
|
|
// If the logger was stopped, the reader needs to be immediately
|
|
|
|
// canceled to prevent an endless request running in the background.
|
|
|
|
if (this.poll.isRunning) {
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
reader.cancel();
|
|
|
|
}, fetchFailure(url));
|
2018-02-26 20:23:01 +00:00
|
|
|
|
|
|
|
if (!reader) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 01:09:28 +00:00
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
this.set('reader', reader);
|
|
|
|
|
2019-12-05 18:51:51 +00:00
|
|
|
let streamClosed = false;
|
|
|
|
let buffer = '';
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
while (!streamClosed) {
|
|
|
|
yield reader.read().then(({ value, done }) => {
|
|
|
|
streamClosed = done;
|
2017-11-21 01:09:28 +00:00
|
|
|
|
|
|
|
// There is no guarantee that value will be a complete JSON object,
|
|
|
|
// so it needs to be buffered.
|
|
|
|
buffer += decoder.decode(value, { stream: true });
|
|
|
|
|
|
|
|
// Only when the buffer contains a close bracket can we be sure the buffer
|
|
|
|
// is in a complete state
|
|
|
|
if (buffer.indexOf('}') !== -1) {
|
|
|
|
// The buffer can be one or more complete frames with additional text for the
|
|
|
|
// next frame
|
|
|
|
const [, chunk, newBuffer] = buffer.match(/(.*\})(.*)$/);
|
|
|
|
|
|
|
|
// Peel chunk off the front of the buffer (since it represents complete frames)
|
|
|
|
// and set the buffer to be the remainder
|
|
|
|
buffer = newBuffer;
|
|
|
|
|
|
|
|
// Assuming the logs endpoint never returns nested JSON (it shouldn't), at this
|
|
|
|
// point chunk is a series of valid JSON objects with no delimiter.
|
2019-06-26 21:52:41 +00:00
|
|
|
const { offset, message } = decode(chunk);
|
|
|
|
if (message) {
|
|
|
|
this.set('endOffset', offset);
|
|
|
|
this.write(message);
|
2017-11-21 01:09:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
});
|
|
|
|
}
|
2020-06-11 21:23:00 +00:00
|
|
|
})
|
|
|
|
poll;
|
|
|
|
}
|