open-nomad/ui/app/utils/classes/poll-logger.js

45 lines
1,014 B
JavaScript
Raw Normal View History

import EmberObject from '@ember/object';
import { task, timeout } from 'ember-concurrency';
import { decode } from 'nomad-ui/utils/stream-frames';
import AbstractLogger from './abstract-logger';
import { fetchFailure } from './log';
import classic from 'ember-classic-decorator';
@classic
export default class PollLogger extends EmberObject.extend(AbstractLogger) {
interval = 1000;
start() {
return this.poll.linked().perform();
}
stop() {
2019-03-26 07:46:44 +00:00
return this.poll.cancelAll();
}
2021-12-28 14:45:20 +00:00
@task(function* () {
2019-03-26 07:46:44 +00:00
const { interval, logFetch } = this;
while (true) {
2019-03-26 07:46:44 +00:00
const url = this.fullUrl;
2021-12-28 14:45:20 +00:00
let response = yield logFetch(url).then((res) => res, fetchFailure(url));
if (!response) {
return;
}
let text = yield response.text();
if (text) {
const { offset, message } = decode(text);
if (message) {
this.set('endOffset', offset);
this.write(message);
}
}
yield timeout(interval);
}
})
poll;
}