Add timeout to loading the cache server status page, avoids long (>1minute) loading time if the server hangs

This commit is contained in:
Pieter Vander Vennet 2024-03-28 15:53:41 +01:00
parent 2b9ccd4b34
commit 1c03e7ab07

View file

@ -20,16 +20,26 @@ function webgl_support() {
return false return false
} }
} }
async function timeout(timeMS: number): Promise<{ layers: string[] }> {
await Utils.waitFor(timeMS)
return { layers: [] }
}
async function getAvailableLayers(): Promise<Set<string>> { async function getAvailableLayers(): Promise<Set<string>> {
try { try {
const host = new URL(Constants.VectorTileServer).host const host = new URL(Constants.VectorTileServer).host
const status = await Utils.downloadJson("https://" + host + "/summary/status.json") const status: { layers: string[] } = await Promise.any([
Utils.downloadJson("https://" + host + "/summary/status.json"),
timeout(5000)
])
return new Set<string>(status.layers) return new Set<string>(status.layers)
} catch (e) { } catch (e) {
console.error("Could not get MVT available layers due to", e) console.error("Could not get MVT available layers due to", e)
return new Set<string>() return new Set<string>()
} }
} }
async function main() { async function main() {
// @ts-ignore // @ts-ignore
try { try {
@ -38,7 +48,7 @@ async function main() {
} }
const [layout, availableLayers] = await Promise.all([ const [layout, availableLayers] = await Promise.all([
DetermineLayout.GetLayout(), DetermineLayout.GetLayout(),
await getAvailableLayers(), await getAvailableLayers()
]) ])
console.log("The available layers on server are", Array.from(availableLayers)) console.log("The available layers on server are", Array.from(availableLayers))
const state = new ThemeViewState(layout, availableLayers) const state = new ThemeViewState(layout, availableLayers)
@ -62,9 +72,10 @@ async function main() {
{ mimetype: "application/json" } { mimetype: "application/json" }
) )
) )
: undefined, : undefined
]).AttachTo("maindiv") ]).AttachTo("maindiv")
} }
} }
main().then((_) => {}) main().then((_) => {
})