|
17 | 17 | </a-list-item> |
18 | 18 | <a-list-item v-for="(job, index) in jobs" :key="index"> |
19 | 19 | <a-list-item-meta :title="job.title" :description="job.description"> |
20 | | - <a-avatar :style="job.style" :icon="job.icon" slot="avatar"/> |
| 20 | + <a-avatar :style="notificationAvatar[job.status].style" :icon="notificationAvatar[job.status].icon" slot="avatar"/> |
21 | 21 | </a-list-item-meta> |
22 | 22 | </a-list-item> |
23 | 23 | </a-list> |
|
32 | 32 | </template> |
33 | 33 |
|
34 | 34 | <script> |
| 35 | +import { api } from '@/api' |
| 36 | +import store from '@/store' |
| 37 | +import { constants } from 'crypto'; |
| 38 | +
|
35 | 39 | export default { |
36 | 40 | name: 'HeaderNotice', |
37 | 41 | data () { |
38 | 42 | return { |
39 | 43 | loading: false, |
40 | 44 | visible: false, |
41 | | - jobs: [] |
| 45 | + jobs: [], |
| 46 | + poller: null, |
| 47 | + notificationAvatar: { |
| 48 | + 'done': { 'icon': 'check-circle', 'style': 'backgroundColor:#87d068' }, |
| 49 | + 'progress': { 'icon': 'loading', 'style': 'backgroundColor:#ffbf00' }, |
| 50 | + 'failed': { 'icon': 'close-circle', 'style': 'backgroundColor:#f56a00' } |
| 51 | + } |
42 | 52 | } |
43 | 53 | }, |
44 | 54 | methods: { |
45 | 55 | showNotifications () { |
46 | 56 | this.visible = !this.visible |
47 | | - this.jobs.push({ 'title': 'Start VM', description: 'VM Deployment', icon: 'check-circle', status: 'done', style: 'backgroundColor:#87d068' }) |
48 | | - this.jobs.push({ 'title': 'Start VM', description: 'VM Deployment', icon: 'loading', status: 'progress', style: 'backgroundColor:#ffbf00' }) |
49 | | - this.jobs.push({ 'title': 'Start VM', description: 'VM Deployment', icon: 'close-circle', status: 'failed', style: 'backgroundColor:#f56a00' }) |
50 | 57 | }, |
51 | 58 | clearJobs () { |
52 | 59 | this.visible = false |
53 | 60 | this.jobs = [] |
| 61 | + this.$store.commit('SET_ASYNC_JOB_IDS', []) |
| 62 | + }, |
| 63 | + startPolling() { |
| 64 | + this.poller = setInterval(() => { |
| 65 | + this.pollJobs() |
| 66 | + }, 2500) |
| 67 | + }, |
| 68 | + async pollJobs () { |
| 69 | + var hasUpdated = false |
| 70 | + for (var i in this.jobs) { |
| 71 | + if (this.jobs[i].status === 'progress') { |
| 72 | + await api('queryAsyncJobResult', {'jobid': this.jobs[i].jobid}).then(json => { |
| 73 | + var result = json.queryasyncjobresultresponse |
| 74 | + if (result.jobstatus === 1 && this.jobs[i].status !== 'done') { |
| 75 | + hasUpdated = true |
| 76 | + this.$notification['success']({ |
| 77 | + message: this.jobs[i].title, |
| 78 | + description: this.jobs[i].description |
| 79 | + }) |
| 80 | + this.jobs[i].status = 'done' |
| 81 | + } else if (result.jobstatus === 2 && this.jobs[i].status !== 'failed') { |
| 82 | + hasUpdated = true |
| 83 | + this.jobs[i].status = 'failed' |
| 84 | + if (result.jobresult.errortext !== null) { |
| 85 | + this.jobs[i].description = '(' + this.jobs[i].description + ') ' + result.jobresult.errortext |
| 86 | + } |
| 87 | + this.$notification['error']({ |
| 88 | + message: this.jobs[i].title, |
| 89 | + description: this.jobs[i].description |
| 90 | + }) |
| 91 | + } |
| 92 | + }).catch(function (e) { |
| 93 | + console.log('Error encountered while fetching async job result' + e) |
| 94 | + }) |
| 95 | + } |
| 96 | + } |
| 97 | + if (hasUpdated) { |
| 98 | + this.$store.commit('SET_ASYNC_JOB_IDS', this.jobs.reverse()) |
| 99 | + } |
54 | 100 | } |
| 101 | + }, |
| 102 | + beforeDestroy () { |
| 103 | + clearInterval(this.poller) |
| 104 | + }, |
| 105 | + created () { |
| 106 | + this.startPolling() |
| 107 | + }, |
| 108 | + mounted () { |
| 109 | + this.jobs = store.getters.asyncJobIds.reverse() |
| 110 | + this.$store.watch( |
| 111 | + (state, getters) => getters.asyncJobIds, |
| 112 | + (newValue, oldValue) => { |
| 113 | + if (oldValue !== newValue && newValue !== undefined) { |
| 114 | + this.jobs = newValue.reverse() |
| 115 | + } |
| 116 | + } |
| 117 | + ) |
55 | 118 | } |
56 | 119 | } |
57 | 120 | </script> |
|
0 commit comments