From 2dff7d31c5cb63417c505fed2da38907c8f4c7ba Mon Sep 17 00:00:00 2001 From: antoinefa Date: Mon, 30 Apr 2018 22:06:32 +0200 Subject: [PATCH] log stream api port into jobs vue --- src/components/Jobs.vue | 56 ++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/components/Jobs.vue b/src/components/Jobs.vue index e950adc..7cfd2e4 100644 --- a/src/components/Jobs.vue +++ b/src/components/Jobs.vue @@ -128,6 +128,7 @@ export default { }, data () { return { + evtSource: null, runningJobs: {}, doneJobs: {}, log: null, @@ -150,19 +151,10 @@ export default { this.getLog(this.$route.params.job) } }, - mounted () { + created () { this.getJobs() - - window.bus.$on('updateJobs', v => { - this.runningJobs = v.running - this.doneJobs = v.done - }) - - if (this.$route.name === 'job') { - this.interval.jobs = setInterval(() => { - this.getLog(this.$route.params.job) - }, 5000) - } + }, + mounted () { }, computed: { filteredLog () { @@ -195,17 +187,41 @@ export default { .then(response => { this.runningJobs = response.body.running this.doneJobs = response.body.done + if (this.$route.name === 'job') { + this.getLog(this.$route.params.job) + } }) }, getLog (recipe) { - this.$http.get(this.apiUrl + 'recipes/' + recipe + '/log') - .then(response => { - let arr = response.body.split('\n') - if ((this.log === null) || (arr.length !== this.log.length)) { - this.log = arr - this.setPageCurrent(Math.ceil(this.log.length / this.pageSize)) - } - }) + if (this.runningJobs.some(r => { return r.recipe === recipe })) { + this.getRunningLog(recipe) + } else { + this.$http.get(this.apiUrl + 'recipes/' + recipe + '/log') + .then(response => { + let arr = response.body.split('\n') + if ((this.log === null) || (arr.length !== this.log.length)) { + this.log = arr + this.setPageCurrent(Math.ceil(this.log.length / this.pageSize)) + } + }) + if (this.evtSource !== null) { + this.evtSource.close() + } + } + }, + getRunningLog (recipe) { + this.evtSource = new EventSource(this.apiUrl + 'recipes/' + recipe + '/log') + let that = this + this.evtSource.addEventListener('open', function (e) { + that.log = [] + }, false) + this.evtSource.addEventListener('message', function (e) { + that.log = that.log.concat(e.data.split('\n')) + that.setPageCurrent(Math.ceil(that.log.length / that.pageSize)) + }, false) + this.evtSource.addEventListener('close', function (e) { + that.evtSource.close() + }, false) } } }