Skip to content

Commit

Permalink
core: frontend: improve logic for checking if backend is online in ve…
Browse files Browse the repository at this point in the history
…rsionchooser
  • Loading branch information
Williangalvani authored and patrickelectric committed Mar 25, 2022
1 parent 43cc6d1 commit 1ecbb77
Showing 1 changed file with 71 additions and 19 deletions.
90 changes: 71 additions & 19 deletions core/frontend/src/components/version-chooser/VersionChooser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,77 @@ export default Vue.extend({
this.loadCurrentVersion()
},
methods: {
async checkIfBackendIsOnline() {
this.waiting = true
await back_axios({
method: 'get',
url: '/version-chooser/v1.0/version/current',
timeout: 500,
})
.then(() => {
if (this.waiting) {
// Allow 3 seconds so the user can read the "complete" message
// reload(true) forces the browser to fetch the page again
setTimeout(() => { window.location.reload(true) }, 3000)
}
})
.catch((error) => {
console.log(error)
this.waiting = true
backendIsOnline() {
return new Promise((resolve) => {
back_axios({
method: 'get',
url: '/version-chooser/v1.0/version/current',
timeout: 500,
})
.then(() => {
resolve(true)
})
.catch(() => {
resolve(false)
})
})
},
async waitForBackendToGoOffline() {
let timeout = 0
let interval = 0
return new Promise((resolve, reject) => {
timeout = setTimeout(
() => {
reject(new Error('backend took to long to shutdown!'))
clearInterval(interval)
},
20000,
)
interval = setInterval(() => {
this.backendIsOnline().then((backend_online) => {
if (!backend_online) {
clearTimeout(timeout)
clearInterval(interval)
resolve('backend went offline')
}
})
}, 1000)
})
},
waitForBackendToGoOnline() {
let timeout = 0
let interval = 0
return new Promise((resolve, reject) => {
timeout = setTimeout(
() => {
reject(new Error('backend took to long to come back'))
clearInterval(interval)
},
20000,
)
interval = setInterval(() => {
this.backendIsOnline().then((backend_online) => {
if (backend_online) {
clearTimeout(timeout)
clearInterval(interval)
resolve('backend went online')
}
})
}, 1000)
})
},
async waitForBackendToRestart(reload: boolean) {
this.waiting = true
await this.waitForBackendToGoOffline()
await this.waitForBackendToGoOnline()
if (reload) {
window.location.reload()
}
},
runningBeta() {
return VCU.getVersionType(this.current_version) === VersionType.Beta
},
Expand Down Expand Up @@ -401,7 +453,7 @@ export default Vue.extend({
// Force it to true again in case the user tried to close the dialog
this.show_pull_output = true
},
}).then(() => setInterval(this.checkIfBackendIsOnline, 1000))
}).then(() => { this.waitForBackendToRestart(true) })
},
async setVersion(args: string | string[]) {
const fullname: string = Array.isArray(args) ? args[0] : args
Expand All @@ -413,7 +465,7 @@ export default Vue.extend({
repository,
tag,
},
}).finally(() => setInterval(this.checkIfBackendIsOnline, 1000))
}).finally(() => { this.waitForBackendToRestart(true) })
},
async deleteVersion(args: string | string[]) {
const fullname: string = Array.isArray(args) ? args[0] : args
Expand Down

0 comments on commit 1ecbb77

Please sign in to comment.