2017-12-15 21:39:18 +00:00
|
|
|
import EmberObject from '@ember/object';
|
2017-11-14 18:54:36 +00:00
|
|
|
import { task, timeout } from 'ember-concurrency';
|
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-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
export default class PollLogger extends EmberObject.extend(AbstractLogger) {
|
|
|
|
interval = 1000;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
start() {
|
2019-06-26 21:52:41 +00:00
|
|
|
return this.poll.linked().perform();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
stop() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.poll.cancelAll();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@task(function*() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const { interval, logFetch } = this;
|
2017-11-14 18:54:36 +00:00
|
|
|
while (true) {
|
2019-03-26 07:46:44 +00:00
|
|
|
const url = this.fullUrl;
|
2018-02-26 20:23:01 +00:00
|
|
|
let response = yield logFetch(url).then(res => res, fetchFailure(url));
|
|
|
|
|
|
|
|
if (!response) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let text = yield response.text();
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2017-11-18 04:06:37 +00:00
|
|
|
if (text) {
|
2019-06-26 21:52:41 +00:00
|
|
|
const { offset, message } = decode(text);
|
|
|
|
if (message) {
|
|
|
|
this.set('endOffset', offset);
|
|
|
|
this.write(message);
|
2018-05-24 22:46:55 +00:00
|
|
|
}
|
2017-11-18 04:06:37 +00:00
|
|
|
}
|
2017-11-17 00:31:18 +00:00
|
|
|
|
|
|
|
yield timeout(interval);
|
2017-11-14 18:54:36 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
poll;
|
|
|
|
}
|